Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Join and Concatenation

by roho (Monsignor)
on Sep 13, 2013 at 17:48 UTC ( #1053976=perlquestion: print w/ replies, xml ) Need Help??
roho has asked for the wisdom of the Perl Monks concerning the following question:

I found out (much to my surprise) that join MUST enclose its arguments in parens if it is used in a statement with concatenations. Is this normal behavior for join, or is this a bug? The sample code below shows the effect:

#!/usr/bin/perl use strict; use warnings; my @array = qw( a b c ); print "\nSimple join (without parens) - OK\n"; my $cols = join('],[',@array); print "cols = $cols\n"; print "\nConcatenation and join (without parens)- NOT OK\n"; $cols = '[' . join '],[',@array . ']'; print "cols = $cols\n"; print "\nConcatenation and join (with parens) - OK\n"; $cols = '[' . join('],[',@array) . ']'; print "cols = $cols\n";

"Its not how hard you work, its how much you get done."

Comment on Join and Concatenation
Download Code
Re: Join and Concatenation
by toolic (Chancellor) on Sep 13, 2013 at 18:03 UTC
    I don't think this is a join bug. I think it is a matter of operator precedence (see perlop). I think the concatenation operator is enforcing scalar context on the array (hence the 3).

    Tip #6 from the Basic debugging checklist (B::Deparse). Look how perl internally places parens:

    print "\nConcatenation and join (without parens)- NOT OK\n"; $cols = '[' . join('],[', @array . ']');

    If your goal is to enclose all elements of an array with square brackets, then join them with commas, I think a more natural approach uses map:

    $cols = join ',', map { "[$_]" } @array;
Re: Join and Concatenation
by LanX (Canon) on Sep 13, 2013 at 18:31 UTC
    it's b/c concat has higher operator precedence, using B::Deparse reveals it

    DB<234> dp $cols = '[' . join '],[',@array . ']'; { ($cols = ('[' . join('],[', (@array . ']')))); }

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: Join and Concatenation
by ricDeez (Scribe) on Sep 14, 2013 at 00:53 UTC

    As has already been stated this is related to operator precedence, with the parenthesis forcing order and returning the results you want. However you can avoid using join altogether if you modify the $" internal variable. In my opinion, this is cleaner and even obviates the need for an intermediate variable.

    This is what I am thinking:

    #!/usr/bin/perl use strict; use warnings; my @array = qw( a b c ); { local $" = '],['; print "[@array]\n"; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2014-07-12 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (241 votes), past polls