F
Frédéric Perrin
Hello,
I want to subclass Net::SSH::Expect, by adding a couple of fields to
it. Using "perldoc fields" as a guide, I did the following:
---------------- 8< ----------------
#!/usr/bin/perl
{
package essai;
use base 'Net::SSH::Expect';
use fields qw/ new_field /;
use Data:umper;
sub new {
my $class = shift;
my $self = fields::new($class);
$self->SUPER::new(host => "server.invalid",
password => "password");
print Dumper $self;
$self->run_ssh;
$self->{new_field} = "something";
return $self;
}
}
package main;
my $ssh = essai->new();
---------------- 8< ----------------
But when $self->run_ssh is called, the ->run_ssh method croaks with:
We see that the pseudo-hash has all its fields, including new_field,
but there is no data.
Looking at the Net::SSH::Expect->new method, I see that it doesn't
initiate the object in the same way as "perldoc fields" does. I fixed
that by patching the new method in the following way (indentation was
already broken):
# diff -C1 Expect.pm.orig Expect.pm
*** Expect.pm.orig 2010-04-21 09:44:29.000000000 +0200
--- Expect.pm 2010-04-21 09:46:34.000000000 +0200
***************
*** 25,29 ****
sub new {
! my $type = shift;
my %args = @_;
! my Net::SSH::Expect $self = fields::new(ref $type || $type);
--- 25,31 ----
sub new {
! my Net::SSH::Expect $self = shift;
my %args = @_;
! unless (ref $self) {
! $self = fields::new($self);
! }
Is this a genuine bug in Net::SSH::Expect, or am I doing something
wrong?
I want to subclass Net::SSH::Expect, by adding a couple of fields to
it. Using "perldoc fields" as a guide, I did the following:
---------------- 8< ----------------
#!/usr/bin/perl
{
package essai;
use base 'Net::SSH::Expect';
use fields qw/ new_field /;
use Data:umper;
sub new {
my $class = shift;
my $self = fields::new($class);
$self->SUPER::new(host => "server.invalid",
password => "password");
print Dumper $self;
$self->run_ssh;
$self->{new_field} = "something";
return $self;
}
}
package main;
my $ssh = essai->new();
---------------- 8< ----------------
But when $self->run_ssh is called, the ->run_ssh method croaks with:
croak(ILLEGAL_STATE . " field 'host' is not set.") unless $host;
We see that the pseudo-hash has all its fields, including new_field,
but there is no data.
Looking at the Net::SSH::Expect->new method, I see that it doesn't
initiate the object in the same way as "perldoc fields" does. I fixed
that by patching the new method in the following way (indentation was
already broken):
# diff -C1 Expect.pm.orig Expect.pm
*** Expect.pm.orig 2010-04-21 09:44:29.000000000 +0200
--- Expect.pm 2010-04-21 09:46:34.000000000 +0200
***************
*** 25,29 ****
sub new {
! my $type = shift;
my %args = @_;
! my Net::SSH::Expect $self = fields::new(ref $type || $type);
--- 25,31 ----
sub new {
! my Net::SSH::Expect $self = shift;
my %args = @_;
! unless (ref $self) {
! $self = fields::new($self);
! }
Is this a genuine bug in Net::SSH::Expect, or am I doing something
wrong?