It seems to me that there are several ways to do this.
Ev i al Way:
As you basically send a regex expression, it can be evaluated to get the result. Like this:
my @args = ('s/hi/bye/', 'hi.h'); my ($regex, @filenames) = @args; for my $file (@filenames) { eval("\$file =~ $regex"); print "OUTPUT: $file\n"; }
Of course, after that some unpleasant surprises will be revealed to you. For example, consider passing this set of arguments:
... my @args = ('s/hi/bye/; print qq{MINE IS AN EVIL LAUGH!\n}', 'hi.h'); ...
Yes, he will laugh at you the most.
illy.
Safe way:
my ($regex_expr, @filenames) = @args; my ($substr, $replace) = $regex_expr =~ m
As you can see, we analyze the expression given to us in two parts, then use these parts to create a complete statement. Obviously, this approach is less flexible, but, of course, it is much safer.
The easiest way:
my ($search, $replace, @filenames) = @args; for my $file (@filenames) { $file =~ s/$search/$replace/; print "OUTPUT: $file\n"; }
Yes, that’s right - no regular expression exists! Here we decided to accept two arguments - "search pattern" and "string replacement" - instead of one. Will this make the script less flexible than the previous one? No, since we still needed to regularly analyze the regex expression. But now the user clearly understands all the data that is given to the team, which is usually an improvement. )
@args in both examples corresponds to the @ARGV array.