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 = <>

In reply to Re: Strange "Useless use of constant" message: should not appear at all! by Sandy
in thread Strange "Useless use of constant" message: should not appear at all! by muba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.