[oak perl] Applying shift() to an array reference

George Woolley george at metaart.org
Thu Sep 15 08:57:48 PDT 2005


Hi Steve,
An interesting question.

My first thought is that's what I'd expect.

What result are they expecting?
George

On Thursday 15 September 2005 12:58 am, steve kolupaev wrote:
> Hi Everyone,
>
> An odd Perl question came up today on
> my contract job in Washington state.
>
> When we call the shift function on an array
> we get back the first element of the array,
> and the array size drops by one.
>
> When we apply the shift function to a reference to the array, the same
> thing happens.
>
>     @AR = ( 1  .. 10 );    # create array of 10 scalars
>
>     shift @AR ;            # the array is down to 9 scalars
>
>     $ar = \@AR;            # create a reference to the array
>
>     shift @{$ar};          # the array is down to 8 scalars
>     shift @$ar;            # the array is down to 7 scalars
>
>     $AR[0] == 4 is true    # the first element of @AR is now 4
>
>     $ar->[0] == 4 is true  # testing @AR through the reference $ar
>                            # gives the same result, of course.
>
> Members of the project from a non-Perl background find this surprising.
>     But it is consistent with the current perlref.pod document and
> matches my experience.   Nothing protects the referent from a
> properly expressed operation on its reference.
>
> Is this behavior common with to all major ports of Perl?
>
> Steve Kolupaev
> Everett, Washington.
>
>
>
> _______________________________________________
> Oakland mailing list
> Oakland at pm.org
> http://mail.pm.org/mailman/listinfo/oakland



More information about the Oakland mailing list