I'm trying to simplify a terribly complicated network inventory program by using objects, but I can't figure out how to do what I want to do.

Given a list of pingable IP addresses, I use async SNMP to query sysName, sysDesc, and sysObjectID. This part is easy.

But here's where I get stuck. Given the IP addresses that reply to the SNMP query, I want to create an SNMPDevice object. However, in the constructor of SMNPDevice, I want to do some additional queries to see if the device requires special handling (like a cisco switch stack where one IP address actually consists of multiple hardware units). If a special device type is detected, I want the SNMPDevice object to create an object of the appropriate derived class (like SNMPDevice::CiscoStack) and returning it instead of its own blessed self.

Something like this:

package SMNPDevice; sub new { my $class = shift; my $ip = shift; my $sysName = shift; my $sysDesc = shift; my $sysObjectID = shift; my @members = GetStackMembers($ip); return SNMPObject::CiscoStack::new($ip, $sysName, $sysDesc, $sysObj +ectID) if scalar @members > 1; return SNMPDevice::CiscoWLC::new($ip, $sysName, $sysDesc, $sysObjec +tID) if $sysDesc =~ /Cisco WLC/; #etc }
Or, would SNMPDevice bless itself as a SNMPDevice::CiscoStack ?

In reply to Can a class constructor create an instance of a derived class and return it? Or, can objects transmute into other objects? by poodad

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.