in reply to Perl templating/macro creating using 'BEGIN'...
The reason your subroutines are not defined in your package 'MyPackage' is that your subroutines are being defined in package main, rather than in package MyPackage. Even though you use a reference to the subroutine main_package_vars that is defined in package MyPackage (i.e. package_vars3) the subroutine (main_package_vars) still runs in the context of package main and the subroutines it defines are defined in package main.
By using fully qualified names, you can create the subroutines in any package name space. One option is to use __PACKAGE__ to pass the name of your package to main_package_vars and use this to create fully qualified names.
You might also be interested to use Data::Dumper to inspect the namespace of your package.
Here is a modified version of one of your code samples that might help you understand what is happening:
#!/usr/bin/perl -w use strict; use feature ':5.10'; BEGIN { sub package_vars { my $package = shift; foreach(@_) { eval "sub ${package}::$_ { my \$p = shift; \$p->{$_} = \$_[0] if \@_; \$p->{$_}; }" } } } package MyPackage; use Data::Dumper; { main::package_vars( __PACKAGE__, qw(one two three) ); print Dumper(\%MyPackage::); sub new { my $package=shift; my $parms=$_[0]; my $this={}; foreach(%$parms) { $this->{$_}=$parms->{$_}; } bless $this, $package; } } package main; my $p=new MyPackage({three => 3,}); $p->two(1); printf "two=%d, three=%d\n",$p->two, $p->three;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl templating/macro creating using 'BEGIN'...
by perl-diddler (Chaplain) on Sep 27, 2010 at 20:16 UTC | |
by Corion (Patriarch) on Sep 27, 2010 at 20:19 UTC | |
by perl-diddler (Chaplain) on Sep 27, 2010 at 21:24 UTC |