in reply to Re: Type of arg 1 to shift must be array (not sort)
in thread Type of arg 1 to shift must be array (not sort)

Note that this is true with a number of other different perlfuncs that return as immutable lists rather than arrays... copying the de-referenced return and forcing it back into an array will cure most non-perlish headaches.

For example:
#!/usr/bin/perl use strict; use warnings; my @array = qw( a b c d e f ); # With sort.. my $sorted = shift( @{ [ sort( @array ) ] } ); # With grep my $grepped = shift( @{ [ grep{ /\w+/ } @array ] } ); # With map my $mapped = shift( @{ [ map{ $_ } @array ] } ); # As a stringified method call my $method = "@{ [ FooBar->new()->a_method() ] }"; print "$sorted\n$grepped\n$mapped\n$method\n"; package FooBar; use strict; use warnings; sub new { return bless {}, shift; } sub a_method{ return 'some string'; }
Yay for Perl!

---hA||ta----
print map{$_.' '}grep{/\w+/}@{[reverse(qw{Perl Code})]} or die while ( 'trying' );

Replies are listed 'Best First'.
Re^3: Type of arg 1 to shift must be array (not sort)
by merlyn (Sage) on Dec 16, 2005 at 01:00 UTC
    I don't know why you'd waste the time and space to array-ify the return value when a literal slice will work just as easily for those. Instead of:
    my $result = shift @{[some list function]};
    just write
    my $result = (some list function)[0];
    A whole lot easier on the CPU and the eyes. That's why I invented the literal slice (one of the few things I can lay direct claim to in Perl).

    Of course, if you're going to store it into a scalar like this, you can simplify it further:

    my ($result) = some list function;

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.