Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Try string, not block

by pushtaev (Sexton)
on Sep 12, 2012 at 08:03 UTC ( #993171=perlquestion: print w/replies, xml ) Need Help??
pushtaev has asked for the wisdom of the Perl Monks concerning the following question:


We are using Try::Tiny module in our project. I'm always using "try" instead of "eval" whenever I can, but eval can do some things, that "try" cannot: it's evaluation code from strings.

So, when I use eval() with string I should do something like this:

eval "require $module" or $self->error("...");

instead of nice and pretty

try "require $module" # It's not going to work catch { $self->error("..."); }

I can try something like this:

try { eval "require $module" or die $@; } catch { $self->error("..."); }

but it still looks ugly, because it's actually eval{eval""}.

Should I worry about this? Should I suggest improving Try::Tiny so it can "try" strings, not only blocks?

P. S. I believe the root of the problem is perl "eval" itself. It does two things for me (evaluate strings AND catch "exceptions") that are not really connected.

Replies are listed 'Best First'.
Re: Try string, not block
by tobyink (Abbot) on Sep 12, 2012 at 08:09 UTC

    You can suggest it, but I don't think it'll be accepted.

    (Besides which, I doubt it would actually be possible to implement without breaking the "Tiny" philosophy. try gets its block-handling syntax by using a sub prototype of (&;@). To be able to accept a string you'd need to part ways with that prototype which would make using Try::Tiny a lot uglier and would break backwards compatibility for all existing users of Try::Tiny; or you'd need to write a lot of custom parsing code using something like Devel::Declare, and that would make it a lot less tiny.)

    Stringy eval is usually a sign of code smell (i.e. a sign that you're doing something bizarre that could be handled better another way). Taking your eval "require $module" as an example, you should take a look at Class::Load or Module::Runtime.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      It's just an example. I understand that "eval $string" is not in common usage, but question is how do I do it properly when I need to.

Re: Try string, not block
by GrandFather (Sage) on Sep 12, 2012 at 08:15 UTC

    Don't use string eval. Use block eval instead. Try::Tiny is pretty much just syntactic sugar wrapped around a block eval in any case.

    String eval is open to vast abuse and, for the code you have shown, is simply not needed. If you do need to use string eval ugly is good.

    True laziness is hard work

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://993171]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-01-21 09:36 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.