Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Join and Concatenation

by roho (Canon)
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."

Replies are listed 'Best First'.
Re: Join and Concatenation
by toolic (Bishop) 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 (Bishop) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1053976]
Approved by toolic
and dust plays in a shaft of sunlight...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2017-09-25 01:45 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (275 votes). Check out past polls.