Module to prevent eval'd code from using $&, $' and $`.
Use at your own risk; this will result in symbol table entries with the FAKE flag (which is supposed to only occur on non-symbol table globs).
Update: removed unused confess
Update: to clarify, this will prevent the performance degradation that occurs through use of $&,
$', and $` (so long as AmpKiller is use'd before code
that uses one of the variables is even compiled). It
also will croak if code actually tries to use
one of the variables at run-time. If you do:
use AmpKiller;
if ("foo" =~ /bar/) { print $& }
no croak will be done, since $& didn't actually get used.
package AmpKiller;
# usage:
# eval $some_code_that_may_use_dollerampersand;
# die "Sorry, can't do that: $@" if $@ && $@=~/Use of .* disallowed/;
use strict;
use warnings;
use Carp 'croak';
sub TIESCALAR { my $var = $_[1]; bless \$var }
sub FETCH { croak "Use of ${$_[0]} disallowed" }
sub STORE { croak "Modification of a read-only value attempted" }
tie our $amp, __PACKAGE__, '$&';
tie our $backtick, __PACKAGE__, '$`';
tie our $tick, __PACKAGE__, '$\'';
$::{"&"} = *amp;
$::{"'"} = *tick;
$::{"`"} = *backtick;
1;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.