I noticed that most beginner texts will introduce and use print like this:
print $moose, $squirrel, $boris, "\n";
However, when I review code from CPAN, I often (typically) see:
print $bullwinkle . $rocky . $natasha . "\n";
As I recall, print is a list operator (and therefore the comma syntax is
used to separate items in a list), but is catenation somehow faster/more
memory efficient?
Thanks,
--Marc
Compared with the price of the IO operation the concatenation versus
passing several values is irrelevant. I do not dare to guess which
one is more efficient in what circumstances, but I don't think the
difference matters.
Jenda
===== Je...@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
I should note that there's a small difference in behaviour. Reading from
http://perldoc.perl.org/perlvar.html :
<<<<<<<<<<
#
# IO::Handle->output_field_separator EXPR
# $OUTPUT_FIELD_SEPARATOR
# $OFS
# $,
The output field separator for the print operator. If defined, this value is
printed between each of print's arguments. Default is undef. (Mnemonic: what
is printed when there is a "," in your print statement.)
>>>>>>>>>>
Of course, setting it to anything but the default is a sure fire way to break
practically most production code out there , and "Perl Best Practices"
recommends against setting it. But it may be useful in one-liners / small
scripts / golfs / obfuscations / etc.
Regards,
Shlomi Fish
> Jenda
> ===== Je...@Krynicky.cz === http://Jenda.Krynicky.cz =====
> When it comes to wine, women and song, wizards are allowed
> to get drunk and croon as much as they like.
> -- Terry Pratchett in Sourcery
>
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Why I Love Perl - http://shlom.in/joy-of-perl
Bzr is slower than Subversion in combination with Sourceforge.
( By: http://dazjorz.com/ )
If you're going to use it, localize it in a block:
{
local $, = ', ';
print $a, $comma, $separated, $list, "\n";
}
Do the same thing with the $LIST_SEPARATOR
{
local $" = ', ';
print "@some_array\n";
}
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
I like Perl; it's the only language where you can bless your
thingy.
Hello,
AFAIK there is no practical difference in efficiency.
The first is short for:
print join( $,, $moose, $squirrel, $boris, "\n" );
Where print() creates the string it outputs by joining the list elements
using the $, variable.
Also of note is that each element is in LIST context whereas in the
second example each element is in SCALAR context. This _could_ make a
difference, for example:
print "start ", localtime, " end\n";
Versus:
print "start " . localtime . " end\n";
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway