Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.

In reply to Re: Small examples of string eval by ruzam
in thread Small examples of string eval by spurperl

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (10)
    As of 2014-07-23 08:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (137 votes), past polls