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:

  1. From the Data::Dumper output, is what I'm dealing with from Net::SSH2 an "inside-out object"?
  2. In my new(), should I be re-blessing to my class (Net::SSH2::Mine) - seems the only way to get the ->prompt and ->host accessors in main to work?
  3. Should I be storing parameters (e.g., prompt, host) a different way given this type of object?
  4. Any existing example code you could point me to?

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?


In reply to Child Net::SSH2 object trouble by VinsWorldcom

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.