Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

unless versus if ( ! ) inside a subroutine

by Wheeler (Acolyte)
on Jan 15, 2008 at 16:04 UTC ( [id://662493]=perlquestion: print w/replies, xml ) Need Help??

Wheeler has asked for the wisdom of the Perl Monks concerning the following question:

I know the last calculation in a subroutine is automatically the return value but why does 'unless' evaluate differently than 'if not'? Are they not the same? Example:
sub testUnless { my $v = 'Navidson'; unless ( $v ) {}; # returns 'Navidson' } sub testIfNot { my $v = 'Holloway'; if ( ! $v ) {}; # returns nothing }

Replies are listed 'Best First'.
Re: unless versus if ( ! ) inside a subroutine
by kyle (Abbot) on Jan 15, 2008 at 16:49 UTC
    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.

      Here is an answer I received via email. Thoughts?

      "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."

        So the entire unless statement is not evaluated and the return is the last evaluation (your variable assignment).

        This is not exactly true.

        sub t1 { my $v = 'last expression'; my $x = 'last assignment'; unless ( $v ) {} } sub t2 { my $x = 'last assignment'; unless ( 'last expression' ) {} } printf "t1 returns [%s]\n", t1(); printf "t2 returns [%s]\n", t2(); __END__ t1 returns [last expression] t2 returns []

        Looking at the output from B::Deparse, It seems the first unless is left as-is, but the second one (where the condition is a literal) is reduced to !1 (which returns as the empty string).

        If I change each unless to if, the second one reduces the same way, and the first one stays (just like the first unless stays originally).

        So I think the rest of your email response is suspect also. In my testing, if seems to evaluate the same way as unless.

        ...the unless is explicitly not going to ever evaluate because the condition is predetermined to be false.

        This doesn't make much sense to me. In both cases (if and unless) the conditional has to be evaluated. It's just that when the value isn't negated, the last thing evaluated is the value as is. When you request the negated value to be tested in the conditional, the negated value is the last thing evaluated. How else would you explain that the following returns 1?

        sub testUnlessNot { my $v = ''; unless ( !$v ) {}; # returns 1 }
Re: unless versus if ( ! ) inside a subroutine
by Anonymous Monk on Jan 15, 2008 at 18:42 UTC
    I think what is being overlooked is the effect of evaluating the empty statement block {}, which appears in all of the examples of the OP.

    In the statement if ('') {}, the last expression evaluated is the conditional, which is false (the empty string); this is the value returned.

    In the statement if ('true') {}, the last expression evaluated is the true clause, which is the empty statement block, which evaluates to the empty string; this is the value returned.

    In the statement if (! 'true') {}, the last expression evaluated is the conditional, which evaluates to false (the empty string), etc.

    This can be confirmed by re-writing the if statement to the 'statement modifier' form and using do {} for the empty statement (because {} on its own will be interpreted as an anonymous hash constructor).

    E.g.:

    C:\@Work\Perl>perl -wMstrict -e "sub S { if (! $_[0]) {} } print qq(<@{[ S($_) ]}> \n) for @ARGV" "" "hiya" <> <> C:\@Work\Perl>perl -wMstrict -e "sub S { if ( $_[0]) {} } print qq(<@{[ S($_) ]}> \n) for @ARGV" "" "hiya" <> <> C:\@Work\Perl>perl -wMstrict -e "sub S { unless (! $_[0]) {} } print qq(<@{[ S($_) ]}> \n) for @ARGV" "" "hiya" <1> <> C:\@Work\Perl>perl -wMstrict -e "sub S { unless ( $_[0]) {} } print qq(<@{[ S($_) ]}> \n) for @ARGV" "" "hiya" <> <hiya> C:\@Work\Perl>perl -wMstrict -e "sub S { do {} if ! $_[0] } print qq(<@{[ S($_) ]}> \n) for @ARGV" "" "hiya" <> <> C:\@Work\Perl>perl -wMstrict -e "sub S { do {} if $_[0] } print qq(<@{[ S($_) ]}> \n) for @ARGV" "" "hiya" <> <> C:\@Work\Perl>perl -wMstrict -e "sub S { do {} unless ! $_[0] } print qq(<@{[ S($_) ]}> \n) for @ARGV" "" "hiya" <1> <> C:\@Work\Perl>perl -wMstrict -e "sub S { do {} unless $_[0] } print qq(<@{[ S($_) ]}> \n) for @ARGV" "" "hiya" <> <hiya>
Re: unless versus if ( ! ) inside a subroutine
by perrin (Chancellor) on Jan 15, 2008 at 16:18 UTC
    Looks like it returns the value evaluated inside the conditional. Just another reason to use explicit returns.
      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!
          A reply falls below the community's threshold of quality. You may see it by logging in.
      I can respect the fact the many like "unless", but that is one of the very few aspects of Perl that I cannot stand.
Re: unless versus if ( ! ) inside a subroutine
by ambrus (Abbot) on Jan 16, 2008 at 10:15 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://662493]
Approved by citromatik
Front-paged by naikonta
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-20 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found