develooper Front page | perl.perl5.porters | Postings from January 2021

Re: local %SIG = (%SIG, $signame => ...) not working as expected?

Thread Previous | Thread Next
From:
Ricardo Signes
Date:
January 12, 2021 21:27
Subject:
Re: local %SIG = (%SIG, $signame => ...) not working as expected?
Message ID:
3d19cbe4-e4ad-4339-ace5-5eec71a2e14e@beta.fastmail.com
On Tue, Jan 12, 2021, at 4:11 PM, Eric Wong wrote:
> Uri Guttman <uri@stemsystems.com> wrote:
> > i wonder why he copies %ENV. he should be able to just do:
> > 
> >     local( $SIG{$signame} ) = blah ;
> > 
> > the is faster and doesn't have the odd number thing you mention
> 
> Laziness :)  I'm getting a hash(ref) of SIG overrides either as
> a return value from another sub.  My workaround is something
> like this:
> 
> my %sig = $self->some_callback;
> local $SIG{$_} = $sig->{$_} for keys %sig;

"something like" can cover a lot of ground here, but are you sure that your workaround isn't so close to this that, like this, it just doesn't work?

X for Y is equivalent to "for (Y) { X }" including the enclosing scope, which delimits the effect of local.

use v5.30.0;
use Data::Dumper;

our %hash = (a => 1, b => 1);

{
  local $hash{$_} = 2 for keys %hash;

  print Dumper(\%hash);
}

{
  local %hash = %hash;
  $hash{$_} = 2 for keys %hash;

  print Dumper(\%hash);
}

print Dumper(\%hash);

…which outputs…

$VAR1 = {
          'a' => 1,
          'b' => 1
        };
$VAR1 = {
          'a' => 2,
          'b' => 2
        };
$VAR1 = {
          'a' => 1,
          'b' => 1
        };

-- 
rjbs
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