Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

While this isn't what you asked, your evaluate() routine only works in this case by accident of positioning. When it eval's your code, it can only see the lexical $x because the evaluate() subroutine happens to be in the same scope as the original declaration. If you moved the evaluate() subroutine above $x's declaration or out of scope entirely (say, into a library file) things break.

#!/usr/bin/perl -w use strict; sub evaluate{ my $perl=shift || return; print "evaluate\n$perl\n\n"; my $rtn=eval($perl); if($@){return $@;} return $rtn; } my $x=42; my $evalstatement=q| $x; |; print evaluate($evalstatement);
evaluate $x; Global symbol "$x" requires explicit package name at (eval 1) line 2.

For lexical purposes the code is executed at the point eval() is run, not where evaluate() is called. You can't safely use lexicals in code eval'd somewhere else which means you can't safely have a subroutine which takes any ol code as an argument and eval's it.

Also your eval code would be a lot simpler with q{}.


In reply to Your evaluate() routine does not work. by schwern
in thread Stopping execution of an eval statement by slloyd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2022-05-23 02:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (81 votes). Check out past polls.

    Notices?