Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: conditional print. Is correct to use it?

by choroba (Cardinal)
on Oct 27, 2020 at 12:04 UTC ( [id://11123205]=note: print w/replies, xml ) Need Help??


in reply to conditional print. Is correct to use it?

What's the question?

print "My homework: $fo" ; # prints "My homework: " and exits

No, it doesn't exit. It continues running the program on the next line.

print 'My homework: ',($fo || 'Roses are red');

There are only few values considered false in Perl: an undefined value, empty string, 0, and the string "0". In this case, it's the empty string, so you see the alternative value.

$fo + 2 || 8
An empty string in numeric context is treated as zero. 0 + 2 is two which is true, so no need to substitute 8.

$fo + 0
See above. 0 + 0 = 0 and that's what we see.

$fo + 0 || 8
Again, an empty string in numeric context is 0, 0 + 0 is 0 which is false, so we see 8.

Modern Perl explains context in a nice and simple way: see Context.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: conditional print. Is correct to use it?
by BillKSmith (Monsignor) on Oct 27, 2020 at 15:49 UTC
    Your complete list of false values is one of those "well known" facts that I cannot find the documentation for. The list does appear in the second paragraph of the documentation of the function defined, but it does not claim to be Perl's definition of 'false'. Can you suggest a better reference.
    Bill
      See "Scalar values" in perldata.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Your complete list of false values is one of those "well known" facts that I cannot find the documentation for.

      There used to be a section in perlsyn, and I wrote the node Truth and Falsehood as a replacement.

        Thanks, I "sorta" remembered that it was in perlsyn and was surprised to not find it there. My search today of perldoc.perl failed to find the article in either perldata or perl5280delta. (searched for FALSE and for Boolean) A google search found an obsolete reference to the perlsyn article. I still do not know what I could have done different to find it myself.
        Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found