Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Are we seeing syntax inconsistency?

by Aristotle (Chancellor)
on Nov 11, 2005 at 17:56 UTC ( [id://507796]=note: print w/replies, xml ) Need Help??


in reply to Re: Are we seeing syntax inconsistency?
in thread Are we seeing syntax inconsistency?

No, that doesn’t work in the general case. It would work here, because the string which eval sees is, say, sub { print 0 } on the first iteration. But if the loop was for( "foo bar", "baz quux" ) ){ ... }, then the eval would incorrectly be called to compile sub { print foo bar }, which may fail to compile or may compile to nonsense.

You want to stick the value in a lexical and close over the lexical. That is the correct general solution.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^3: Are we seeing syntax inconsistency?
by sgifford (Prior) on Nov 11, 2005 at 18:22 UTC
    Right, I gave that as an example to clarify what's going on, not a recommendation. Though I think it can be made to work in the general case with judicious use of quotes and quotemeta. :)

      You mean something like eval qq{ sub { print "\Q$_\E" } }? Not what I think of as a pretty solution, since you’re firing up the compiler (and repeatedly) at runtime, but yeah, that would do it.

      Makeshifts last the longest.

        It may not be pretty, but I personally believe that, on a deeper level, the Perl interpreter enjoys this approach more. :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found