Your code is not a functioning programming.
Within the if statement, @movement is evaluated in scalar context, and consequently gives a number that is the size of the array.
#!/usr/bin/perl -w
my @movement = (0,1);
my $from = "";
if (@movement == 2) {$from = "north"}
elsif (@movement == (1, 0)) {$from = "east"}
elsif (@movement == (0, -1)) {$from = "south"}
# elsif (@movement == (-1, 0)) {$from = "west"}
print "\$from = <$from>\n";
gives "north" as the answer.
Similarly, the list, (not array) of (0,1) returns the last thing in the list.
The 'useless use of number in void context' happens when numbers in the list (not the last one) are thrown away. For some reason this doesn't happen if the number thrown away is a 'zero'.
Example 1
my @movement = (0,1);
my $from = "";
if (5 == (3,4,5)) {$from = "north"}
elsif (@movement == (1, 0)) {$from = "east"}
elsif (@movement == (0, -1)) {$from = "south"}
# elsif (@movement == (-1, 0)) {$from = "west"}
print "\$from = <$from>\n";
gives
Useless use of a constant in void context at ./t.pl line 4.
Useless use of a constant in void context at ./t.pl line 4.
$from = <north>
Example 2
#!/usr/bin/perl -w
my @movement = (0,1);
my $from = "";
if (5 == (0,0,5)) {$from = "north"}
elsif (@movement == (1, 0)) {$from = "east"}
elsif (@movement == (0, -1)) {$from = "south"}
# elsif (@movement == (-1, 0)) {$from = "west"}
print "\$from = <$from>\n";
$from = <north>
I'm not quite sure why the
(@movement == (1,0))
doesn't give a warning, but the
(@movement == (-1,0)) does.
Example 3
#!/usr/bin/perl -w
my @movement = (0,1);
my $from = "";
if (@movement == (1, 0)) {$from = "east"}
if (@movement == (-1, 0)) {$from = "west"}
print "\$from = <$from>\n";
Gives
Useless use of a constant in void context at ./t.pl line 5.
$from = <>
Sandy
UPDATE:
Looking at a previous post, I thought that I misunderstood how @movement would be read within the if statement.
Does it compare the last element of @movement with the list within the if block. This little test says no, unless I am missing somehting?
#!/usr/bin/perl -w
my @movement = (0,1);
my $from = "";
if (@movement == (0,1)) {$from = "east"}
print "\$from = <$from>\n";
Gives
$from = <>
|