Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: return if 0

by massa (Hermit)
on Jun 22, 2009 at 18:24 UTC ( #773720=note: print w/replies, xml ) Need Help??


in reply to Re: return if 0
in thread return if 0

perl -E 'sub result { return 1 if "0" } say result'
yields
0
and
perl -E 'sub result { return 1 if "0 but true" } say result'
yields
1
thus proving your statement :-D
[]s, HTH, Massa (κς,πμ,πλ)

Replies are listed 'Best First'.
Re^3: return if 0
by ikegami (Pope) on Jun 22, 2009 at 19:31 UTC
    Not really, but the following does:
    $ perl -MData::Dumper -e'print Dumper sub{return 1 if $_[0]}->(undef)' $VAR1 = undef; $ perl -MData::Dumper -e'print Dumper sub{return 1 if $_[0]}->(0 )' $VAR1 = 0; $ perl -MData::Dumper -e'print Dumper sub{return 1 if $_[0]}->("0" )' $VAR1 = '0'; $ perl -MData::Dumper -e'print Dumper sub{return 1 if $_[0]}->("" )' $VAR1 = '';

    Update: Switched to DD to show the difference between string zero and numerical zero.

      Look how interesting:
      $ perl -MData::Dumper -e 'print Dumper (do { 1 if $_ }) for undef, 0, +"0", ""' $VAR1 = undef; $VAR1 = 0; $VAR1 = '0'; $VAR1 = ''; $ perl -MData::Dumper -e 'print Dumper ($_ and 1) for undef, 0, "0", " +"' $VAR1 = undef; $VAR1 = 0; $VAR1 = '0'; $VAR1 = ''; $ perl -MO=Concise -e 'sub a { 1 if $_ } print a for undef, 0, "0", "" +' k <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 2 -e:1) v:{ ->3 3 <;> nextstate(main 2 -e:1) v:{ ->4 j <2> leaveloop vK/2 ->k a <{> enteriter(next->g last->j redo->b) lK/8 ->h - <0> ex-pushmark s ->4 - <1> ex-list lKM ->9 4 <0> pushmark sM ->5 5 <0> undef s ->6 6 <$> const[IV 0] sM ->7 7 <$> const[PV "0"] sM ->8 8 <$> const[PV ""] sM ->9 9 <#> gv[*_] s ->a - <1> null vK/1 ->j i <|> and(other->b) vK/1 ->j h <0> iter s ->i - <@> lineseq vK ->- f <@> print vK ->g b <0> pushmark s ->c e <1> entersub[t2] lKS/TARG,1 ->f - <1> ex-list lK ->e c <0> pushmark s ->d - <1> ex-rv2cv sK/128 ->- d <#> gv[*a] s ->e g <0> unstack v ->h -e syntax OK $ perl -MO=Concise -e 'sub a { $_ and 1 } print a for undef, 0, "0", " +"' k <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 2 -e:1) v:{ ->3 3 <;> nextstate(main 2 -e:1) v:{ ->4 j <2> leaveloop vK/2 ->k a <{> enteriter(next->g last->j redo->b) lK/8 ->h - <0> ex-pushmark s ->4 - <1> ex-list lKM ->9 4 <0> pushmark sM ->5 5 <0> undef s ->6 6 <$> const[IV 0] sM ->7 7 <$> const[PV "0"] sM ->8 8 <$> const[PV ""] sM ->9 9 <#> gv[*_] s ->a - <1> null vK/1 ->j i <|> and(other->b) vK/1 ->j h <0> iter s ->i - <@> lineseq vK ->- f <@> print vK ->g b <0> pushmark s ->c e <1> entersub[t2] lKS/TARG,1 ->f - <1> ex-list lK ->e c <0> pushmark s ->d - <1> ex-rv2cv sK/128 ->- d <#> gv[*a] s ->e g <0> unstack v ->h -e syntax OK
      Conclusion: $_ and 1 and 1 if $_ is exactly the same thing. perl compiles it to the same bytecode.
      []s, HTH, Massa (κς,πμ,πλ)

        Indeed. And

        if (c()) { f(); g() }
        is the same as
        c() and do { f(); g() };
        $ diff -u \ > <( perl -MO=Concise,-exec -e' if (c()) { f(); g() } ' 2>&1 ) \ > <( perl -MO=Concise,-exec -e' c() and do { f(); g() } ' 2>&1 ) --- /dev/fd/63 2009-07-07 12:25:27.614985473 -0400 +++ /dev/fd/62 2009-07-07 12:25:27.598984321 -0400 @@ -1,6 +1,6 @@ -e syntax OK 1 <0> enter -2 <;> nextstate(main 3 -e:1) v +2 <;> nextstate(main 2 -e:1) v 3 <0> pushmark s 4 <#> gv[*c] s/EARLYCV 5 <1> entersub[t2] sKS/TARG,1
Re^3: return if 0
by zigdon (Deacon) on Jun 22, 2009 at 19:31 UTC

    Interesting! The last expression evaluated is the string "0". That's false, so the return doesn't fire, but the "0" is actually returned.

    Cool example, thanks!

    -- zigdon

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2019-06-19 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (83 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!