Thanks jwkrahn, but if they're interpreted to be the same, then why does this code:
while (split(/[, ]+/, <DATA>))
{ print @_ }
print nothing, while this code:
#while (split(/[, ]+/, <DATA>)) # Edit: Sorry - ignore this
while (@_ = split(/[, ]+/, <DATA>)) # Edit: I meant this
{ print @_ }
prints:
me@here.com
those@there.comothers@there.com
you@there.comthem@there.com
?
Update: Cancel the above. I see AnomalousMonk's answer to it now. | [reply] [d/l] [select] |
$ perl -e '@_ = qw{a bcd ef}; print @_'
abcdef
If you put the array in interpolating quotes,
you will see each element separated by the value of the special variable $"
(which, by default, is a space):
$ perl -e '@_ = qw{a bcd ef}; print "@_"'
a bcd ef
$ perl -e '@_ = qw{a bcd ef}; print qq{@_}'
a bcd ef
In rare situations, perhaps where $" was changed elsewhere in the code,
you may want to make a localised change to $" (typically within an anonymous block).
See local,
"Temporary Values via local()" and
"Localization of special variables"
for more details on that.
Some examples:
$ perl -e '@_ = qw{a bcd ef}; { local $" = " "; print "@_" }'
a bcd ef
$ perl -e '@_ = qw{a bcd ef}; { local $" = "__"; print "@_" }'
a__bcd__ef
$ perl -e '@_ = qw{a bcd ef}; { local $" = "\n"; print "@_" }'
a
bcd
ef
| [reply] [d/l] [select] |
If you just print an array (as in print @_) there will be nothing to show where each element starts and ends
Technically, there will be something: the value of the special variable $, -- it just happens that it defaults to undef (‡) so the separator used is nothing when printed. But if you change or localize its value to a different value, there will be a separator, without having to stringify the array first.
I wouldn't have mentioned this, but you went on to encourage the use of $" and interpolating the array in a string. If all you are doing is trying to get a separator for printing an array or other list (²), then $, will work without having to interpolate/stringify.
-- pryrt
‡: ... and apparently ignores use warnings 'uninitialized';: gotta love magic variables.
²: However, $, applies to everything when printing in list context, so local $,=',';print $a, @_, $b will have more separator instances than local $,=undef; local $"=','; print $a, "@_", $b; -- which may or may not be what you want.
³: Also, $, is much easier to use in a Windows cmd.exe one-liner than $". As a workaround, use English to the rescue:
C:\usr\local\share>perl -MEnglish -wle "@_ = qw{a bcd ef}; print qq(@_
+); local $LIST_SEPARATOR=','; print qq(@_)"
a bcd ef
a,bcd,ef
| [reply] [d/l] [select] |
Fair point, Ken, but I was just trying to concisely demonstrate that there was stuff-all in @_ with one bit of code, and the addresses in the other bit, so the 2 bits of code could not be equivalent.
| [reply] |