in reply to Resolving scalars in a scalar

There is a slick touch of confusion happening that's worthy of an obfu.

$ perl -e'($alpha, $delta) = qw{A D};$test = q($delta and $alpha); pri +nt eval $test' A$
$delta and $alpha are substituted all right, but the and operator gets evaluated, too. 'D' is true, so 'A' is evaluated and becomes the value of the expression.

I don't know of a way to get double interpolation that will survive this problem.

Update: Well, there is a way of course, but it's really crude, $test =~ s/\$(\w+}\b/$$1/g;

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Resolving scalars in a scalar
by SavannahLion (Pilgrim) on Oct 30, 2003 at 05:44 UTC
    To clarify, I wrote that original message. I guess I had my cookies off. :)

    The "$delta and $alpha" is supposed to be a string. I'm not sure I understand why 'and' would be evaluated as an operator in that case?

    And some one should fix the reference to and, it points to someone who took that name. Or did you intentionally mean for me to go to his profile? Unfortunately, it flew waaay over my head on that one :(

    I'm also still puzzled on just what 'q' does. I see it here and there, but I'm having trouble finding information about it.

    Is it fair to stick a link to my site here?

    Thanks for you patience.

      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.

        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.
Re: Re: Resolving scalars in a scalar
by hardburn (Abbot) on Oct 30, 2003 at 14:42 UTC

    I realize your regex wasn't meant to be used in a real example, but do realize that Perl will allow far more than \w chars in a variable. You can even get whitespace in there with symbolic refs:

    $ perl -MData::Dumper -e '$field = "foo bar"; *$field = 1; print Data: +:Dumper::Dumper(\%main::)' $VAR1 = { # Cut other variables in output 'foo bar' => *{'::1'}, };

    Of course, you could only access such a beast with another symbolic ref.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated