Re: Exporter variables get lost
The answer to the trick question is that the subroutine name in package Sub::First is misspelled (missing a "t"). However, I think I would provide the Apache::Log object as a parameter to new() rather than twiddling with package vars to try to have it magically updated by Exporter. For example: package Sub::First; use strict; # simple constructor expects classname, log object sub new { my($class,$ap_log) = @_; return bless {apache_log=>$ap_log}, $class; } sub do_something { $self->{apache_log}->notice("this will work"); } package Sub::Second; use strict; # another simple constructor expects classname, log object sub new { my($class,$ap_log) = @_; return bless {apache_log=>$ap_log}, $class; } sub do_something_else { $self->{apache_log}->notice("this will work, too"); } package Site; use strict; use Sub::First; use Sub::Second; sub handler { my $r = shift; # test values for conditions my $some_case = 'true'; my $some_other_case = 'yes'; if ( $some_case eq 'true' ) { my $sub_first = Sub::First->new($r->log); $sub_first->do_something(); if ( $some_other_case eq 'yes' ) { my $sub_second = Sub::Second->new($r->log); $sub_first->do_something(); $sub_second->do_something_else(); } } } # End Example Regards, Tim Tompkins -- Programmer http://www.arttoday.com/ http://www.rebelartist.com/ --
Re: Exporter variables get lost
Viljo Marrandi actually wrote: >my $r = shift; >use vars qw( $log ); >$log = $r->log(); > >if ( $some_case eq 'true' ) { > use Sub::First qw( $log ); I haven't tested, but this doesn't look as a good idea. I don't think your code does what you think it does (or what you think it should do.) Moreover I don't know what this actually does (or should do) precisely. Exporter essentially does this: *Site::log = \$Sub::First::log; and vars essentially does this: *Site::log = \$Site::log; And this symbol table manipulation occurs at compile-time, before your code is run (without scoping effecs). In other words don't do that. -- Rafael Garcia-Suarez
Exporter variables get lost
Hello, I'm working on mod_perl project which has many different modules. One is so-called 'main' modules which loads other when needed. Until now everything worked just fine, but now one module just says that he doesn't know the variable I've exported. And interesting is, that when I comment out either $sub_first->do_something(); or other three lines about Sub::Second then it works just fine. Error it get looks like this: Can't call method "notice" on an undefined value at /usr/local/lib/perl/5.6.1/Sub/First.pm line 25. First.pm and Second.pm use Export.pm exactly same way. So, it this some mod_perl specific feature to just eat up some variables, or what? Here are cut-down versions of my modules: Site.pm: - package Site; use strict; use Apache::Log; sub handler { my $r = shift; use vars qw( $log ); $log = $r->log(); if ( $some_case eq 'true' ) { use Sub::First qw( $log ); my $sub_first = new Sub::First; $sub_first->do_something(); if ( $some_other_case eq 'yes' ) { use Sub::Second qw( $log ); my $sub_second = new Sub::Second; # if I comment out $sub_first->do_something then it works... $sub_first->do_something(); $sub_second->do_something_else(); } } } 1; --- package Sub::First; use strict; use Apache::Log; BEGIN { use Exporter(); @Sub::First::ISA = qw( Exporter ); @Sub::First::EXPORT= qw(); @Sub::First::EXPORT_OK = qw( $log ); } use vars qw( $log ); sub new { my $class = $_[0]; my $objref = { }; bless ( $objref, $class ); return $objref; } sub do_somehing { $log->notice("Now this here doesn\'t work"); } 1; -- Rgds, Viljo