The concatenation operator takes a scalar on both sides.

This is correct as far as it goes, but the truth is more specific...

The most peculiar thing I can think of in all this is the fact that concatenation operator doesn't accept a list while variable interpolation into a string does. I know that "concatenating" lists is what join is for. But to a relative greenie like me, it seems inconsistant.

You're missing a small bit of the contextual richness that makes Perl so cool. The binary concatenation operator evaluates its left and right arguments in string context, which happens to be a particular variety of scalar context. You can see this difference for example if you concatenate $!

$! = rand(7); print("In numeric context it's ". ($!+0)." but in string context it's " . $! . $/);

The join function is a list operator, like print. It evaluates each argument in string context, but it takes those arguments as a flattened list. You can write your own function that does this:

sub listop { my @result; for (@_) { push @result, "".$_; } @result; }
#update: or, more concisely, sub listop{map{"".$_}@_}

You can pass this thing an array or list (or several arrays or lists), and it will return a list containing all the elements, with each one stringified. This is basically what join and print do, except that rather than returning that list they do something interesting with it.


$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

In reply to string context and list operators (was Re: Array in scalar context.) by jonadab
in thread Array in scalar context. by the_0ne

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.