[Israel.pm] $^I question

Levenglick Dov-RM07994 dov at freescale.com
Mon Sep 17 23:54:24 PDT 2007


Perldoc says:
    % perl -i.orig -pe 's/foo/bar/g' *.c

you can use the following equivalent code in your program:

    local $^I   = '.orig';
    local @ARGV = glob("*.c");
    while (<>) {
        s/foo/bar/g;
        print;
    }



I have a script which follows this, however it is not doing in place
editing, rather it is printing to the STDOUT (notice, I am running on
Win32). Any ideas?

use strict;
use warnings;
use Cwd;

&print_usage unless defined $ARGV[0] and -d $ARGV[0];
my $search_pattern =  "blah blah blah";

iterate_over_dir($ARGV[0]);

local $^I = '.xxbakxx';

sub iterate_over_dir
{
	my $curdir = Cwd::realpath($_[0]);
	chdir $curdir;

	opendir DIR, "." or warn "Can't open directory $curdir ($!)\n"
and return;
	my @dirs = grep { -d && !/^\.{1,2}$/} readdir DIR;
	rewinddir DIR;
	local @ARGV = grep {-f && /\.(?:c|h|asm)$/} readdir DIR;
	closedir DIR;

	# recursivly call funciton for directories
	foreach (@dirs)
	{
		iterate_over_dir($_);
		chdir $curdir;
	}

	#change files
	return unless scalar @ARGV;
	while (<>) 
	{
	 	print unless /$search_pattern/;
	}
}


sub print_usage
{
	print STDERR "USAGE: $0 <dir_name>\n";
	exit -1;
}  
 
 
Best Regards,
Dov Levenglick
SmartDSP OS Development Team,
DevTech, Technology and System Organization
Freescale Semiconductor Israel
Tel. +972-9-952-2804
The information contained in this email is classified as:
[ ] Freescale General Business Information
[ ] Freescale Internal Use Only
[ ] Freescale Confidential Proprietary
[x] Personal Memorandum



More information about the Perl mailing list