http://www.perlmonks.org?node_id=36523

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (object-oriented programming)

I'm working on an object that is complex enough (using XML::Parser to initialize the object) that I broke the initialization into a separate function from the constructor. I've put the initialization function into a block with some package variables and private functions something like this:
sub new
{
  my $self = {};
  bless $self, shift;
  $self->_init();

  return $self;
}

{
  my(%registry,%regattrs)
  ...

  sub _parseStart
  sub _parseEnd
  ...

  sub _init()
  {
    $self = shift;

    parse file....

    $self = { 'modules' => { %registry },
              'site'    => $regattrs{site}
            };
  }

}
The problem is that upon returning from init back in the constructor, $self doesn't seem to be referring to anything anymore, even though i know it's been assigned properly (tested to see if the content was there before exiting _init - it was). Am i having scoping issues? This isn't the first time I've done something like this, but I had no difficulties last time. Thanks much, Stephen

Originally posted as a Categorized Question.