Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Double Interpolation in // operator?

by BlueLines (Hermit)
on May 24, 2001 at 22:52 UTC ( [id://83068]=perlquestion: print w/replies, xml ) Need Help??

BlueLines has asked for the wisdom of the Perl Monks concerning the following question:

Here's some code:
#!/usr/bin/perl -w use strict; sub FOO { return qw($bar $foobar); } my $foo = '$bar'; print "It matched!\n" if (grep /$foo/, FOO());
This fails, even though it should work. And this code works if i change this line:
my $foo = '\$bar';
Which looks (to me) like /$foo/ is interpolating $foo twice. Why is this?

BlueLines

Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.

Replies are listed 'Best First'.
Re: Double Interpolation in code///code operator?
by japhy (Canon) on May 24, 2001 at 23:00 UTC
    Please look at your regex. When you do /$foo/, it interpolates $foo to the string '$bar'. This makes your regex try to match end-of-line ($), followed by 'bar'. That can't happen.

    If $foo is '\$bar', then you have a different case. You try to match '\$', which is a literal dollar-sign, followed by 'bar'. That works.

    Watch:
    friday:~ $ perl -mre=debug $re = '$bar'; print map ">$_\n", grep /$re/, qw( $foo $bar ); compiling RE `$bar' size 5 first at 1 1: EOL(2) 2: EXACT <bar>(5) 5: END(0) anchored `bar' at 0 (checking anchored) minlen 3 Matching `$bar' against `bar' Setting an EVAL scope, savestack=12 1 <$> <bar> | 1: EOL friday:~ $ perl -mre=debug $re = '\$bar'; print map ">$_\n", grep /$re/, qw( $foo $bar ); compiling RE `\$bar' size 4 first at 1 1: EXACT <$bar>(4) 4: END(0) anchored `$bar' at 0 (checking anchored isall) minlen 4 >$bar


    japhy -- Perl and Regex Hacker
      *forehead smack*

      I spent two hours last night pouring over this, and that never occured to me. ++ and thanks.

      BlueLines

      Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://83068]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found