in reply to unless versus if ( ! ) inside a subroutine
use strict; use warnings; sub testUnless { my $v = 'Navidson'; unless ( $v ) {}; # returns $v } sub testIfNot { my $v = 'Holloway'; if ( ! $v ) {}; # returns ! $v } sub testIf { my $v = 0; if ( $v ) {} # returns $v } sub testNothing { my $v = 'kyle'; if ( $v ) {} # returns nothing } printf "testUnless -> [%s]\n", testUnless(); printf "testIfNot -> [%s]\n", testIfNot(); printf "testIf -> [%s]\n", testIf(); printf "testNothing -> [%s]\n", testNothing(); __END__ testUnless -> [Navidson] testIfNot -> [] testIf -> [0] Use of uninitialized value in printf ... testNothing -> []
In every case (except testNothing), it's returning the last expression evaluated. In testNothing, it's returning nothing (which is turned to undef and then stringified to an empty string). Perhaps the confusion here is that the false value returned by !$v is a defined empty string, and not zero.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: unless versus if ( ! ) inside a subroutine
by Wheeler (Acolyte) on Jan 15, 2008 at 18:30 UTC | |
by kyle (Abbot) on Jan 15, 2008 at 19:06 UTC | |
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 |