NileshJ has asked for the wisdom of the Perl Monks concerning the following question:

Can someone tell me the difference between @array."\n" and @array "\n" i did not quite understand what does . do here. My understanding is just that . is used to match single characters in regex. Nilesh
  • Comment on difference between @array."\n" and @array "\n"

Replies are listed 'Best First'.
Re: difference between @array."\n" and @array "\n"
by james2vegas (Chaplain) on Aug 27, 2009 at 10:43 UTC
    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.
      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.

Re: difference between @array."\n" and @array "\n"
by ikegami (Patriarch) on Aug 27, 2009 at 14:42 UTC

    The concatenation operator (.) is documented in perlop. It evaluates its operands in scalar context, stringifies and concatenates what the operands returned, then returns the result of the concatenation. In scalar context, @array evaluates to the number of elements in the array.

    $ perl -we'my @a=qw( a b c ); my $x = @a . "\n"; print $x' | od -c 0000000 3 \n 0000002

    @array "\n" is not valid Perl. Below, I'll talk about @array, "\n" in case that's what you meant.

    $ perl -we'my @a=qw( a b c ); my $x = @a "\n";' String found where operator expected at -e line 1, near "@a "\n"" (Missing operator before "\n"?) syntax error at -e line 1, near "@a "\n"" Execution of -e aborted due to compilation errors.

    In scalar context, the list operator aka comma operator executes all but the last of its operands in turn (sometimes in void context, sometimes in scalar context) and discards what they return. It then executes that last operand and returns the result. In this case, this would mean that @array returns the number of elements it contains, that would get discarded, then it returns "\n".

    $ perl -we'my @a=qw( a b c ); my $x = (@a, "\n"); print $x' | od -c Useless use of private array in void context at -e line 1. 0000000 \n 0000001

    In list context, the list operator aka comma operator executes all of its operands in turn (in list context) and forms a list from the values they return. That list is then returned. In this case, this would mean that @array returns all of its elements. A value of "\n" is appended to that list, then the whole list is returned.

    $ perl -we'my @a=qw( a b c ); my @x = (@a, "\n"); print "[$_]" for @x' + | od -c 0000000 [ a ] [ b ] [ c ] [ \n ] 0000014

    My understanding is just that . is used to match single characters in regex.

    Regex patterns have their own syntax.