On 2009-05-29, A. Sinan Unur <> wrote in
comp.lang.perl.misc:
> First, you don't need substitution. You need to capture matches.
> Second, just pass the pattern string to the program.
> [sinan@kas ~]$ cat s.pl
> #!/usr/bin/perl
> use strict;
> use warnings;
> my $pattern = shift @ARGV;
> my $re = qr/$pattern/;
> for my $arg ( @ARGV ) {
> if ( my @parts = ( $arg =~ $re ) ) {
> my $subdir = join q{}, @parts;
> # put actual code to mkdir, move etc
> print "$arg goes in $subdir\n";
> }
> }
> __END__
> [sinan@kas ~]$ perl s.pl '\.(\w{1,3})$' *
> wallpaper.png goes in png
> test.zip goes in zip
> etc.zip goes in zip
> s.pl goes in pl
> t.pl goes in pl
> www.zip goes in zip
Very Nice. Thank you. I have something similar (though much, much
cruder, and probably much slower) working now, but I'll put your
suggestions to good use.
Now if you don't mind, for my edification, two questions:
What is the 'if' statement testing? What is
( @parts = ( $arg =~ $re ) )
doing? What values is @parts being populated with?
What exactly is the q{} operator doing in the join statement? What is
its content, so to speak? Is it just the same as '' here?
Thanks again for your advice. I'm slowly picking up 'idiomatic perl', so
I'm always eager to learn new things.
On a side note, how /would/ one pass a whole s/// operator on the
command line (sed-style, I guess), supposing you wanted to do
interesting things with backreferences, the g and i modifiers, etc.?