in reply to Re: Re: Resolving scalars in a scalar
in thread Resolving scalars in a scalar

q{ } is the equivalent of a single quote - ' '.
qq{ } is the equivalent of a double quote - " ".
qx{ } is the equivalent of the back quote - ` `.
...

You should read the chapter "Quote and Quote-like Operators" on CPAN here.

The code
$text = eval { '$delta and $alpha' };
is equivalent to -
$text = ($delta and $alpha);
where and is a valid Perl operator. Because $delta is true (not empty), $alpha is evaluated, and $text gets assigned with the value of $alpha, which is 'A'. That's what Zaxo was talking about.

Replies are listed 'Best First'.
Re: Re: Re: Re: Resolving scalars in a scalar
by SavannahLion (Pilgrim) on Oct 30, 2003 at 08:29 UTC
    I drank too much to night so I might regret this one.
    Thanks for teh explanation on q, qq and qx. I understand what qw was used for, but I couldn't make the leap or connection to figure out what they meant on my own. I was sure I read a mention of it in the llama book, but I couldn't find it in the index.

    I understood the context that Zaxo was putting it in (sort of), but I still don't quite follow on why "and" would be treated as an operator. Is it because Zaxo is only wrapping what I want to be an evaluated string only once in quotes? So when it evaluates it, it umm... that's the part that loses me. If Zaxo wrapped my $delta and $alpha in quotes already, why did it treat and as an operator? Does eval strip the outer nesting of quotes? I found that if I re-write Zaxon's code like this;

    perl -e '($alpha, $delta) = qw{A D};$test = q(qq($delta and $alpha)); +print eval $test'
    then I get the behavior that I want.
      qw is short for quoted words. It builds a list. So the code ($alpha,$delta)=qw/A D/; means: build a list with two elements A and D, and assign value A to $alpha, and value D to $delta.

      In your example, $test=q{qq{$delta and $alpha}}; is equivalent to $test='"$delta and $alpha"'. Which means: give me a variable $test, and make the value equal to "$delta and $alpha" (with double quotes). When you evaluate this expression -
      print eval ' "$delta and $alpha" ';
      it is equivalent to the code -
      print "$delta and $alpha";
      Which will print "D and A" natually.

      eval evaluates the value held inside the string given, "$delta and $alpha", in this case, which returns a scalar string.

      In Zaxo's example, he was doing -
      print eval '$delta and $alpha';
      which is equivalent to -
      print $delta and $alpha;
      which will print the value of $alpha. Note that in the second case, there is no double quote in the equivalent code.