Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

why this works? quoted strings

by patcat88 (Deacon)
on Oct 25, 2011 at 12:23 UTC ( [id://933610]=perlquestion: print w/replies, xml ) Need Help??

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

#!/usr/bin/perl -w $ALIAS = 0; $arg = 'ST(0)'; $var = 'self'; $pname = 'MyMod::MyFunc'; $expr = '\n if (SvROK($arg) && SvTYPE(SvRV($arg))==SVt_PVHV) $var = (HV*)SvRV($arg); else Perl_croak(aTHX_ \"%s: %s is not a hash reference\", ${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]}, \"$var\");\n' ; $str = qq/"\\n$expr;\\n"/; $res = eval $str; 0;
Can someone explain why this works? All of it. Its from ParseXS. I also can't find any documentation on "${" operator.

edit: changed typemap string from double to single quotes so eval works

Replies are listed 'Best First'.
Re: why this works? quoted strings
by moritz (Cardinal) on Oct 25, 2011 at 12:30 UTC

    ${ ... } just interpolates the result from an expression, in this case ($ALIAS being 0) it is \"$pname\".

    With the example you gave, the eval fails, so I don't know what's the point of it.

      $... interpolates. Except for ${BAREWORD} (which means $BAREWORD), ${ ... } dereferences first. Just like outside of string literals.
Re: why this works? quoted strings
by mrstlee (Beadle) on Oct 25, 2011 at 16:07 UTC
    ${ isn't an operator itself - it has to be book-ended with a }. Basically you can put any legal perl inside the {}'s - so long as it evaluates to a reference. You then de-reference according to the type. It makes for some interesting strings:
    print "My scalar = ${\(my $scalar = 5)}\n"; print "My array = @{[(0..9)]}\n"; print "My array via scalar references passed to map = @{[ map {$$_} \( +0..9) ]}\n"; print "My array returned by anonymous subroutine = @{[do {(0..9)}]}\n" +; print "My array formatted with join = ${\(join '*',(0..9))}\n"; print "My hash treated as array = @{ [my %h = (0..9)] }\n";
    Prints:
    My scalar = 5 My array = 0 1 2 3 4 5 6 7 8 9 My array via scalar references passed to map = 0 1 2 3 4 5 6 7 8 9 My array returned by anonymous subroutine = 0 1 2 3 4 5 6 7 8 9 My array formatted with join = 0*1*2*3*4*5*6*7*8*9 My hash treated as array = 0 1 2 3 4 5 6 7 8 9

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://933610]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found