Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Code in package hell, deceive by debugger

by throop (Chaplain)
on Nov 23, 2011 at 16:50 UTC ( #939697=perlquestion: print w/ replies, xml ) Need Help??
throop has asked for the wisdom of the Perl Monks concerning the following question:

I'm stuck in package hell. Will some good monk pray for an intervention for me? My code works fine in the debugger, but loses when I run it normally.

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 the file for step (1) where I create the array:
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);
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 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{ ....}{}
In Step (3) I try to access the array in a third pacakge:
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} ...}
And finally, a script test/ipack.pl which runs them all:
package main; use strict; use base::whereami; use safer::Indenture::facetsIn qw(orionDirs); &orionDirs(); 1
When I run it normally it shows that the array never it from base::Var to main:
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
But when I run it in the debugger all's well:
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
What am I doing wrong? And why does it work OK in the debugger, but not outside of it?

And am I missing a tutorial that would have shown me a better way to debug this?

Comment on Code in package hell, deceive by debugger
Select or Download Code
Re: Code in package hell, deceive by debugger (T,IFI)
by tye (Cardinal) on Nov 23, 2011 at 17:44 UTC
    use base::Vars qw(%runSwitches); # use vars qw(%runSwitches);

    - tye        

      Thanks... but when I do that, I get fatal errors. E.g., with
      package test::facetsIn; use strict; use base::Vars qw(%runSwitches); use Carp; # use vars qw(%runSwitches); 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} } 1
      and
      perl -w test/facetsIn.pm Global symbol "%runSwitches" requires explicit package name at test/fa +cetsIn.pm line 8. Global symbol "%runSwitches" requires explicit package name at test/fa +cetsIn.pm line 16. Execution of test/facetsIn.pm aborted due to compilation errors.

        Then that means that your exporting code is broken:

        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);

        Since you hint that this isn't actually the code that you ran and had the problem with, it is risky for me to declare what the problem with it is. But there is certainly a potential problem.

        You don't actually load Exporter.pm. Add 'require Exporter;' or 'use Exporter;'.

        - tye        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-08-29 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls