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.
In reply to Re: unless versus if ( ! ) inside a subroutine
by kyle
in thread unless versus if ( ! ) inside a subroutine
by Wheeler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |