Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

having difficulty getting $self back from an _init() function

by Anonymous Monk
on Oct 13, 2000 at 02:07 UTC ( [id://36523]=perlquestion: print w/replies, xml ) Need Help??

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.

  • Comment on having difficulty getting $self back from an _init() function

Replies are listed 'Best First'.
Re: having difficulty getting $self back from an _init() function
by Fastolfe (Vicar) on Oct 13, 2000 at 02:10 UTC
    In your init function, you are changing $self, creating a new hash reference. This destroys the associations it's had before. If you want to add some items to $self, set them individually:
    sub _init { ... $self->{modules} = { %registry }; $self->{site} = $regattrs{site}; ... }
Re: having difficulty getting $self back from an _init() function
by Anonymous Monk on Oct 13, 2000 at 08:04 UTC
    Thanks. It works fine now. -Stephen

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found