On Apr 20, 9:38 am, jimsgib...@gmail.com (Jim Gibson) wrote: > On 4/20/10 Tue Apr 20, 2010 9:25 AM, "Shawn H Corey" > <shawnhco...@gmail.com> scribbled: > > > srd wrote: > >> #!/usr/bin/env perl > >> use warnings; > >> use strict; > >> my $x= (1,2,3); > >> print $x,"\n"; > >> exit(0); > >> ************************************* > >> output: > >> Useless use of a constant in void context at ./try.plx line 4. > >> 3 > >> ************************************* > >> If we put $x=(1,2) then we get 2 without the error message. > > >> Can someone please explain why? > > > Yes, perl places the last item in the list into the variable. That's > > why the others are "useless". > > > Try: > > > my $x = ( 1, 2, 'this one' ); > > Yes, but as srd has observed, you get one fewer warning message than there > are "useless" items. Try: > > my $x = ( 1, 2 ); > > and you get no warnings. Try: > > my $x = ( 1, 2, 3, 4 ); > > and you get 2 warnings. One of those "useless" items isn't useless (or Perl > just doesn't care).
That makes sense since the () list op is right associative. The comma op is binary and would consume 3,4 without warning but 1,2 would then draw "useless use of a constant..." warnings. If, it'd been (1,$y,3,4) you'd get a "useless use of a variable..." as well as a "useless use of a constant..." -- Charles DeRykus -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/