in reply to Re^4: Splitting array into two with a regex
in thread Splitting array into two with a regex

Can you back that up? I can't find any way of using it as an lvalue. Everything I tried gave me a count (in scalar context) or a list (in list context). Some tests:

use strict; use warnings; my @array = qw( a b c d e ); sub test { return @array; } (test())[0] = '!'; __END__ Can't modify list slice in scalar assignment at script.pl line 10, nea +r "'!';"
use strict; use warnings; my @array = qw( a b c d e ); sub test { return @array; } test() = qw( A B C D E ); __END__ Can't modify non-lvalue subroutine call in scalar assignment at script +.pl line 10, near "qw( A B C D E );"
use strict; use warnings; my @array = qw( a b c d e ); sub test { return @array; } my $aref = \(test()); @{$aref} = qw( A B C D E ); __END__ Not an ARRAY reference at script.pl line 11.
use strict; use warnings; my @array = qw( a b c d e ); sub test { return @array; } sub test2(@) { print(scalar @{$_[0]}, "\n"); } test2(test()); __END__ Can't use string ("a") as an ARRAY ref while "strict refs" in use at s +cript.pl line 11.

Are you talking about guts? I don't know anything about those. I was talking about the language.

Replies are listed 'Best First'.
Re^6: Splitting array into two with a regex
by merlyn (Sage) on Nov 08, 2005 at 19:49 UTC
    You're just looking at it sideways. {grin}

    The method needs to return an arrayref, which is then dereferenced by the @, and can thus be used in an lvalue context.

    my $rec = bless {}; # main is the package my @sideeffected = qw(a b c); sub vals { return \@sideeffected } @{$rec->vals()} = qw(d e f); print "$_\n" for @sideeffected;
    That prints d\ne\nf\n. Maybe you thought I was talking about the inner part? I was talking about the whole string of text from the "@" to the "}".

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

      ah yes! My eyes must have been crossed ;)
Re^6: Splitting array into two with a regex
by dragonchild (Archbishop) on Nov 08, 2005 at 20:00 UTC
    I use that structure in Tree so that the basic methods (add_child(), for instance) can modify the children attribute without knowing how the children attribute is set up. All that matters is that children() returns an arrayref that guarantees it will be modifying the children (however the subclass set them up). So, there's a lot of splice @{$self->children}, $idx, 0, $new_node; stuff going on.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?