in reply to conditional print. Is correct to use it?
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.
An empty string in numeric context is treated as zero. 0 + 2 is two which is true, so no need to substitute 8.$fo + 2 || 8
See above. 0 + 0 = 0 and that's what we see.$fo + 0
Again, an empty string in numeric context is 0, 0 + 0 is 0 which is false, so we see 8.$fo + 0 || 8
Modern Perl explains context in a nice and simple way: see Context.
|
|---|
| 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 | |
by choroba (Cardinal) on Oct 27, 2020 at 16:03 UTC | |
by haukex (Archbishop) on Oct 28, 2020 at 21:21 UTC | |
by BillKSmith (Monsignor) on Oct 30, 2020 at 14:24 UTC |