in reply to Strange "Useless use of constant" message: should not appear at all!
Within the if statement, @movement is evaluated in scalar context, and consequently gives a number that is the size of the array.
gives "north" as the answer.#!/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";
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
givesmy @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";
Example 2Useless 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>
#!/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
Gives#!/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";
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?
Gives#!/usr/bin/perl -w my @movement = (0,1); my $from = ""; if (@movement == (0,1)) {$from = "east"} print "\$from = <$from>\n";
$from = <>
|
|---|