Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

if and unless

by Anonymous Monk
on Dec 15, 2001 at 23:17 UTC ( #132230=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Err, I'm very very very new to Perl. I would like to ask if syntax like this can work:
if (something complies to conditions) {
do something else
} unless {
something screws up
}
I've tried like everywhere, and I still don't know. Help.

Replies are listed 'Best First'.
Re (tilly) 1: if and unless
by tilly (Archbishop) on Dec 15, 2001 at 23:27 UTC
    That is (of course) incorrect syntax, but the program flow you want can be achieved with exception handling, which in Perl is done with eval:
    if (test_conditions(@info)) { eval {do_something(@info)}; if ($@) { warn "Failed to do whatever because '$@'"; } }
    and then in your do_something function you would just die whenever you detect that you "screwed up".

      I wonder if he also meant this:

      if (test_conditions(@info)) { eval(do_something(@info)); warn "Failed to do whatever because '$@'" if ($@); } else { # handler method for whenever something is 'screwed up' # such as when test_conditions() fails... something_screwed_up(); }
      ps: actually, I'm sure there might be quite a few implementations for the original 'pseudo' code since it contains some uncommon language constructs/flow. Say,  if(..) { } unless {} isn't something you find in a standard pseudo code? Also the meaning of the 'unless' keyword is a bit vague.

      --
      print join(" ", map { sprintf "%#02x", $_ }unpack("C*",pack("L",0x1234 +5678)))
        The program flow apparently meant from your comment does not match the program flow of the code.

        My warn was merely a reasonable implementation of your something_screwed_up() function. If you want it to be called, it has to be called there.

Re: if and unless
by Spenser (Friar) on Dec 16, 2001 at 14:16 UTC

    Since your syntax is a little off (as tilly pointed out), I'm not exactly sure what you're asking.  Looking at the interpretations by the others above me, I'm even less sure of what you want.  But, let me point out some simple and obvious things since you are as you say, new to Perl and to this site.

    You may not have figured this out yet, but the Perl Monks site has very good tutorials.  If you look in the Tutorial section of this site, in particular the pages on if statements and unless statements, you should find your answers pretty quickly.

    After reviewing these two tutorial pages you will be better equiped to ask a more detailed of a question, using the syntax and layout presented in the tutorials.

    Incidentally, I hope my comments don't sound condescending.  They are not meant to be.  I'm still pretty new to Perl myself and have figured out the two basic things I'm mentioning here:  the tutorials are helpful, and one can get good feedback from the monks when one is specific and when one asks questions using the vocabulary and common points of reference.

Re: if and unless
by tfrayner (Curate) on Dec 16, 2001 at 21:36 UTC
    Hmm...

    Possibly the following is what you want. It depends on whether the 'something screwing up' happens during execution of your 'do something else' or whether it just happens to be an input into the whole condtional, as shown here:

    if ((some condition) && !(some counter-condition)) { do something }
    As has been obliquely pointed out, unless can really be thought of as the opposite of if, so your code is essentially two consecutive and unrelated conditionals.

    Sorry if this is not what you're after. It's hard to be sure :-)

    Tim

Re: if and unless
by strat (Canon) on Dec 17, 2001 at 18:56 UTC
    I guess:
    if ( ...... ){ &DoSomething(); } else { &SomethingScrewedUp(); }

    Best regards,
    perl -e "print a|r,p|d=>b|p=>chr 3**2 .7=>t and t"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2020-07-02 08:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?