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

steve kolupaev skolupae at sonic.net
Thu Sep 15 00:58:20 PDT 2005


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.





More information about the Oakland mailing list