what's the meaning of @{[pop @a]} ?

J

jck11

Can somebody explain it for me why the resulte of the code is 'ef'.
I just don't understand the @{[pop @a]} mean.
@a=('a', 'b' , 'cd' , 'ef');
print @{[pop @a]}, "\n";
 
P

Peter J. Holzer

Can somebody explain it for me why the resulte of the code is 'ef'.
I just don't understand the @{[pop @a]} mean.
@a=('a', 'b' , 'cd' , 'ef');
print @{[pop @a]}, "\n";

If you don't understand a complex idiom in a programming language it
usually helps to break it into little parts (reading from the inside
out):

pop @a you should understand. If not, read perldoc -f pop.

[ ... ] constructs an anonymous array and returns a reference to it.

@{ ... } dereferences a reference to an array. (both are explained in
perlref and perlreftut)

So in this case @{[ ... ]} is rather superfluous, since you end up with
a copy of the list you started with, giving you the same output as if you
had written

print pop @a, "\n";

in the first place.

The @{[ ... ]} construct can be used to embed arbitrary perl expressions
into a double-quoted string:

print "the last element was @{[pop @a]} but now it isn't any more\n";

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top