Peeter Joot's (OLD) Blog.

Math, physics, perl, and programming obscurity.

Archive for August, 2010

perl implicit matching operator.

Posted by peeterjoot on August 26, 2010

Perl can regularly surprise you with many ways of doing the same thing. I’d seen the following fragment of a script and thought “how can that work … there’s no match operator”.

if ( $CC =~ "xlC|xlc" )
{
  # stuff.
}

What I’d expected is a match // operator, or one that used explicit delimiters such as m@@, as in one of:

if ( $CC =~ /xlC|xlc/ )
{
  # stuff.
}

# or

if ( $CC =~ m@xlC|xlc@ )
{
  # stuff.
}

I’d had the urge to “correct” this incorrect if condition, but a small experiment confirms that this code actually worked as intended:

$CC = "/blah/xlC" ;
#$CC = "/blah/gcc" ;

if ( $CC =~ "xlC|xlc" )
{
print "m\n" ;
}

No explicit match delimited expression is required. It appears that the context of the binding operator is enough to convert a subsequent string into a regular expression. I think I still like doing it explicitly better, perhaps just since that’s how I’ve seen it done the most.

Advertisements

Posted in perl and general scripting hackery | Tagged: , | 2 Comments »