yramesh.1981 has asked for the wisdom of the Perl Monks concerning the following question:

$a=a; $b=b; @x= qw(a,b,c); @y=qw(c,d,e); {my$var=@x; print "$var\n";} # This prints 1} $var = $a lt $b ? "@x":"@y"; print "\n$var\n"; # This prints a,b,c $a=$b ? "@x" : "@y"; print "$a\n"; # This prints a,b,c

Hi, could you please explain what is happening here behind the scene.

Replies are listed 'Best First'.
Re: Perl interpretor for conditional operator "?"
by davido (Cardinal) on May 22, 2011 at 07:54 UTC

    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

      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.

Re: Perl interpretor for conditional operator "?"
by CountZero (Bishop) on May 22, 2011 at 09:47 UTC
    If you run this script with warnings turned on, you would have been given a "Possible attempt to separate words with commas at ..." warning for the @x= qw(a,b,c) and @y=qw(c,d,e) lines.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Perl interpretor for conditional operator "?"
by Anonymous Monk on May 22, 2011 at 07:20 UTC
    Hi, could you please explain what is happening here behind the scene.

    Did you write that code?

    Ask perl

    $ perl -MO=Deparse,-p pm906128.pl ($a = 'a'); ($b = 'b'); (@x = 'a,b,c'); (@y = 'c,d,e'); { (my $var = @x); print("$var\n"); } ($var = (($a lt $b) ? "@x" : "@y")); print("\n$var\n"); ($a = ($b ? "@x" : "@y")); print("$a\n"); pm906128.pl syntax OK
Re: Perl interpretor for conditional operator "?"
by Corion (Patriarch) on May 22, 2011 at 07:21 UTC

    What parts do you have problems with?

    For the operators, see perlop (or perldoc perlop for the locally installed version of the Perl documentation).

Re: Perl interpretor for conditional operator "?"
by ww (Archbishop) on May 22, 2011 at 13:40 UTC
    Your title leads me to believe you want an explanation of the question mark in line 6 and perhaps better identified as "?:."

    It's part of the ternary operator construct,
          expression ? resultA:resultB
    where, if the expression evaluates as true, the operator returns resultA or (where the colon is read as "or") resultB if the original statement is false.

    I never have figured out a way to make perldoc -f (any form of the operator or "perldoc -q (any variant)" cough up the explanation, but you can read about it any number of times here (Super Search on "ternary" [sometimes misspelled "trinary"]) or read about it in "Learning Perl" from O'Reilly.

Re: Perl interpretor for conditional operator "?"
by JavaFan (Canon) on May 22, 2011 at 13:17 UTC
    I don't easily say "use strict; use warnings;", because in most times it's said here on Perlmonks, it actually doesn't solve the problem being asked in anyway.

    This here is different. Turning on strict and warnings, and finding out what the messages mean really does help you understand what is happening.