On 10/14/09 Wed  Oct 14, 2009  3:38 PM, "Felix Dorner" <felix...@web.de>
scribbled:

> Hi,
> 
> I did the best book purchase in years: The Perl Cookbook. They have an
> example that seems to come right from Larry Wall himself. And I don't
> get it. I can use it but I don't understand why it works with wildcards.
> 
> $op = shift or die "Usage: rename expr [files]\n";
> chomp (@ARGV = <STDIN>) unless @ARGV;
> for(@ARGV) {
>     $was = $_;
>     eval $op;
>     die $@ if $@;
>     rename ($was, $_) unless $was eq $_;
> }
> 
> 
> #rename.pl s/\.orig// httpd.conf.orig
> 
> will rename httpd.conf.orig to httpd.conf. But it also works with wildcards:
> 
> #rename.pl s/\.orig// *
> 
> will chop .orig from all files in the current directory.
> So it assigns <STDIN> to @ARGV. But <STDIN> is just a * right? Does the
> shell expand this, or perl? Any comments/detailed explanations welcome.

The shell will expand the '*' to all files in the current directory and
place the file names in the @ARGV array, with the regular expression as the
first element $ARGV[0]. The RE is shifted out of the @ARGV array, leaving
the files names to be processed by the for loop.

If there are no arguments left in @ARGV after the RE has been removed, then
standard input will be read to supply a list of file names. Therefore, you
can pipe the results of some command that generates file names, such as ls
or find, to rename:

    ls *.dat | rename s/dat$/data/


-- 
Jim Gibson



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to