Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: why this works? quoted strings

by mrstlee (Beadle)
on Oct 25, 2011 at 16:07 UTC ( #933645=note: print w/ replies, xml ) Need Help??


in reply to why this works? quoted strings

${ 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


Comment on Re: why this works? quoted strings
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://933645]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2015-07-31 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls