VinsWorldcom has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to write my own child class of Net::SSH2, call it 'Net::SSH2::Mine'. I thought I had a pretty good grasp of OO-programming until I encountered the object Net::SSH2 produces - I think it's called 'inside-out'?
Essentially, I'm trying to open a Net::SSH2 connection but take in a bunch of other parameters specific to the device so I was just going to store them in the returned object, but the 'inside-out object' (and I hope I'm using the right terminology) is boggling my brain.
Some Google-ing helped me write the following example code which "works", but I don't know what I may be doing right / wrong / breaking / whatever.
#!perl use strict; use warnings; package Net::SSH2::Mine; use Net::SSH2; our @ISA = qw( Net::SSH2 ); my %NSM; sub new { my $class = shift; my $self = $class->SUPER::new(); $NSM{$self} = { prompt => '#', host => 'host' }; return bless $self, $class; } sub prompt { my $self = shift; return $NSM{[keys %NSM]->[0]}->{prompt} } sub host { my $self = shift; return $NSM{[keys %NSM]->[0]}->{host} } 1; package main; my $ssh = Net::SSH2::Mine->new(); use Data::Dumper; print Dumper \$ssh; print $ssh->prompt . "\n"; print $ssh->host . "\n"; exit;
And running (on Windows 7 x64 / Strawberry Perl 5.18.1 64-bit - Net::SSH2 0.51 comes in vendor\lib) produces:
VinsWorldcom@C:\Users\VinsWorldcom\tmp> test.pl $VAR1 = \bless( do{\(my $o = 5182840)}, 'Net::SSH2::Mine' ); # host
So many questions:
Lastly, in an earlier version of non-working code, I was getting AUTOLOAD and DESTROY errors when calling the accessors and 'exit' in main respectively. I did read that inside-out-objects require DESTROY, do I need to do something in my Net::SSH2::Mine (sub DESTROY) to facilitate this especially since I'm adding parameters to the object?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Child Net::SSH2 object trouble
by kcott (Archbishop) on Jun 29, 2015 at 19:45 UTC | |
by VinsWorldcom (Prior) on Jun 29, 2015 at 20:04 UTC | |
|
Re: Child Net::SSH2 object trouble
by salva (Canon) on Jun 30, 2015 at 06:48 UTC | |
|
Re: Child Net::SSH2 object trouble
by dasgar (Priest) on Jun 29, 2015 at 20:08 UTC | |
by VinsWorldcom (Prior) on Jun 29, 2015 at 20:30 UTC | |
by salva (Canon) on Jun 30, 2015 at 06:54 UTC |