Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Easy Reference for Contexts

by tchrist (Pilgrim)
on Apr 14, 2011 at 19:27 UTC ( [id://899514]=note: print w/replies, xml ) Need Help??


in reply to Re: Easy Reference for Contexts
in thread Easy Reference for Contexts

For instance I doubted that scalar m// returns true or false (those identifiers don't exist in Perl), I thought it's the number of matches returned.

But I was wrong: :)

DB<23> $_="aaa" DB<24> print scalar m/a/ 1
Anything in Perl is true or false. Or rather, ANYTHING is true but undef is false. 😋

Like the relops, m//, s///, and y/// can all return the magical false value, that secret special value that nobody talks about:

% perl -wle 'printf "got %d matches\n", scalar ANYTHING =~ /nothing/'
got 0 matches
So I shan’t, either. 😜

Replies are listed 'Best First'.
Re^3: Easy Reference for Contexts
by LanX (Saint) on Apr 15, 2011 at 10:11 UTC
    > Or rather, ANYTHING is true but undef is false.

    Many things in Perl are evaluated to false in boolean context, not only undef.

    Cheers Rolf

      Tom is right. ANYTHING is true:
      $ perl -E 'say +ANYTHING ? "true" : "false"' true
        Ladys and gentleman, let me introduce the new "author context"!

        If you sense¹ a hidden guru identity behind a novice account, reread a post with amplified irony-factor ... ;-)

        Cheers Rolf

        1) just be careful if you see magical unicode "switches" in the text ...

        ---------------------------

        UPDATEs:

        DB<119> sub ANYTHING {} DB<120> print "false" unless ANYTHING false

        not feasible with undef.

        --------

        Funnily in Python it's easily possible to globally redefine False and True...

        > python Python 2.5.2 (r252:60911, Jan 20 2010, 23:16:55) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> False,True=True,False >>> False True >>> True False

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-25 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found