Re: Tiny Perl puzzle
by tobyink (Canon) on Jun 05, 2014 at 22:19 UTC
|
I realised fairly quickly that the plus would be treated as a unary operator, and thus assumed the whole thing would be effectively:
print two( 0==0 ? "true" : "false" );
... and thus Perl would die because there's no function two() defined.
But how foolish of me!! The first two becomes a file handle of course, albeit an unopened one. So the whole line ends up printing to nowhere.
Good puzzle. This sort of thing is precisely the reason various best practices have arisen.
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
| [reply] [d/l] [select] |
Re: Tiny Perl puzzle (duh)
by tye (Sage) on Jun 06, 2014 at 00:22 UTC
|
This surprises any even moderately experienced Perl programmer?
Update: It appears that my test didn't sufficiently accurately replicate the original program text. Though I admit this particular quirk didn't and doesn't bother me much as I avoid barewords exactly because they lead to lots of ambiguities.
| [reply] |
Re: Tiny Perl puzzle
by LanX (Saint) on Jun 06, 2014 at 00:35 UTC
|
not sure about the precedence my first guess is "true" my second 1
Can't test ATM :)
Cheers Rolf
(addicted to the Perl Programming Language)
update
tobyinc's analysis is correct! :)
one more reason to enforce use strict and 'warnings'...
| [reply] [d/l] |
|
| [reply] [d/l] |
|
| [reply] [d/l] [select] |
|
|
|
|
Re: Tiny Perl puzzle
by sundialsvc4 (Abbot) on Jun 06, 2014 at 12:09 UTC
|
print (two + two == five)
? ( user == politician)
? "Re-elect me and I will fix it."
: "It's not a bug, it's a feature!"
: (user == accountant)
? "What do you -want- the answer to be?"
: "Don't worry, sonny, no child will be left behind.™"
;
| [reply] [d/l] |
|
| [reply] |
|
| [reply] |