in reply to Re: Can this If/Else be condensed into a 1 liner with a trailing If?
in thread Can this If/Else be condensed into a 1 liner with a trailing If?
As you may or may not know, perl will return the last value in a subroutine it sees... so
will return foo even without an explicit return 'foo';sub foo { 'foo'; }
foo will be printed, since it is the last real value perl saw in the subroutine, everything else was basically blank lines... BUT! one must be careful in this situation because if you ever come back and put code in after the if statement likeprint(foo(),"\n"); sub foo { if(1) { 'foo'; } else { 'bar'; } }
then you will get 1, the return value of a successful print. most people use an explicit return unless the value they are returning is the last line of the sub, since it is then much harder to miss.print(foo(),"\n"); sub foo { if(1) { 'foo'; } else { 'bar'; } print STDERR 'fooed'; }
- Ant
- Some of my best work - Fish Dinner
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Can this If/Else be condensed into a 1 liner with a trailing If?
by George_Sherston (Vicar) on Oct 03, 2001 at 10:25 UTC | |
by suaveant (Parson) on Oct 03, 2001 at 18:17 UTC |