in reply to Disabling runtime warnings in dynamic scope?

Is it possible to disable a certain warning in the dynamic scope?

Which scope is dynamic here? All scopes here are determined at compile time.

but maybe there is a cleaner solution?

The most obvious clean solutions are

{ no warnings 'exiting'; switch [1,2,3] => sub { print "bla" ; next}, 3 => sub { print "bla2" } for (3); }

or

switch [1,2,3] => sub { print "bla" ; { no warnings 'exiting'; next } }, 3 => sub { print "bla2" } for (3);

or even

sub goNext { no warnings 'exiting'; next } switch [1,2,3] => sub { print "bla" ; goNext }, 3 => sub { print "bla2" } for (3);

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: Disabling runtime warnings in dynamic scope?
by LanX (Saint) on Apr 26, 2018 at 09:09 UTC
    > All scopes here are determined at compile time.

    Only lexical aka static scopes.

    Eg local has a dynamic scope .

    Check the definitions.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

      Only lexical aka static scopes.

      Eg local has a dynamic scope.

      <edit>
      Because of LanX answer below:

      Well, local has no scope at all - it just masks a (package) global at runtime until the localizing goes out of (lexical) scope, but that's just nitpicking at wording perhaps. See also my/local, space/time (was: Re: The difference between my and local).

      local has a static scope. Localized variables have a dynamic scope.
      </edit>

      But here you answered the question for yourself. The warning bits are compiled into each lexical scope at compile time, and a local $^W = $my_bits doesn't change them at runtime for scopes further down.

      So there are two possibilities:

      1. localize $SIG{__WARN__} to propagate a runtime behavior
      2. compile and eval code to be called with a current $^W at runtime
      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

        ... and a local $^W = $my_bits doesn't change them at runtime for scopes further down ...

        What do you mean? This works fine:

        use strict; my $lasthash = { lastfunc => sub { last ; }, } ; sub lastfunc { last ; } sub test { # local ($^W) = 1 ; # Activate this line to display the warnings while(1) { lastfunc } ; while(1) { $lasthash->{lastfunc}->(); } ; } test() ;