Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Small examples of string eval

by ruzam (Curate)
on May 14, 2006 at 03:16 UTC ( #549287=note: print w/replies, xml ) Need Help??

in reply to Small examples of string eval

I have two places where I use eval all the time. I think eval is the best choice in these situations, maybe someone has a better alternative (that doesn't require non-core modules).

1) retrieving stored hashes
I regularily used Data::Dumper to turn hashes into strings so they can be stored in a database for later retrieval. The string accurately represents the original hash with out my having to do a thing and it's safe to store anyway I have to, flat files, dbm, mysql. When the data is later retrieved, eval is the easiest way to get it back into a useful hash. Something like this:
use Data::Dumper; $Data::Dumper::Indent = 0; # no spaces tabs or other fluff $Data::Dumper::Purity = 1; # include code to recreate recursive refere +nces sub store_hash { my $hash_ref = shift; my $saved = Dumper($hash_ref); # store $saved somewhere } sub get_hash { # get $saved from somewhere my $VAR1; eval {$saved} return $VAR1 ? $VAR1 : {}; }

1) wrapping DBI calls to capture errors. Far as I know this is the recommended (only?) way to do this.
sub selectrow_array { my $dbh = shift; my $query = shift; my @result; eval { @result = $dbh->selectrow_array($query) }; return my_custom_error_handler("selectrow_array $query") if $@; return @result; }
More generically I guess you could say eval is the tool of choice when ever you have to call code beyond your control that may kill your script.

Replies are listed 'Best First'.
Re^2: Small examples of string eval
by eyepopslikeamosquito (Chancellor) on May 14, 2006 at 03:43 UTC

    eval { @result = $dbh->selectrow_array($query) }; return my_custom_error_handler("selectrow_array $query") if $@;
    That is an example of block eval, not string eval; the OP was asking about string eval. There is wide agreement that block eval is a good thing in Perl 5 (for exception handling). Overloading eval with two different meanings in Perl 5 was unfortunate and is fixed in Perl 6, where block eval is spelled try.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://549287]
robby_dobby is not entirely serious
[LanX]: ... thank God France doesn't have an electoral college...
[Eily]: LanX, let's hope it gets better for the second turn
vrk adds oatmeal cookies to the platter on the sideboard.
[LanX]: evrybody can participate in a YAPC
choroba is still undecided what to present at YAPC
[LanX]: imagine the stock market if the choice for the second round would have been those two ...
[robby_dobby]: LanX: Not me. No YAPC in my country and the only one in Asia is so far off in Japan
[LanX]: Oh I thought "Misery" meant Missoury
[LanX]: where do you live?

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2017-04-24 15:55 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (442 votes). Check out past polls.