[Chicago-talk] Perl 101 question

Pete Krawczyk mongers at bsod.net
Tue Feb 13 17:00:54 PST 2007


Subject: [Chicago-talk] Perl 101 question
From: Andy_Bach at wiwb.uscourts.gov
Date: Tue, 13 Feb 2007 15:44:53 -0600

}So are the parens just to 'bind' the index to the bare list?

 $ perl -MO=Deparse -e 'print "how"'
 print 'how';
 -e syntax OK
 
Consider what happens if we add parens:
 
 $ perl -MO=Deparse -e 'print ("how")'
 print "how";
 -e syntax OK
 
The parens are bound to the print() function, not to a list containing the
single element "how".  That, incidentally, is why you can't tack the [0]  
onto the end - you can't treat a function as a direct arrayref.
 
 $ perl -MO=Deparse -e "print (('how', 'price', 'hat') [0]);"
 print(('how', 'price', 'hat')[0]);
 -e syntax OK
 
Taking the index off your print gives:
 
 $ perl -MO=Deparse -e "print (('how', 'price', 'hat'));"
 print 'how', 'price', 'hat';
 -e syntax OK
 
So, as you can see, the first set is used by the function, and the second 
set creates a list, of which the first element is selected.
 
-Pete K
-- 
Pete Krawczyk
    Chicago Perl Mongers
    mongers at bsod dot net


More information about the Chicago-talk mailing list