Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Should I fear eval? (want to do two things on the left side of an if/unless)

by tphyahoo (Vicar)
on Aug 10, 2006 at 12:14 UTC ( #566598=perlquestion: print w/ replies, xml ) Need Help??
tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:

I cooked up the following one liner
$ perl -e 'my $type="blee"; eval { print "bad type\n"; print "blee\n" +} unless ($type eq "newer" || $type eq "older")' bad type blee
It has an eval in it, because I want to "do two semicolon-separated things" on the left side of an if/unless. What I mean is, if this bit in curly brackets was on the right side of the if/unless, I wouldn't need the eval statement, it would just flow from the syntax.

I never ever use eval, here I just kind of popped it out to see what would happen.

So I guess what I'm asking is, other than having that extra "eval" statement, is there something else wrong with what I am doing here?

Should I fear eval?

And should I fear trying to do more than one "semicolon-separated thing" on the left side of an if/unless statement?

UPDATE:

1) Changed single quotes to double quotes.

2) Obviously, I wanted "do" here. Thanks for helping -- I think I got three answers to my question in under 45 seconds, and not a single RTFM. wow :)

Comment on Should I fear eval? (want to do two things on the left side of an if/unless)
Download Code
Replies are listed 'Best First'.
Re: Should I fear eval? (want to do two things on the left side of an if/unless)
by Corion (Pope) on Aug 10, 2006 at 12:17 UTC
    And should I fear trying to do more than one "semicolon-separated thing" on the left side of an if/unless statement?

    Yes :-). I'd use do{...} over eval{...} because do doesn't have the nasty habit of eating fatal errors unless I explicitly check for them. I'd still reverse the logic and put the consequent to the right side:

    perl -e 'my $type="blee"; if ($type ne "newer" and $type ne "older") { + print "bad type\n"; print "blee\n"}'
Re: Should I fear eval? (want to do two things on the left side of an if/unless)
by davorg (Chancellor) on Aug 10, 2006 at 12:20 UTC

    Why not just use the more traditional "if/unless" construct?

    unless ($type eq 'newer' || $type eq 'older') { print "bad type\n"; print "blee\n"; }

    Alternatively, you could use "do":

    do { print "bad type\n"; print "blee\n"; } unless ($type eq 'newer' || $type eq 'older');

    "eval" is completely inappropriate in this instance. You shouldn't fear using "eval" - just abusing it :-:

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Should I fear eval? (want to do two things on the left side of an if/unless)
by jhourcle (Prior) on Aug 10, 2006 at 12:20 UTC

    If you just want to do two things, you can use 'do' in place of 'eval'.

    Also, watch your quotes inside a one liner (you're using single quotes inside single quotes)

Re: Should I fear eval? (want to do two things on the left side of an if/unless)
by ikegami (Pope) on Aug 10, 2006 at 15:09 UTC
      If you're wondering if you should fear eval, the answer is yes, as you don't know eval.
Re: Should I fear eval? (want to do two things on the left side of an if/unless)
by broquaint (Abbot) on Aug 11, 2006 at 11:20 UTC
    If you're not using compound statements you can just put the comma operator to use e.g
    my $type="blee"; print("bad type\n"), print("blee\n") unless ($type eq "newer" || $type eq "older");
    HTH

    _________
    broquaint

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2015-07-08 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls