develooper Front page | perl.perl5.porters | Postings from June 2015

Re: [perl #125436] Experimental refaliasing with local not restoringvalues when scope exited

Thread Previous
From:
Eric Brine
Date:
June 23, 2015 21:18
Subject:
Re: [perl #125436] Experimental refaliasing with local not restoringvalues when scope exited
Message ID:
CALJW-qEKFeVuazpLst5eQyiZU1nwD_NxAY9aetV=XxJr3GbTCw@mail.gmail.com
On Tue, Jun 23, 2015 at 7:46 AM, Dave Mitchell <davem@iabyn.com> wrote:

> On Thu, Jun 18, 2015 at 07:12:12PM -0700, Ken Cotterill wrote:
> > This bug relates to the experimental feature 'refaliasing' described in:
> >
> http://search.cpan.org/~rjbs/perl-5.22.0/pod/perlref.pod#Assigning_to_References
> >
> > My expectation was that using the "\local our" form would restore values
> when
> > the scope of the 'local' was exited.
> >
> > This wasn't the case and the observed behaviour was identical to using
> the "\our"
> > form.
>
> I don't think its a bug.
>

I agree.

`local our $x` is an attempt to approximate `my $x` using package variables.

If you change `local our @data` for `my @data` in the program, the
behaviour is the same, and you realize that it's the only behaviour that
makes sense.



#!/usr/bin/env perl

use 5.022;
use warnings;
use feature 'refaliasing';
no warnings 'experimental::refaliasing';

my @data = (1 .. 3);

say "Data before pop_sum: @data";
say "Sum of data: ", pop_sum(\@data);
say "Data after pop_sum: @data";
say '---';
say "Data before pop_sum_ref: @data";
say "Sum of data: ", pop_sum_ref(\@data);
say "Data after pop_sum_ref: @data";

sub pop_sum {
    my @data = @{+shift};

    my $total = 0;

    say "\tTotal: $total; Data: @data";

    while (1) {
        $total += pop @data;
        say "\tTotal: $total; Data: @data";
        last if $#data < 0;
    }

    return $total;
}


sub pop_sum_ref {
    \my @data = shift;

    my $total = 0;

    say "\tTotal: $total; Data: @data";

    while (1) {
        $total += pop @data;
        say "\tTotal: $total; Data: @data";
        last if $#data < 0;
    }

    return $total;
}

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About