Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Comparison of the comma versus the period in print statements

0 views
Skip to first unread message

Marc Perry

unread,
Dec 22, 2009, 7:49:24 AM12/22/09
to begi...@perl.org
Hi,

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

Jenda Krynicky

unread,
Dec 22, 2009, 8:01:47 AM12/22/09
to begi...@perl.org
From: Marc Perry <marcpe...@gmail.com>

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

Shlomi Fish

unread,
Dec 22, 2009, 9:39:11 AM12/22/09
to begi...@perl.org, Jenda Krynicky
On Tuesday 22 Dec 2009 15:01:47 Jenda Krynicky wrote:
> From: Marc Perry <marcpe...@gmail.com>
>
> > 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?
>
> 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.
>

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/ )

Shawn H Corey

unread,
Dec 22, 2009, 9:59:45 AM12/22/09
to Shlomi Fish, begi...@perl.org, Jenda Krynicky
Shlomi Fish wrote:
> 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.

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.

John W. Krahn

unread,
Dec 22, 2009, 12:08:33 PM12/22/09
to Perl Beginners
Marc Perry wrote:
> Hi,

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

0 new messages