in reply to How to access directly an element of an array / list returned by a function?

Turn it not into an anonymous array, but an anonymous list, and you have the standard Perl idiom:

my $name = ( File::Spec->splitpath($path) )->[2];

update: must have been the caffeine level dropping too low .. .no need for '->' in list.

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

  • Comment on Re: How to access directly an element of an array / list returned by a function?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to access directly an element of an array / list returned by a function?
by stevieb (Canon) on Aug 26, 2010 at 17:42 UTC

    That doesn't work with the dereferencing operator (->), since the return of File::Spec->splitpath is not a reference. This works:

    my $name = ( File::Spec->splitpath($path) )[2];
    Steve
      Sorry, could have sworn I tried that first. Many thanks!!
        Your code used the [] markers around your function to create an anonymous array reference, with that the -> dereference was a correct, if scenic, choice.

        Tom's solution used () for precedence to help the parser understand that the list returned from the function is what needs slicing. Unfortunately he kept the -> which was incorrect.