in reply to calling a method within a constructor

Try something like this:
sub new { my $class = shift; my $device = shift; my $self = { 'device' => $device, }; bless $self, $class; $self->setInformation($device); return $self; }

There's nothing particularly magical about bless -- you can call it in the middle of the constructor with no ill effects, as long as you remember to explicitly return the object you created.

-- David Irving

Replies are listed 'Best First'.
Re^2: calling a method within a constructor
by ocs (Monk) on Apr 11, 2007 at 08:44 UTC
    Thank you, this works fine for me! (It isn't that nifty because you have to watch the order (what is seemingly a peculiarity of perl), but hey, it works!)

    tennis players have fuzzy balls.

      It's a peculiarity only if you want Perl to behave as other OO languages.

      OOP, if you listen to the purists, is about taking some data and some behavior and associating the two of them such that you can call behaviors on the data. That most OO language designers think programmers are too stupid to be entrusted with the mechanics of this isn't Perl's problem.

      As for constructors, there's nothing magical about new() at all. DBI uses connect() as the constructor - there is no new().


      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?