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?


In reply to Code in package hell, deceive by debugger by throop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.