Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: testing a croak

by BrowserUk (Patriarch)
on Jan 19, 2006 at 06:12 UTC ( [id://524140]=note: print w/replies, xml ) Need Help??


in reply to Re^2: testing a croak
in thread testing a croak

Eval creates major security issues if improper used.

Wrong. That problem only exists with the string form of eval, not the block form shown, which is the standard Perl equivalent of the try()/catch() pairing found in other languages. Ie. it is the proper way to do this.

2. I don't like die

Your prerogative, but it is a standard language facility and deciding you don't like it is arbitrary. Also, remember that not all Perl programs are "web applications".


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: testing a croak
by jbrugger (Parson) on Jan 19, 2006 at 06:40 UTC
    Wrong. That problem only exists with the string form of eval, not the block form shown, which is the standard Perl equivalent of the try()/catch() pairing found in other languages. Ie. it is the proper way to do this.

    I know you're using it the proper way, but i only state to be careful with it and that you absolutely should know what you're doing, (As stated if improper used.. Thats why i try to avoid the usage of it, a mistake is easily made like this:
    #!/usr/bin/perl -w use stritct; my $c1 = 'system("touch /tmp/test1.txt")'; my $c2 = 'system("touch /tmp/test2.txt")'; #used as string (Dangerous!!!) eval $c1; #used as block (ok) eval {$c2}; # <- gives a useless use of private variable in void cont +ext.
    .
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
      You can think of the two versions of eval as completely different 'functions' (although the block form is not a function - it's a flow control statement(?)). Perhaps they should actually have different names, because they do completely different things.

      String eval takes a string, interpolates varibles, and executes the string as Perl code. You use this to delay the evaluation / parsing of whatevers in the string to run-time. It allows you to build Perl programs on the fly and run them. The reason it's dangerous is because the temptation is there to insert user input into the string, effectively allowing the user to execute arbitrary code.

      Block eval doesn't have this issue because the code inside it is parsed at the same time as the rest of the code in the program. Therefore, user values can't be inserted into the code.

      The primary use of block eval is to catch die / croak, etc. Again, this is another slightly strange naming issue in Perl - in this case, your program won't actually die, it just provides a way of exception handling (i.e. passing error information between levels of the application). In most other languages, the block eval is called 'try', and has a corresponding block called 'catch'. In Perl (5 at least, this changes in 6), the same functionality is achieved by eval{} / if ($@).

      I suggest you read the eval POD for full details.
        String eval takes a string, interpolates varibles, and executes the string as Perl code.
        No, eval doesn't interpolate variables. If you say something like eval "$foo$bar", the "" results in a string with $foo and $bar interpolated into it, but that's nothing to do with eval.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-18 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found