Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How do you evaluate a perl command that is in a perl string?

by perlNinny (Beadle)
on Jan 20, 2006 at 20:21 UTC ( #524566=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to write the debug function below that will display both commands and variables if in debug mode and ask before executing a command. However, the exec command doesn't seem to be working as I inteded. What am I doing wrong?
{ # executing a DOS copy command &Debug("system(\"copy c:\\oldversion.txt c:\\version.txt \");", 1); } sub Debug { my ($display, $executeable) = (@_); if ( $debug == 1 ) { print "$display\n"; if ($executeable == 1) { print "Do you wish to execute? (y/n):"; my $answer = <STDIN>; chomp($answer); if ($answer ne "y" && $answer ne "Y" ) { print "\n"; $executeable = 0; } } } if ($executeable == 1) { eval { $display }; if ($@) # $@ contains the exception that was thrown { print "Exception $@"; } print "$display executed\n"; } }

Replies are listed 'Best First'.
Re: How do you evaluate a perl command that is in a perl string?
by ikegami (Patriarch) on Jan 20, 2006 at 20:28 UTC

    Instead of
    eval { $display };
    use
    eval $display;

    The former executes the code $display (not the code stored in $display), which simply returns the value of $display. The latter compiles and executes the Perl code stored in $display. Both trap exceptions.

    Reference: eval

    Update: Roy Johnson brought it to my attention that you have insufficient slashes in your Debug argument.
    &Debug("system(\"copy c:\\oldversion.txt c:\\version.txt \");", 1);
    results in the string
    system("copy c:\oldversion.txt c:\version.txt ");
    but that's not what you want. You want
    system("copy c:\\oldversion.txt c:\\version.txt");
    so you should be using
    &Debug("system(\"copy c:\\\\oldversion.txt c:\\\\version.txt\");", 1);

Re: How do you evaluate a perl command that is in a perl string?
by zombie_fred (Scribe) on Jan 20, 2006 at 20:42 UTC
    I think that you want to use
    eval $string;
    not
    eval { $string };
    The string form of C<eval> will cause the string to be compiled and executed at run-time, capturing the result in $@.

    The block form of C<eval> gets compiled only once during the compilation pass of the main program and at run-time the result of executing $string are captured into $@.

    Both versions of C<eval> isolate the main program from failures of the code in $string, but only the string-form causes the code to be compiled each time the C<eval> is encountered.

    --
    zf

      the result of executing $string are captured into $@.

      The parent is essentially correct but this part is not quite. $@ captures not the "result" of the evaluation, but rather the error message that would be displayed when the eval code died had it not been an eval. If the eval code finishes normally and does not die, then $@ is undefined.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2022-08-07 16:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?