in reply to elegant array assignment

There is elegant, and there is correct.

If the arrayref has been blessed into another package you will run into trouble. In the general case, you should use the following:

my @arr = UNIVERSAL::isa($x,'ARRAY') ? @$x : ($x);

• another intruder with the mooring in the heart of the Perl

Replies are listed 'Best First'.
Re^2: elegant array assignment
by kyle (Abbot) on Feb 15, 2008 at 22:48 UTC

    It's not clear to me that the OP wants to treat a blessed array reference as an array reference and not as an object.

    There was a discussion of this kind of thing recently...um...here. The conclusion seemed to be that if you really want to assess arrayref-ness, wrap a dereference in an eval.

    if ( ! eval { @$x; 1 } ) { # not array ref (ish) } else { # can be used as array reference! }

    This will detect objects that have used overload to pretend to be an arrayref as well as any real arrayref that's been blessed. It also won't fall for any random reference that some joker did "bless $x, 'ARRAY'" on.

    Here again, however, I can't tell that this is what the OP wanted to find out.