Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Run 2 Subroutines after a die

by jarich (Curate)
on Oct 03, 2002 at 07:17 UTC ( [id://202450]=note: print w/replies, xml ) Need Help??


in reply to Run 2 Subroutines after a die

Try the following instead:
unless(open TST, ">>$var1") { One(); Two(); exit; # or die "Failed to open $var1: $!"; } # rest of code here.
Then, if open fails you'll execute all the code in the block and then exit, which is what you're trying to do.

I alse get 1 at C:\Dev\MiscTests\sub.pl line 8
die is a function that takes a string and exits the program printing that message out with the line number and script name. In this case die is getting the result of your do{} as its string and printing it: "1". The result of your do just happens to be the return value of your subroutine Two, which is 1. (print returns 1 if it printed, and if there is no explicit return, the value of the last line (of code) in a subroutine is the return value).

If you try my suggestion from above, you won't get that any more.

Hope it helps.

jarich

Short answer to below
Yes, you're entirely correct. if( !cond ) is identical to unless( cond ). unless( cond ) { ...... } else { ......} is also legal (although there is no elsunless) but I'd encourage you to change any unless-then-else structures you might be tempted to write into if-then-else because that is much easier to read (mostly because that's what everyone else expects to see).

I prefer unless because I find that sometimes I don't see the ! in if( !$foo ). Readability kinda depends on the reader. unless does take more character presses...

Replies are listed 'Best First'.
Re: Re: Run 2 Subroutines after a die
by AcidHawk (Vicar) on Oct 03, 2002 at 07:38 UTC

    Would I be correct in assuming that

    if(! open TST, ">>$var1"){ }

    would just be another way of doing

    unless(open TST, ">>$var1"){ }

    If so, is this just another way of doing things or are there further benefits for using unless. I did once read that the code we use should be readable to humans and that the computers will still interpret our code. Is this a case of unless being easier to read than if not?

    -----
    Of all the things I've lost in my life, its my mind I miss the most.

      Would I be correct in assuming that if(! open TST, ">>$var1"){ } would just be another way of doing unless(open TST, ">>$var1"){ }

      Yes, you would.

      If so, is this just another way of doing things or are there further benefits for using unless. I did once read that the code we use should be readable to humans and that the computers will still interpret our code. Is this a case of unless being easier to read than if not?

      unless (foo) is just another way of writing if (not (foo)). It's up to you to decide what you find better readable.

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

Re: Run 2 Subroutines after a die
by Reverend Phil (Pilgrim) on Oct 03, 2002 at 20:11 UTC
    As a point of preference, I prefer the unless blocks also.. I generally use them in just such a situation, and besides not missing the ! in the !condition, when I see an unless block I know that I'm generally handling failure. I know... in all conditional blocks, you're acting upon something either 'succeeding' or 'failing', but when I'm specifically putting something in there that is mainly sort-of error checking things that I'm assuming (files exist and are openable, etc.), I'll use unless.

    -=rev=-

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://202450]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 14:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found