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

Can you explain the result?

by PerlOnTheWay (Scribe)
on Dec 06, 2011 at 13:15 UTC ( #942011=perlquestion: print w/replies, xml ) Need Help??
PerlOnTheWay has asked for the wisdom of the Perl Monks concerning the following question:

sub func { my ($delimiter, $text) = @_; return 1 and 0; } use Data::Dumper; print Dumper(func());

guess what?

the result is $VAR1 = 1;

anyone knows the reason?

seems that Perl gives return the same priority as an ordinary sub,is this really a good design?

Replies are listed 'Best First'.
Re: Can you explain the result?
by choroba (Chancellor) on Dec 06, 2011 at 13:26 UTC
      I know and has lower priority than &&,but it doesn't explain this issue.
        It does. Try with print instead of return to see.
Re: Can you explain the result?
by RMGir (Prior) on Dec 06, 2011 at 13:20 UTC
    Sure. The result of your function is the value 1, and that's what you're passing to Dumper...

    Now, if you called

    print Dumper(\&func);
    on the function reference, you'd get a slightly more interesting result:
    $VAR1 = sub { "DUMMY" };
    I guess my version of Dumper isn't up to decompiling subs - I'm guessing that's expected.

    Mike
      shouldn't  1 and 0 be false/0?
        As choroba said, return 1&&0 is different from return 1 and 0.

        $ perl -MB::Deparse -e' sub f{return 1 and 0;}; sub g{return 1 && 0}; $deparse=B::Deparse->new(); print "f(): ",$deparse->coderef2text(\&f),"\n"; print "g(): ",$deparse->coderef2text(\&g),"\n";' f(): { 0 if return 1; } g(): { return 0; }

        Mike
Re: Can you explain the result?
by TJPride (Pilgrim) on Dec 06, 2011 at 16:33 UTC
    Rather than worrying about why Perl does this, it may be simpler to just do return (1 and 0);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://942011]
Approved by marto
help
Chatterbox?
[marto]: Sane Dots, chapter 13: ITIL never work :P
[marto]: sorry, bad pun of it'll :P
[Corion]: :-D
[Corion]: I think that ITIL is mostly to prevent people from doing harm to a system that basically runs unattended :)
[Corion]: Maybe you could reduce ITIL to a computer network, a man and a dog. The network runs the computations. The man is there to feed the dog. The dog is there to prevent the man from accessing the computers.
[marto]: from experience of this side of the fence, it's all about proving to people that you are ITIL compliant, then the client ignores logical processes anyway :P
[marto]: when you work on the outside, for a client, you can make them aware of the stupids, but they don' t always listen :/
[Corion]: Yeah, you can lead them to water, but it's illegal to drown them...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2017-01-24 10:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (203 votes). Check out past polls.