in reply to Perl interpretor for conditional operator "?"

The first line sets the variable $a to contain the bareword 'a', which would be a big problem if you were using strict, but definitely not the only problem. ;) So $a will contain 'a'. The second line is similar.

The third and fourth lines are probably bugs, though they do compile. @x = qw(a,b,c); is really only assigning ONE value to the array @x. The value is the literal string, "a,b,c" (a single string). In other words, the script is probably using the qw// operator in a way that is visually misleading.

The fifth line takes @x in scalar context and prints the result. An array in scalar context divulges its number of elements. Since you only assigned one literal string to @x (not three distinct values), you see the number '1' get printed; only one element.

The sixth line checks to see if $a is less than $b. It should output the content of @x, which will appear as 'a,b,c' (giving the illusion of a list, though it's actually just a literal string in this case).

The eighth line is assigning the string held in the first (and only element) of @x to $a, and then it gets printed on the last line of the script.

It really looks to me like someone just wanted to be cute and send you on a goose chase trying to figure out why a list prints yet seems to be only one element. The fact is that there's only one element held in each array, and the singular elements are strings that have the appearance of lists.


Dave

Replies are listed 'Best First'.
Re^2: Perl interpretor for conditional operator "?"
by yramesh.1981 (Initiate) on May 22, 2011 at 17:49 UTC

    Hello Dave, Thanks for detailed explanation. "The sixth line checks to see if $a is less than $b. It should output the content of @x" I'm newbie, as per my understanding here after expression evaluation @x will be assigned to $var in scalar context and print the no of elements in the array, however it is printing content of array.

      You misunderstand. Perl makes a difference between @x and "@x".

      $count = @x;

      will assign the number of elements in @x to $count.

      $list = "@x";

      will assign string with all elements of @x separated by comma to $list.

      See perlop for the stringification/interpolation of arrays and the assignment operator.

        Hi Corion, Thank you for the explanation. This clears my understanding. Please ignore my previous update on this thread.