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

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:

Hello.

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.

Comment on Try string, not block
Select or Download Code
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 (Cardinal) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-12-28 17:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls