develooper Front page | perl.perl5.porters | Postings from April 2010

Re: what should 'local @tied' do?

Thread Previous | Thread Next
From:
Eric Brine
Date:
April 12, 2010 14:01
Subject:
Re: what should 'local @tied' do?
Message ID:
j2jf86994701004121400h2de0d5dbqd79c0341fb011ab3@mail.gmail.com
On Sun, Apr 11, 2010 at 3:46 PM, Vincent Pit <perl@profvince.com> wrote:

>
> > There are a couple of long-standing open tickets (from Michael Schwern)
> > to the effect that with tied arrays and hashes,
> >     local @tied
> > doesn't DWIM (RT #7938, #7939).
> >
> > The current behaviour of local @tied is to create a new array, store it
> > in the ARRAY slot of *tied, and then I<attach tie magic to the new array
> > too>. This makes the local essentially a noop, and gives the following
> > behaviour:
> >
>
> > My preferences in descending order are 2,1,3.
> >
>
> Just a couple of notes to add to the discussion :
> - localized tied scalars remains tied.
>

Which is quite annoying.

sub func {
    my ($fn) = @_;
    local $_;
    open(my $fh, '<', $fn) or die;
    while (<$fh>) {
        ...
    }
}

for ($foo, $bar) {
    func($_);
}

Do you see the bug in the above? Despite an attempt to privatize $_, the
while is loop is unknowingly calling magic some modules away because a
variable that's not even in the current function ($bar) happens to be tied.

my $_; avoids the common case of this problem.

Thread Previous | Thread Next


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