To answer the questions you actually asked, in reverse order:
- Yes, this is the way to rebless into a new class.
- The way you've coded the "new" method, it calls _init using a method call on $self. It needs to be blessed for this to work. You could change this to a simple subroutine call: _init( $self ), and not bother with the first blessing.
What you've coded will work, and most of it is fine; but there are a couple of things I dislike about the class structure you've used:
- First, you have two _init methods, which do different things: one is a factory method; the other is a genuine instance initialisation.
- Second, your base class, ProcInfo, has two roles: first, it's a factory; second, it's a ProcInfo thingy.
You've said you have written a few of these; if you put the code from _init into a separate module, called, say, Factory, in a method called something like "makeOSSpecific", you can get reuse. The first parameter would be the base class name, so you'd call it from "new" something like:
return Factory::makeOSSpecific( "ProcInfo", $self ). There would be no need to include Factory in the class hierarchy, you'd just
require or
use it in ProcInfo.