in reply to difference between @array."\n" and @array "\n"

Update: Too early in the morning

The . operator is the string concatenation operator, so @array . "\n" stringifies @array and adds "\n" to the end, which returns scalar(@array) . "\n", i.e. the count of elements in @array and a "\n".

@array "\n" doesn't appear to be valid syntax, how are you using it?
See perlop for more information on Perl Operators.

Replies are listed 'Best First'.
Re^2: difference between @array."\n" and @array "\n"
by LanX (Saint) on Aug 27, 2009 at 21:38 UTC
    The . operator is the string concatenation operator, so @array . "\n" stringifies @array and adds "\n" to the end, which returns scalar(@array) . "\n", i.e. the count of elements in @array and a "\n".

    Not 100% correct ... :)

    "scalar(@array)"? Yes!

    "stringifies @array"? No!

    It's more like stringifies scalar(@a) ...

    DB<26> @a=1..3 DB<27> p scalar @a 3 DB<28> p "@a" 1 2 3 DB<29> p @a."\n" 3 DB<30> p "@a"."\n" ; # $" is separator for stringification 1 2 3 DB<31> print "@a\n" 1 2 3 DB<32> p @a,"\n" ; # $, is separator for print args 123 DB<33> p "@a","\n" 1 2 3

    Cheers Rolf

    UPDATE: From perlop#Operator Precedence and Associativity:

    "With very few exceptions, these¹ all operate on scalar values only, not array values."

    (1) = perl operators

      "stringifies @array"? No!

      It stringifies the result of @array.

      To be fair, "stringifies @array" could mean "stringifies the result of @array", but most people think of "@array" as a variable rather than code. Clarity is definitely lacking.

      The concat operator evaluates its operands in scalar context, to which @a responds by returning the number 3. The concat operator then stringifies that value.

        Stringification is defined as

        The process of producing a "string" representation of an abstract object.

        and "3" is for sure no representation of @array.

        The important point to learn is that most operators enforce scalar context of the operands, IMHO the stringification of +3 to "3" is in this case of small importance ...

        Just want to clarify the terminology, before people start to think that stringification enforces scalar context!

        Cheers Rolf