LanX has asked for the wisdom of the Perl Monks concerning the following question:
Is it possible to externally disable warnings within the scope of an already compiled code-block?
That is without manipulating $SIG{__WARN__}³ or to deparse and reevalute the code?
Maybe using some B magic?
I'm playing around to mimic the for LIST -> variables feature of Perl6¹, but with more syntactic sugar, than splice or natatime can provide.
One of my attempts looks like this and kinda works...
implementation:
model:
my @a=1..10; # update2 for (1..3) { from {@a} loop { set my ($a,$b,$c); print "(",$a,$b,$c,")"; # no warnings 'exiting'; # last; }; print "\n"; } __END__ (123)(456)(789)(10) (123)(456)(789)(10) (123)(456)(789)(10)
...but while using loop-control statements like last works (amazing²) it throws a warning which needs to be silenced.
I could refrain to adding new pseudo statements like xlast() but this break in orthogonality would confuse too much.
Manipulating the sig-handler adds to much overhead, using B::Deparse is somehow risky, cause it's not bug-free.
I think this is of general interest for those playing around with syntactic sugar. :)
Cheers Rolf
( addicted to the Perl Programming Language)
¹) from perlcabal
This can be extended to take more than one element at a time: Was: while (my($age, $sex, $location) = splice @whatever, 0, 3) + { ... } Now: for @whatever -> $age, $sex, $location { ... } (Except the for version does not destroy the array.)
²) ...and disturbing ... and scaring ... and Perl! ;-)
³) for completeness
$SIG{__WARN__}= sub { return if $_[0] =~ /^Exiting subroutine/; warn $_[0]; };
Put initialization of @a to the start to show that the from-loop reinitializes each time, even if broken with last.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Silencing specific warnings when executing coderef?
by tobyink (Canon) on Mar 23, 2014 at 14:23 UTC | |
by LanX (Saint) on Mar 23, 2014 at 15:16 UTC | |
by tye (Sage) on Mar 23, 2014 at 19:00 UTC | |
by LanX (Saint) on Mar 23, 2014 at 19:27 UTC | |
by tobyink (Canon) on Mar 23, 2014 at 21:29 UTC | |
by LanX (Saint) on Mar 23, 2014 at 22:18 UTC | |
by tobyink (Canon) on Mar 24, 2014 at 10:42 UTC | |
by LanX (Saint) on Mar 24, 2014 at 03:32 UTC | |
by tobyink (Canon) on Mar 24, 2014 at 12:50 UTC |