Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
"be consistent"
 
PerlMonks  

Re^5: Can't Reference a Sub by Variable when using Strict

by broquaint (Abbot)
on Oct 04, 2004 at 08:49 UTC ( #396155=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Can't Reference a Sub by Variable when using Strict
in thread Can't Reference a Sub by Variable when using Strict

Try exporting symbols from one package to another like Exporter for example.
The code below demonstrates a very simple exporting mechanism within the confines of strict
use strict; use warnings; use warnings::register; use Symbol 'qualify_to_ref'; 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; } }
One might point out that the code in Symbol doesn't use strictures, but much of this could quite easily be done within its confines, however the code would necessarily be more verbose. If someone can find code that they believe can't be done within strictures then I shall quite happily rise to the occassion :)
HTH

_________
broquaint


Comment on Re^5: Can't Reference a Sub by Variable when using Strict
Download Code
Re^6: Can't Reference a Sub by Variable when using Strict
by tachyon (Chancellor) on Oct 04, 2004 at 12:24 UTC

    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

      No problems :) All I did was grok the symbol table for the name of the glob, whereas Symbol's qualify_to_ref simply uses perl's symbolic lookup
      use strict; { package Foo; 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 fun +ction my %global = map {$_ => 1} qw(ARGV ARGVOUT ENV INC SIG STDERR STDIN +STDOUT); 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 ($;$) { my @pkgs = split '::', qualify $_[0], @_ > 1 ? $_[1] : caller; my $tbl = \%main::; $tbl = $tbl->{"$_\::"} for @pkgs[0 .. $#pkgs - 1]; return \$tbl->{$pkgs[-1]}; } } Foo->import('foo'); foo(); __output__ Foo!
      HTH

      _________
      broquaint

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://396155]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2014-04-18 05:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (462 votes), past polls