4

I am creating a child class having base class as Net::SSH2. when I am trying to add a class variable I am getting error saying -

Not a HASH reference at F:\temp\fooA.pl line 17.

If I do the same thing wihtout Net::SSH2 then it works fine.

Here is the code :

use strict;

my $x = Foo->new();

package Foo;

use base qw (Net::SSH2);

sub new {
    my ($class, %args) = @_;

    my $self = $class->SUPER::new(%args);
    $self->{'key'} = 'value';
    bless $self, $class;
    return $self;
}

1 Answer 1

5

It's simple: Net::SSH2 doesnt return a hash ref, but a blessed scalar:

use Scalar::Util qw(reftype);
print reftype($self) . "\n";  # SCALAR

BTW: It's always dangerous to rely on implementation details of third party code.

A possible solution would be to use inside out objects:

package Foo;

use Scalar::Util qw( refaddr );
use base qw( Net::SSH2 );

my %keys;

sub new {
    my ( $class, %args ) = @_;

    my $self = $class->SUPER::new ( %args );

    $keys{ refaddr ( $self ) } = 'value';

    bless $self, $class;
    return $self;
}
Sign up to request clarification or add additional context in comments.

1 Comment

... or just $self->{'super'} = $class->SUPER::new(%args)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.