throop has asked for the wisdom of the Perl Monks concerning the following question:
I'm gradually refactoring a large code base (many dozens of files) that was all written in package::main. In it, %printSwitches is a data structure that all of the files use. I want to put each of the module-files into its own package. My idea was to put %printSwitches, (and several other variables) into a package, and export that package to the other files that use it.
In brief, what I'm trying to do is
1) Create %base::Vars::runSwitches
2) Export %base::Vars::runSwitches to the main package and set its values there.
3) Export %base::Vars::runSwitches to the safer::Indenture::facetsIn package, and access its values.
When I run this normally, the access loses in the facetsIn file - %safer::Indenture::facetsIn::runSwitches is an empty array. Putting in some trace printing shows that the values are correct in %main::runSwitches; they just aren't being set in the base::Vars or safer::Indenture::facetsIn packages.
Here's file (2) – The file whereami.pm sets $runSwitches{directories} to a value that depends on which machine the code is running on:package base::Vars; # Actually a bunch of others vars in here, but I'm simplifying. use vars qw( %runSwitches ); our @ISA = qw(Exporter); our @EXPORT = qw(%runSwitches);
In Step (3) I try to access the array in a third pacakge:package main; use base::Vars qw(%runSwitches); use vars qw(%runSwitches); my $hostname = `hostname`; chop($hostname); if($hostname eq 'ISL06){ $runSwitches{directories} = {stat_out => '/net/httpd/htdocs/projec +ts/reconciler', DRMetrics => '/net/DRMetrics' ...} elsif{ ....}{}
And finally, a script test/ipack.pl which runs them all:package safer::Indenture::facetsIn; use strict; use base::Vars qw(%runSwitches); use Carp; sub orionDirs{ print("safer::Indenture::facetsIn's directories: " , join(' ', sort keys %{$runSwitches{directories}}), "\n"); print("base::Vars::runSwitches' directories: ", join(' ', sort keys %{$base::Vars::runSwitches{directories}}), "\n"); print("main::runSwitches{directories}'s directories: " , join(' ', sort keys %{$main::runSwitches{directories}}), "\n"); croak unless $runSwitches{directories}{orion_reqs} ...}
When I run it normally it shows that the array never it from base::Var to main:package main; use strict; use base::whereami; use safer::Indenture::facetsIn qw(orionDirs); &orionDirs(); 1
But when I run it in the debugger all's well:perl -w test/ipack safer::Indenture::facetsIn::runSwitches's directories: base::Vars::runSwitches' directories: main::runSwitches's directories: DRMetrics base external_cgi_bin exter +nal_images external_ont external_rec knowledge ontology orion_reqs st +at_out ucfDir
perl -dw test/ipack.pl Loading DB routines from perl5db.pl version 1.33 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(test/ipack.pl:6): &orionDirs(); DB<1> c safer::Indenture::facetsIn's directories: DRMetrics base external_cgi_ +bin external_images external_ont external_rec knowledge ontology orio +n_reqs stat_out ucfDir base::Vars::runSwitches' directories: DRMetrics base external_cgi_bin +external_images external_ont external_rec knowledge ontology orion_re +qs stat_out ucfDir main::runSwitches's directories: DRMetrics base external_cgi_bin exter +nal_images external_ont external_rec knowledge ontology orion_reqs st +at_out ucfDir
And am I missing a tutorial that would have shown me a better way to debug this?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Code in package hell, deceive by debugger (T,IFI)
by tye (Sage) on Nov 23, 2011 at 17:44 UTC | |
by throop (Chaplain) on Nov 30, 2011 at 15:46 UTC | |
by tye (Sage) on Nov 30, 2011 at 19:28 UTC |