[ABE.pm] And another thing...

Ricardo SIGNES rjbs-perl-abe at lists.manxome.org
Mon May 22 04:40:14 PDT 2006


* "Faber J. Fedor" <faber at linuxnj.com> [2006-05-17T20:15:08]
> How do I pass a variable from the superclass to the subclass?  Nothing
> I've tried works.

I think your phrasing of the question here is confusing and/or confused.  I
think you mean this, but I might be wrong:

  How do I make a base class store data that is shared by its derived classes?


I would do something like this:

  package ParentClass;

  my $dbh;
  sub get_dbh {
    # If we've already gotten the connection, return it.
    return $dbh if defined $dbh;

    $dbh = DBI->connect(...);
    return $dbh;
  }

  package ChildClass;
  use base qw(ParentClass);

  sub do_stuff {
    my ($self, $arg) = @_;

    my $dbh = $self->get_dbh;
    ...
  }

Any child of ParentClass can call its own get_dbh and will be handed back a
connection created by the parent and shared for all children.  Is any part of
the above unclear?  It's early, so I'm not sure whether it should be confusing
or not! ;)

-- 
rjbs
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.pm.org/pipermail/abe-pm/attachments/20060522/d2634f4a/attachment-0001.bin 


More information about the ABE-pm mailing list