Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Period vs. Comma concatenation and reference material for it

by chipmunk (Parson)
on Jan 23, 2002 at 09:10 UTC ( #140829=note: print w/replies, xml ) Need Help??


in reply to Re: Period vs. Comma concatenation and reference material for it
in thread Period vs. Comma concatenation and reference material for it

This doesn't answer your question on efficiency, but if you really like using the period, you could always force list context. print (@array) . "\n";

That won't quite do what you intend. First, parentheses around an expression do not force list context. Consider print ( (@array) . "\n");, which prints the size of the array, just as without any parentheses.

Parentheses do affect precedence, however, which leads to the second problem. The parentheses in your snippet cause perl to treat the print as a function call, with the concatenation applied to the result. This has the same effect as: (print(@array)) . "\n";
The contents of @array will be printed, print's return value will be concatenated with newline, and the resulting string will be discarded. With warnings enabled, your code would produce:

print (...) interpreted as function at - line 1. Useless use of concatenation in void context at - line 1.

Useful ways to print an array include:

print @array, "\n"; # joined with '' print "@array\n"; # joined with $", ' ' by default print join("\t", @array), "\n"; # joined with whatever you specify

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2020-01-20 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?