Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by soonix (Canon)
on Dec 23, 2019 at 08:11 UTC ( #11110536=note: print w/replies, xml ) Need Help??


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

The last statement is unless, not until. Of course, both are conditionals, so I wouldn't bet on the return value anyway.

Replies are listed 'Best First'.
Re^12: printing unitialized value of the 'do BLOCK' (summary)
by LanX (Cardinal) on Dec 25, 2019 at 18:38 UTC
    > Of course, both are conditionals, so I wouldn't bet on the return value anyway.

    This thread is hard to follow, so my 2 cents to sum it up

    • unless is the negation of if
    • if (w/o else) and (the always short-circuiting) and are supposed to do the same
    • hence unless should act as or
    • an if(constant) is optimized at compile time
    Especially the last point might have led to inconsistencies when it comes to return values.

    If you doubt it, that's how it shows in the op-tree

    So yes if and unless are effectively expressions in the form of statements, but returning values.

    Did I miss an aspect of this thread?

    C:\WINDOWS\system32>perl -MO=Concise -e"if ($x) { print $x }" 8 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 - <1> null vK/1 ->8 4 <|> and(other->5) vK/1 ->8 # if +-> and - <1> ex-rv2sv sK/1 ->4 3 <#> gvsv[*x] s ->4 - <@> scope vK ->- - <;> ex-nextstate(main 3 -e:1) v ->5 7 <@> print vK ->8 5 <0> pushmark s ->6 - <1> ex-rv2sv sK/1 ->7 6 <#> gvsv[*x] s ->7 -e syntax OK C:\WINDOWS\system32>perl -MO=Concise -e"unless ($x) { print $x }" 8 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 - <1> null vK/1 ->8 4 <|> or(other->5) vK/1 ->8 # unl +ess -> or - <1> ex-rv2sv sK/1 ->4 3 <#> gvsv[*x] s ->4 - <@> scope vK ->- - <;> ex-nextstate(main 3 -e:1) v ->5 7 <@> print vK ->8 5 <0> pushmark s ->6 - <1> ex-rv2sv sK/1 ->7 6 <#> gvsv[*x] s ->7 -e syntax OK C:\WINDOWS\system32>perl -MO=Concise -e"unless (0) { print $x }" 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 - <@> scope vK/FOLD ->6 - <;> ex-nextstate(main 3 -e:1) v ->3 5 <@> print vK ->6 # alw +ays true 3 <0> pushmark s ->4 - <1> ex-rv2sv sK/1 ->5 4 <#> gvsv[*x] s ->5 -e syntax OK C:\WINDOWS\system32>perl -MO=Concise -e"unless (1) { print $x }" 3 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 # nev +er true - <0> ex-const v/SHORT ->3 -e syntax OK

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

      So yes if and unless are effectively expressions in the form of statements

      They are not.

      $ perl -c -e'my $x = if (f()) { g(); } else { h(); }' syntax error at -e line 1, near "= if" -e had compilation errors. $ perl -e'my $x = unless (f()) { g(); } else { h(); }' syntax error at -e line 1, near "= unless" -e had compilation errors.

      if statements and unless statements are statements, not expressions.

      if (w/o else) and (the always short-circuiting) and are supposed to do the same

      They're not, and they don't.

      $ perl -c -e'my $x = ( f() and g() );' -e syntax OK $ perl -c -e'my $x = ( if (f()) { g() } );' syntax error at -e line 1, near "( if" -e had compilation errors.

      -MO=Concise shows opcodes, not operators. An if statement and an and expression often (but not always) use the and opcode, but that doesn't tell you anything relevant to this conversation.

      Would you say an if statement in C and a while statement in C are the same thing because they both use the same branch opcode?

      In short, you are begging the question. You are presuming an if statement does the same as an and expression to conclude that an if<c> statement does the same as an <c>and expression.

        So it looks like they DO the same, except that one of them can't return a value unless enveloped by 'do{}', 'map{}...' etc.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2020-11-30 02:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?