On 7/1/05, Moon, John <[EMAIL PROTECTED]> wrote:
> The following is not returning what I had expected...
>
> SUN1-BATCH>perl -e '$a=q{/var/run}; $home=q{/var/123};print "Yes - $a like
> $home\n" if $a =~ /^$home/;'
> SUN1-BATCH>perl -e '$a=q{/var/run}; $home=q{/var/ra};print "Yes - $a like
> $home\n" if $a =~ /^$home/;'
> SUN1-BATCH>perl -e '$a=q{/var/run}; $home=q{/var/ru};print "Yes - $a like
> $home\n" if $a =~ /^$home/;'
> Yes - /var/run like /var/ru
>
>
> I would have "assumed" that /var/run would NOT be "like" /var/ru just as
> /var/run is not "like" /var/ra...
>
> John W Moon
>
John
A regex match checks to see if the specified pattern appears in the
specified string. And the answer to the question "is /var/ru in
/var/run?" is "yes." Or to put it another way:
$a =~ /$home/
is functionally (although not proceedurally) equivalent to:
$a =~ /^.*$home.*$/
If you want to do a simple test for equality, use 'eq'. If you're
going to test for a pattern and want to match on the entire string,
anchor the patern at the beginning and end of the string:
$a =~ /^$home$/
but if $home is a simple string without regex metacharaters 'eq' it
going to be a lot faster than m//.
HTH,
-- jay
--------------------
daggerquill [at] gmail [dot] com
http://www.tuaw.com
http://www.dpguru.com
http://www.engatiki.org
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>