in reply to Re: looping through array
in thread looping through array

@array = qw(one two three); print @array, "\n"; print "@array\n"; print '@array\n'; __END__ onetwothree #array elements are fused. one two three #spaced and the "\n" replaced with a new line. @array\n #prints @array and "\n" as literals.


I understand first output as all the array elements are printed in a row.

I dont understand second output. When print command is used with array name in quotes, output has a space between elements. where does this space come from ?

In the third output, everything in single quotes is printed as literal. Does it mean that anything in single quotes is treated as literal and will be printed as is. Anything in double quotes will be treated as string value with variables getting its values from the code ?

Replies are listed 'Best First'.
Re^3: looping through array
by AnomalousMonk (Archbishop) on Jan 08, 2010 at 02:50 UTC
    I dont understand second output. When print command is used with array name in quotes, output has a space between elements. where does this space come from ?

    The Perl print built-in prints a list with the $, special variable interpolated between each item.

    Double-quoted strings (which have nothing directly to do with print) interpolate arrays with the $" special variable interpolated between each array element. See the documentation in perlvar for the default values of the  $, and  $" special variables.

    In the third output, everything in single quotes is printed as literal. Does it mean that anything in single quotes is treated as literal and will be printed as is.

    Yes. Whether printed or not, there is no interpolation in single-quoted strings. The  \ (backslash) character has limited effect as an escape under conditions explained in the docs on single-quoted strings linked by other respondents.

    Anything in double quotes will be treated as string value with variables getting its values from the code ?

    Yes and no. Double-quoted strings interpolate scalar and array variables as per the docs.

    >perl -wMstrict -le "my @array = ('one', qw(two three), qq{four}); print @array; $, = 'FOO'; print @array; print qq{@array}; $\" = 'BAR'; print qq{@array}; " onetwothreefour oneFOOtwoFOOthreeFOOfour one two three four oneBARtwoBARthreeBARfour
Re^3: looping through array
by manishrathi (Beadle) on Jan 08, 2010 at 00:16 UTC
    is anything within single quotes is always treated as literal in print statement ?
    anything in double quotes is treated as string with variables getting their values from the code ?
    whats the difference between when an array elements are declared in single quotes, double quotes and without quotes ? I tried out all the ways and I get the same result.
      is anything within single quotes is always treated as literal in print statement ? anything in double quotes is treated as string with variables getting their values from the code ?

      Asked and answered. See Re^3: looping through array.

      whats the difference between when an array elements are declared in single quotes, double quotes and without quotes ? I tried out all the ways and I get the same result.

      None. Strings can be defined using single-quotes, double-quotes and the qw operator (and probably in a few other ways that have slipped my mind). A list can be defined with strings and other things, and a list can be assigned to an array. Although not entirely unrelated, strings, lists and arrays are essentially independent.