I'd change the setInformation subroutine to

sub setInformation { my $self = shift; my $device = shift || $self->{device}; $self->{info} = _setInformation($device); } sub _setInformation { my $device = shift; my $model = `hdparm -i $device`; return $model =~ /model=([^,]+)/i; }

And the constructor to

sub new { my $class = shift; my $device = shift; my $self = { 'device' => $device, 'info' => _setInformation($device), }; bless $self, $class; }

This way, you're calling _setInformation as a normal subroutine, without having to use the (as yet uninstantiated) Disk object. By preceding the subroutine name with an underscore you're also indicating that this subroutine is for private use of your module alone and should not be called by outside code (this is a common convention in Perl modules). You might also want to call that subroutine something different to make differentiation with the method easier (e.g. _setInformationInternal or something) but that's purely an aethetic decision. The setInformation method remains for outside code to call after the Disk object has been instantiated with Disk->new() (if you don't think this is necessary you can leave the method out entirely).

One of the beauties of OO programming in Perl is that the language doesn't force you into the OO paradigm the entire time and allows for sensible little shortcuts like the above. If this doesn't appeal to you you could always just call your original setInformation method after blessing $self. There's nothing that forces you to return from the constructor immediately after blessing after all.

I also slightly changed the way you were parsing the hdparm output. Since you need a regex to obtain the model specification anyway I don't see the point of calling grep via the shell. And the regex I use is slightly more clear. IMO. YMMV.


All dogma is stupid.

In reply to Re: calling a method within a constructor by tirwhan
in thread calling a method within a constructor by ocs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.