user113823
user113823

Reputation:

some stuff not printed

I would like to know the reason why "00" hasn't been printed:

my @compass_points = ('north', 'east', 'south', 'west');
my $direction = 'north-east';
unshift @compass_points, $direction;
# @compass_points contains: north-east, north, east, south and west

print ($west = pop @compass_points)."00"; 
print "11";
push @compass_points, $new_direction; # put $west back

print @compass_points;

Upvotes: 0

Views: 76

Answers (1)

choroba
choroba

Reputation: 241758

With warnings on, Perl would have told you.

It interprets the parentheses after print as surrounding the parameters to it, so the 00 is appended to the return value of print and thrown away, as the print appears in void context.

Either wrap all the parameters into a new pair of parentheses

print(($west = pop @compass_points) . '00');

or use + to tell the parser the parentheses introduce an expression:

print +($west = pop @compass_points) . '00';

You can also take advantage of print taking several parameters:

print $west = pop @compass_points, '00';

Upvotes: 5

Related Questions