Re: [Boston.pm] quick XML::Twig / variable scope problem

2007-08-25 Thread Ted Zlatanov
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


Re: [Boston.pm] quick XML::Twig / variable scope problem

2007-08-25 Thread Duane Bronson
No, don't use globals.  I'm pretty sure (read: too lazy to test) that 
you can send an anonymous subroutine like this:


my $localobject = new MyTwigClass($catalog_timestamp);
twig_handlers = {
'Catalog' = sub { return $localobjext-get_catalog_timestamp(); },
}


$localobject will not get garbage collected when it is out of scope 
because it's still referenced by the anonymous subroutine.




Jeremy Muhlich wrote:
 On Fri, 2007-08-24 at 19:32 -0400, Alex Brelsfoard wrote:

   
 sub main {
  my $catalog_timestamp;
 
 ...

   
 sub get_catalog_timestamp {
 
 ...
   
  $catalog_timestamp = $elt-att('PublishTimestamp');
 

   
 Global symbol $catalog_timestamp requires explicit package name
 

 Yep.  You can get away with accessing those lexicals in a function when
 your main code is at the top level in the script.  But when you have
 the variable declarations inside one function, the other function can't
 see them.

 I would do away with main, moving the code out into the top level.  If
 you *really* have your heart set on your main function (why???), you
 could move just the declarations of the global variables outside of
 it.


  -- Jeremy

  
 ___
 Boston-pm mailing list
 Boston-pm@mail.pm.org
 http://mail.pm.org/mailman/listinfo/boston-pm

   

-- 
Sincerely   *Duane Bronson*
[EMAIL PROTECTED] mailto:[EMAIL PROTECTED]
http://www.nerdlogic.com/
453 Washington St. #4A, Boston, MA 02111
617.515.2909

 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm