more useful options | |
PerlMonks |
Packages with subs using fully qualified class names, not always their own.by SimonSaysCake (Beadle) |
on Jun 30, 2017 at 19:54 UTC ( [id://1193951]=perlquestion: print w/replies, xml ) | Need Help?? |
SimonSaysCake has asked for the wisdom of the Perl Monks concerning the following question: I'm working on a helper package and was looking at the code of another that has a subroutine I want to use and noticed that some of this other package's subroutines included a fully qualified package name. Then I noticed that it had no init ("new") sub and that these qualified names were the "parent" package and that, via this method, the author was extending the functionality of the other "parent" package but without specifically sub-classing it. The package in question is Net::FTP::File and all of its subs use this format:
So when you include this Net::FTP::File package you can just do:
Which is pretty slick in a way; reminds me a little of how Javascript packages can be extended. I wondered how many other packages I had in my Strawberry install folder that behaved the same or similarly so I wrote a little File::Find-er util. I didn't find anything that worked quite like the Net::FTP::File package but did find others that had similarly named subroutines. One example is the Data::Dumper::HTML package which includes this:
Also of note is the Crypt::OpenSSL::X509 package which has a sub that uses its own fully qualified package name:
instead of just using:
for reasons I don't fully comprehend. My question is, what does the collective think of extending existing packages in this way? Is it better to just sub-class the parent (assuming that works) even if your "new" subroutine will essentially be a pass-through to SUPER::new? On that note, what about creating wrapper "child" classes that handle parent class method calls (not specifically overloaded) via an AUTOLOAD mechanism (I could see this being useful when sub-classing the parent doesn't work for some reason)? Best practice in this case?
Back to
Seekers of Perl Wisdom
|
|