On Fri, 24 Aug 2007 19:32:05 -0400 "Alex Brelsfoard" <[EMAIL PROTECTED]> wrote: 

AB> I would like to call a function that declares a few variables and then runs
AB> some XML::Twig processes which in turn access & update said original
AB> variables.
AB> But I'm getting an error from my twig functions when they try to access the
AB> before-defined variables.
...
AB> sub main {
AB>     my $catalog_timestamp;
AB>     my $objinfo_timestamp;
AB>     my %fields;
... calls get_catalog_timestamp ...
AB> }

AB> An example of the twig functions:

AB> sub get_catalog_timestamp {
AB>     my ($twig, $elt) = @_;

AB>     # get the PublishTimeStamp attribute
AB>     $catalog_timestamp = $elt->att('PublishTimestamp');

AB>     $twig->purge();
AB> }

Either set up a global settings hash outside main(), which every
function accesses, or have a global function that returns a hash inside
a closure, e.g.

{
 my %settings;
 sub settings { return \%settings; }
}

settings()->{yes} = 'no';

For settings, always try to consolidate them into a single holder (hash,
object, whatever) because then you can easily track usage and refactor
it as needed.  I usually do something a little more elaborate:

use constant CATALOG_TIMESTAMP => 'catalog_timestamp';
settings()->{CATALOG_TIMESTAMP()} = 'timestamp data';

to avoid typos in the hash key, which make very annoying bugs.  I
usually use the same hash with Getopt::Long to have a single place for
all program options.

HTH
Ted
 
_______________________________________________
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to