print (1 ? 1 : 0) . 'x'

Robin Barker Robin.Barker at npl.co.uk
Thu May 11 11:15:01 BST 2006


-----Original Message-----
From: O'Shaughnessy, Jamie [mailto:jamieosh at amazon.co.uk]
Sent: 11 May 2006 10:59
To: London.pm Perl M[ou]ngers
Subject: print (1 ? 1 : 0) . 'x' 



print (1 ? 1 : 0) . 'x';

What do you expect this would print?

I expected the output would be "1x" - the ?: evaluates to 1, the ()s are needed because . Is higher precedence than ?: so you end up with 1 . 'x' as the parameter to print - hence print "1x".

However it doesn't, it just prints "1".

Can anyone explain what is happening here?

Changing the code to:

print (1 ? 1 : 0), 'x';

----

Did you try -w !?

	% perl -lwe 'print (1?1:0)."x"'
	print (...) interpreted as function at -e line 1.
	Useless use of concatenation (.) or string in void context at -e line 1.
	1

As it says, print is interpreted as a function,
the result of print is then concatenated to "x" 
and the result thrown away! :-(

	print ((1 ? 1 : 0). "x");	
will work, as will
	print +(1 ? 1 : 0). "x";

Robin

-------------------------------------------------------------------
This e-mail and any attachments may contain confidential and/or
privileged material; it is for the intended addressee(s) only.
If you are not a named addressee, you must not use, retain or
disclose such information.

NPL Management Ltd cannot guarantee that the e-mail or any
attachments are free from viruses.

NPL Management Ltd. Registered in England and Wales. No: 2937881
Registered Office: Serco House, 16 Bartley Wood Business Park,
                   Hook, Hampshire, United Kingdom  RG27 9UY
-------------------------------------------------------------------



More information about the london.pm mailing list