Thread Fragen zu OO Perl (rückgabewerte von funktionen) (4 answers)
Opened by f0 at 2010-12-13 19:02

f0
 2010-12-13 19:02
#143533 #143533
User since
2010-03-08
6 Artikel
BenutzerIn
[default_avatar]
Hallo zusammen,

ich versuche mich gerade an Perl OO und hab da ein paar Schwierigkeiten.


folgende klasse(package) hab ich:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package Test::Backup;

use strict;
use warnings;
use diagnostics;
use Data::Dumper;

use PAR 'module.par';
use Net::OpenSSH;
use Git::Repository;

use Expect;


sub new{

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

        my $self =  {
                _username => $args{username},
                _tftproot => $args{tftproot},
                _password => $args{password},
                _autoenable => $args{autoenable},
                _tftpserver => $args{tftpserver},
                _filename => $args{filename},
                _timeout => $args{timeout},
                _hostname => $args{hostname},
                _path     => $args{path},
                _type     => $args{type},
                _tftpuser => $args{tftpuser},
                _context => $args{config},
        };

        return bless($self,$class);

};


sub connect{

        my ($self) = @_;

        my $host = $self->{_hostname};
        my %opts        = (
        user     => $self->{_username},
        password => $self->{_password},
        master_opts =>
          [ -o => "StrictHostKeyChecking false", -o => "HashKnownHosts false" ],
    );
        my $ssh = Net::OpenSSH->new( $host, %opts );
        my ( $pty, $pid ) = $ssh->open2pty();       
        return $pty;
}

sub do_tftp_backup{

        my ($self,%args) = @_;

        my $tftpserver = $self->{_tftpserver};
        my $filename = $self->{_filename};
        my $path = $self->{_path};
        my $context = $self->{_context};

        my $cmd = "copy $context tftp://$tftpserver/$path/$filename";

        my $expect = Expect->init($pty);

        $expect->send($cmd);
        $expect->expect(
            $self->{_timeout},
            [
                qr/Address or name of remote host/sm,
                sub {

                    my $self = shift;
                    $self->send("\r");
                    $self->set_accum( $self->after() );
                    exp_continue;
                  }
            ],
            [
                qr/Destination filename/sm,
                sub {
                    my $self = shift;
                    $self->send("\r");
                    $self->set_accum( $self->after() );
                    exp_continue;
                  }
            ],
            [
                qr/bytes copied/sm,
                sub {
                    my $self=shift;                                                               $self->set_accum( $self->after() );
                    $self->soft_close();
                  }
            ],
            [
                qr/Error/sm,
                sub {
                    my $self = shift;
                    $self->soft_close();
                  }
            ],
        );
return;
}



und dazu das programm was das nutzt

Code (perl): (dl )
1
2
3
4
use Test::Backup
my $test = Test::Backup->new(%tmp_config); (das erzeugen der config hab ich weggelassen das funktioniert)
$test->connect();
$test->do_tftp_backup();

Es funktioniert bis
Code: (dl )
$test->connect();



da möchte ich das $pty objekt zurückgeben um es in der nächsten Funktion zu benutzen.

Wie stelle ich das an?


Gruß Florian
Last edited: 2010-12-13 20:13:50 +0100 (CET)

View full thread Fragen zu OO Perl (rückgabewerte von funktionen)