in reply to Re: unless versus if ( ! ) inside a subroutine
in thread unless versus if ( ! ) inside a subroutine
"The thing about Perl is that (excluding explicit returns), Perl will return the last value seen in the function. With your "unless" example, the unless is explicitly not going to ever evaluate because the condition is predetermined to be false.
So the entire unless statement is not evaluated and the return is the last evaluation (your variable assignment).
'If' works differently because it does no pre-evaluation. It evaluates the if block every time to determine if the conditional is met. So the last "evaluation" in the function is the if block (which returns nothing, just like your function).
The magic happening is the pre-evaluation conditions of the unless block. Unless ONLY fires if the condition is met. If fires every time, but performs an automatic JMP outside of that block (with a null return) as soon as the condition evals to false."
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: unless versus if ( ! ) inside a subroutine
by kyle (Abbot) on Jan 15, 2008 at 19:06 UTC | |
|
Re^3: unless versus if ( ! ) inside a subroutine
by almut (Canon) on Jan 15, 2008 at 19:02 UTC | |
by halley (Prior) on Jan 16, 2008 at 14:02 UTC | |
by almut (Canon) on Jan 16, 2008 at 15:11 UTC |