in reply to Re^3: printing unitialized value of the 'do BLOCK'
in thread printing unitialized value of the 'do BLOCK'

Hey!

So it means that an 'if' statement in function/RHS context returns the last expression evaluated! And this isn't documented I guess.
I guess newcomer would expect to get a 1 or ''/0 as a return value of 'if', but he can get also an 'undefined'. E.g. print do { 3 if undef };

.=
Interestingly, only one of these lines gives a warning:
print do { 3 if () }; print do { () };

Replies are listed 'Best First'.
Re^5: printing unitialized value of the 'do BLOCK'
by LanX (Saint) on Dec 23, 2019 at 12:54 UTC
    > Interestingly, only one of these lines gives a warning:

    because the second one returns an empty list, which is a legal use of print.

    C:\WINDOWS\system32>Perl -MData::Dump=pp -e "pp do{3 if ()} " undef C:\WINDOWS\system32>Perl -MData::Dump=pp -e "pp do{()} " ()

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Thanks, now I understand the difference: in the first case '()' wanted in scalar context which was forced by 'if' (so it becomes an undefined value).

      Added:
      An 'if' forces scalar context as 'and' do, so it may be that these two are internally implemented the same.