in reply to Run 2 Subroutines after a die
Then, if open fails you'll execute all the code in the block and then exit, which is what you're trying to do.unless(open TST, ">>$var1") { One(); Two(); exit; # or die "Failed to open $var1: $!"; } # rest of code here.
I alse get 1 at C:\Dev\MiscTests\sub.pl line 8die 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 | |
by Juerd (Abbot) on Oct 03, 2002 at 07:47 UTC | |
Re: Run 2 Subroutines after a die
by Reverend Phil (Pilgrim) on Oct 03, 2002 at 20:11 UTC |