Operating System - HP-UX
1832098 Members
3011 Online
110038 Solutions
New Discussion

perl - using a scaler variable to represent an R.E.

 
Mr Peter Kempner
Occasional Contributor

perl - using a scaler variable to represent an R.E.

I am trying to write a perl script and I am confused. I want to use a scalar variable to hold an RE against which to pattern match. It doesnt work. eg

$pattern='/humpty|jack/'
.
.
if ($variable_x =~ $pattern)
{
print 'Match found for humpty or jack'
}

Does not work.

If instead of $pattern I use /$var_a|$var_b/ where $var_a = humpty and $var_b = jack then it works fine! What am I not getting???
5 REPLIES 5
harry d brown jr
Honored Contributor

Re: perl - using a scaler variable to represent an R.E.

Change $pattern to

$pattern=/humpty|jack/;

get rid of the quotes

live free or die
harry
Live Free or Die
Rodney Hills
Honored Contributor

Re: perl - using a scaler variable to represent an R.E.

The "/" is part of the syntax for the matching operation.

Therefore do the following-

$pattern='humpty|jack'
.
.
if ($variable_x =~ /$pattern/)
{
print 'Match found for humpty or jack'
}

Should work.

Note- If $pattern does not change, you can use the "o" option (ie /$pattern/o) to have perl generate more effecient code for execution.

-- Rod Hills

There be dragons...
harry d brown jr
Honored Contributor

Re: perl - using a scaler variable to represent an R.E.

Rodney,

I like your solution much better!

live free or die
harry
Live Free or Die
H.Merijn Brand (procura
Honored Contributor

Re: perl - using a scaler variable to represent an R.E.

You got it almost right the first time, but you forgot to read the docs (use the qr// operator):

$pattern = qr/humpty|jack/;
.
.
if ($variable_x =~ $pattern)
{
print 'Match found for humpty or jack'
}

Harry's solution is plain wrong: it'd asign the result of matching $_ against the pattern. Rodney's solution is technically correct, but still suboptimal and error-prone.
Enjoy, Have FUN! H.Merijn
Ralph Grothe
Honored Contributor

Re: perl - using a scaler variable to represent an R.E.

Sorry, a wee "correction" to Rodney's answer.

In deed you can use almost any character pair to embrace search and substituion patterns when using the operators m, s, y, tr

At the shell type

perldoc perlop
perldoc -f m
perldoc -f y
perldoc -f s
perldoc -f qr


This comes handy when you have to deal with paths, as subdirectories are delimited by /, which you otherwise would have to escape by
instead of writing

$path =~ /\/usr/\/local/\/bin/

you can say

$path =~ m|/usr/local/bin/|

or

$path =~ m(/usr/local/bin)

Of course you can also reference variables in patterns

@users = qw(Humpty Jack);

print "found match\n" if ($name =~ /$users[0]|$users[1]/)

You can even use the qr operator if you insist

$pattern = qr(Humpty|Jack);
print "found match" if $name =~ $pattern;

You can inverse logic by using '!~' instead of '=~'
But this should be familiar from awk.





Madness, thy name is system administration