On Apr 20, 2:11 pm, dery...@gmail.com ("C.DeRykus") wrote: > On Apr 20, 9:38 am, jimsgib...@gmail.com (Jim Gibson) wrote: > > ... > > >> 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..." >
I should clarify though that () isn't a list operator but just a grouping of n list items. The binary comma op marches through the items and tosses the first n-2 with the warning "useless use ...". The final two are consumed: the first is tossed away and then the final item is evaluated and then returned. That's why you get n-2 warnings (with the exceptions 0,1 that were mentioned). -- Charles DeRykus -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/