On Fri, Jun 21, 2013 at 02:15:39PM +0100, Gary Stainburn wrote:
> What your code did was to tag the "\n" onto the end of the
> array then pass the whole thing to join.

Gary is basically correct, but he worded it wrongly. When Perl
calls subroutines it basically flattens arguments into a "list".

Earlier, Mark Perry wrote:
> my @array = ( "boris", "natasha", "rocky", "bullwinkle");
> print join "\t", @array, "\n";

In this case, you're calling two subroutines: print and join. The
arguments are "\t", @array, and "\n". The contents of @array are
substituted in its place, so the arguments end up being "\t",
"boris", "natasha", "rocky", "bullwinkle", and "\n". Nothing
special is done to disambiguate which argument is for which
subroutine so Perl does the usual thing of handing them all to
the nearest subroutine: join.

In short, the "\n" is passed to join, so a tab character is
injected into the stream before the newline. The misleading thing
that Gary said was that the "\n" gets "tagged" onto the end of
the array, implying that the array is modified and then passed to
the join subroutine. Of course, the array is unmodified, so
really the "\n" gets "tagged" onto the end of the join argument
*list*. @array is unaffected, but the newline is delivered to the
wrong subroutine.

Using parenthesis as John specified solves the problem; or by
alternatively using say() instead of print(), which automatically
writes a newline after writing the arguments. say() is only
available in non-ancient perl's and only available when enabled
(typically by -E or use v5.010 [or whatever the first version is
to include it] or some equivalent pragma). When you want to write
"lines" that don't already contain a newline it is an ideal
shortcut though.

Regards,


-- 
Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bamccaig.com/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'

Attachment: signature.asc
Description: Digital signature

Reply via email to