Why can't I use
if($l =~ $ARGV) but I can use
if($l =~ /$ARGV/g?
$ perl script.pl '/^[\w]/g'
$ perl script.pl '^[\w]'
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
Strings and regexes are different primitive types in perl, and all variables placed in the
@ARGV array are simply strings given to the program by the kernel at startup;
$ARGV is a scalar string, and not a regex.
When you do
if($l =~ $ARGV) and
'/^[\w]/g' this is equivalent to
if($l =~ '/^[\w]/g') instead of
if($l =~ /^[\w]/g). In the former case the slashes are simply characters in a string while in the later they are a part of the Perl syntax that delimits a regular expression.