Paul Kraus wrote: > From: Jenda Krynicky [mailto:[EMAIL PROTECTED] > > From: "Paul Kraus" <[EMAIL PROTECTED]> > > What's an easy way to grab just one element out of a function that > > returns a list. > > > > For instance if I was to stat a file and all I wanted was the $atime > > or if I just wanted the three timestamps atime mtime and ctime. > > Thanks. > > $atime = (stat($file))[8]; > > ($atime, $mtime, $ctime) = (stat($file))[8,9,10]; > > Ok I understand this but why the need for enclosing the function call in > (). >
You've been told that it's to force list context, which isn't strictly true. () = stat($file) forces list context, but () = stat($file)[8..10] doesn't work. The extra parentheses are there to avoid ambiguity (in practice, to resolve a syntax error). The subscription has equal priority to the 'stat' operator, so you could be meaning either the 8th, 9th and 10th elements of the call to 'stat' (which you do) or the result of 'stat' applied to the 8th, 9th and 10th elements of the list ($file) (which you don't). That would look like this () = stat(($file)[8,9,10]); and return an empty list. It's easier to think of with a list operator which returns a list: my @a = (reverse ('w', 'x', 'y', 'z'))[0,1]; is fine, and assigns ('z', 'y') my @a = reverse (('w', 'x', 'y', 'z')[0,1]); is also fine, and assigns ('x', 'w'). But my @a = reverse ('w', 'x', 'y', 'z')[0,1]; is ambiguous, and dies with a syntax error. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]