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

when I am writing code as follows, then "print @content ;" is printing all elements of array content.
open FILE, "C:/Users/Admin/Desktop/Books/read.txt" or die "Can't open +: $!\n"; @content = <FILE>; print @content; $last = pop(@content); print "\n".$last. "\n";
Now when I am writing following code
open FILE, "C:/Users/Admin/Desktop/Books/read.txt" or die "Can't open +: $!\n"; @content = <FILE>; print @content."\n\n"; $last = pop(@content); print "\n".$last. "\n";
"print @content;" prints number of elements within array content (ie scalar context).

Why is that ? Why is printing in array context in first case and scalar context in second case ?

Replies are listed 'Best First'.
Re: Printing array in scalar context
by NetWallah (Canon) on Aug 08, 2011 at 05:21 UTC
    The "." (Concatenation operator) imposes a scalar context.
    Some alternatives, to show the content of the array are:
    print "@content\n\n"; # Array is interpolated print @content, "\n\n"; # Comma maintains array context imposed by "pr +int"

                "XML is like violence: if it doesn't solve your problem, use more."

Re: Printing array in scalar context
by TomDLux (Vicar) on Aug 08, 2011 at 17:34 UTC

    If you do put the whole thing in a double-quoted string, the $" separator would apply between array elements, introducting a space at the beginning of each line, other than the first. You would need to localize it to be an empty string, if you wanted to avoid additional characters.

    { local $" = q{}; print "@content\n\n"; }

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.