in reply to unless versus if ( ! ) inside a subroutine

Looks like it returns the value evaluated inside the conditional. Just another reason to use explicit returns.
  • Comment on Re: unless versus if ( ! ) inside a subroutine

Replies are listed 'Best First'.
Re^2: unless versus if ( ! ) inside a subroutine
by Wheeler (Acolyte) on Jan 15, 2008 at 16:37 UTC
    If we change
    if ( ! $v ) { # ...
    to
    if ( $v ) { # ...
    It still returns nothing. So it appears with 'if', it does not return the value evaluated inside the conditional. But why?
      How about trying explicit returns?
      sub testUnless { my $v = 'Navidson'; unless ( $v ) { return undef; } else { return $v; } } sub testIfNot { my $v = 'Holloway'; if (!$v) { return undef; } else { return $v; } }
      You have to understand that Perl will return the last evalution from a sub if you are not explicit. So start being explicit!
        The question is not on recommended practices to fix a problem but rather the perl internals regarding if and unless.
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: unless versus if ( ! ) inside a subroutine
by perlfan (Parson) on Jan 15, 2008 at 17:23 UTC
    I can respect the fact the many like "unless", but that is one of the very few aspects of Perl that I cannot stand.