Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: printing unitialized value of the 'do BLOCK'

by Eily (Monsignor)
on Dec 17, 2019 at 15:42 UTC ( [id://11110286]=note: print w/replies, xml ) Need Help??


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

Fletch pointed out in the CB that using -x7 with B::Deparse gives the following confirmation:

perl -MO=Deparse,-x7 -e "if ($a) { &BLOCK }" $a and do { &BLOCK }; -e syntax OK
Where if (EXPR) {BLOCK} is equivalent to EXPR and do { BLOCK } which confirms haukex's interpretation. It's worth noting that if the two forms are equivalent, the latter is probably a lot less confusing when the output value is used.

Replies are listed 'Best First'.
Re^4: printing unitialized value of the 'do BLOCK'
by LanX (Saint) on Dec 17, 2019 at 20:01 UTC
    > It's worth noting that if the two forms are equivalent, the latter is probably a lot less confusing when the output value is used.

    It's also worth noting that B::Deparse can't always be trusted.

    The clause of the if has the same scope like the inner block ( with if (CLAUSE) {BLOCK} )

    DB<3> use strict; my $x=3; if (my $x =1) { print $x } 1 DB<4> use strict; if (my $x =1) { print $x }; print $x Global symbol "$x" requires explicit package name (did you forget to d +eclare "my $x"?) at (eval 13)[C:/Perl_524/lib/perl5db.pl:737] line 2. DB<5>

    but Deparse fails to cover this with -x7

    >>perl -MO=Deparse,-x7 -e"if (my $x =1) { print $x }; print $x" my $x = 1 and do { print $x }; print $x; # <-- no error under strict! -e syntax OK

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

Re^4: printing unitialized value of the 'do BLOCK'
by haukex (Archbishop) on Dec 17, 2019 at 16:09 UTC

    Very nice, thank you!

Re^4: printing unitialized value of the 'do BLOCK'
by rsFalse (Chaplain) on Dec 23, 2019 at 08:22 UTC
    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 { () };
      > 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11110286]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-18 01:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found