Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

RE: RE: Re: Perlmonks Code Proxy

by Boogman (Scribe)
on Aug 19, 2000 at 01:24 UTC ( #28599=note: print w/replies, xml ) Need Help??

in reply to (Ovid) RE(2): Perlmonks Code Proxy
in thread Perlmonks Code Proxy

The /e causes the right hand side of s/// to be handled as code to eval. Well, since we are deliberately matching Perl code with this regex, this solution tends to choke quite spectacularly.
Hmmm... Thats strange. I decided to fool around with it and tried this out:
my $i = 1; my $string = 'print "hello";'; my $content = 'print "hello";$x + 2; print "hello";print "hello";print + "hello";'; $content =~ s/($string)/$1.$i++."\n"/ge; print "$content\n"; eval $string;
and it printed out
print "hello";1 $x + 2; print "hello";2 print "hello";3 print "hello";4 hello
It doesn't seem that it executed the print statement that was matched, even though if we hand the string directly to exec, it does print out hello.

Replies are listed 'Best First'.
RE (tilly) 4: Perlmonks Code Proxy
by tilly (Archbishop) on Aug 19, 2000 at 01:28 UTC
    Right hand side, not left:
    $str = "Hello world\n"; $str =~ s/o/print "Got an o!\n"/eg; print $str;
      yeah - I was saying that if you matched code on the right side, and then used that code in the eval expression with $1, it wouldnot get executed.

      AKA, saying s/(stuff)/$1/eg would not do anything even if the stuff captured in $1 was perl code.

      $string = "print 'got an o\n'"; $string =~ s/(.*)/$1/eg;
      Doesn't print anything.
        OK, I misunderstood what you were trying to figure out.

        Even so ehis should not be a surprise. Execute the statement $var and nothing happens except that the contents of $var are returned. Same thing with $1. Execute it and it returns the contents of $1 as a string, presumably to be placed back in the original string.

        But try using the modifier /eeg and see what that does. :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://28599]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2018-06-22 19:45 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.