in reply to Re^5: Can't Reference a Sub by Variable when using Strict
in thread Can't Reference a Sub by Variable when using Strict
Using a module that does not use strict just proves the point. We can cut and paste the relevant functions from Symbol into your code and then guess what? So the question is can you rise to the occasion ;-)
package Foo; use strict; use warnings; use warnings::register; sub foo { print "Foo!\n" }; sub import { my $cur = shift; my $dest = caller; for my $g (grep $cur->can($_), @_) { warnings::warnif("sub '$g' already exists in $dest") and next if $dest->can($g); my($dest, $src) = map qualify_to_ref("$_\::$g"), $dest, $cur; *$dest = *$src; } } # This is what we need from Symbol to provide the qualify_to_ref funct +ion my %global = map {$_ => 1} qw(ARGV ARGVOUT ENV INC SIG STDERR STDIN ST +DOUT); sub qualify ($;$) { my ($name) = @_; if (!ref($name) && index($name, '::') == -1 && index($name, "'") = += -1) { my $pkg; # Global names: special character, "^x", or other. if ($name =~ /^([^a-z])|(\^[a-z])$/i || $global{$name}) { $pkg = "main"; } else { $pkg = (@_ > 1) ? $_[1] : caller; } $name = $pkg . "::" . $name; } $name; } sub qualify_to_ref ($;$) { return \*{ qualify $_[0], @_ > 1 ? $_[1] : caller }; } 1; __DATA__ Can't use string ("main::foo") as a symbol ref while "strict refs" in +use at c:/Foo.pm line 39. # test script #/usr/bin/perl use lib '/blah'; use Foo 'foo'; foo();
cheers
tachyon
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Can't Reference a Sub by Variable when using Strict
by broquaint (Abbot) on Oct 04, 2004 at 14:10 UTC |