I can't seem to figure this one out, maybe someone out there can help a bit...
Within an
import function in a package I'm working on, one of the things I'm trying to accomplish is re-defining a named routine in the caller via a string
eval (don't ask ;) ). In doing so, and when
warnings are used relative to the calling package, I get the infamous '
redefine' warning. The question I have is: how do I turn this off within a calling package, but the calling package only?
I have tried supplementing the re-definition with
no warnings 'redefine'; from everywhere I can think of. The problem seems that because the
warnings module is so reliant on
caller() that I can't seem to trick it into turning off the '
redefine' warning in the calling package of my package - where it will always turn it off in my package. I've also tried removing stuff from
%INC, dropping the function from the symbol table of the calling package, all the way down to trying a source filter! Nothing seems to help.
I don't see this being such a hard thing and maybe I'm tired, but I still need help. Anything would be appreciated.
Example:
package MyPackage;
use warnings;
use strict;
sub import {
my ( $caller ) = caller;
my $cpackage = $caller;
$cpackage =~ s/::/\//g;
my ( undef, $function ) = ( shift, shift ); # I know, I know
#
# This is just an example. I know re-defining
# BEGIN in the caller has it's problems, but I'm
# trying to keep it simple for this snippet
#
{
no strict 'refs';
eval( qq^
sub ${caller}::BEGIN {
delete( $INC{'${cpackage}\.pm'} );
#
# I've tried the following, too:
# no warnings 'redefine';
# delete( %{ "${caller}::${function}" } );
# FILTER{}
# etc.
#
# I get the re-define warning here
require ${caller};
}
^);
}
}
Thanks!
---------
perl -le '$.=[qw(104 97 124 124 116 97)];*p=sub{[@{$_[0]},(45)x 2]};*d=sub{[(45)x 2,@{$_[0]}]};print map{chr}@{p(d($.))}'
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.