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

Could you explain what does it mean @{$rec->vals}.

@{$rec->vals}
can be written as
@{$rec->vals()}
or as
my $aref = $rec->vals();
@{$aref}

Is it array ref?

$rec->vals() is an expression that returns an array reference.
@{$rec->vals()} is an expression that returns an array lvalue (which means it can be used like a real array).

Update: Fixed problem noted by merlyn.

Replies are listed 'Best First'.
Re^4: Splitting array into two with a regex
by merlyn (Sage) on Nov 08, 2005 at 18:12 UTC
    @{$rec->vals()} is an expression that returns a list.
    Well, technically, it's an lvalue array expression, which when used as an rvalue in a list context, returns a list. When its used in a scalar context, it returns the length of the list. When it's used as an lvalue, it's an array.

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

      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:

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

        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.

        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?