walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:

I have a set of modules like this: Net::SSH::Admin, Net::SSH::Admin::AddAccount, Net::SSH::Admin::DelAccount, etc...

The Net::SSH::Admin instatiates a new ssh object (using expect), and connects to serverX. The other modules are pretty obvious. I am wondering, how would I call a subroutine in the Net::SSH::Admin::AddAccount module? This is my current setup, but I assume there is a better way...

# THIS IS MAIN SCRIPT #!/usr/bin/perl use warnings; use strict; use Net::SSH::Admin; my $server=$ARGV[0]; my $ssh = Net::SSH::Admin->new ( host => $server, password => $secret, user => 'myacct', timeout => 60 ); $login = $ssh->connect(); if ($login) $ssh->add_account(username=>"bob"); } # AND THIS IS HOW I AM CALLING THE FUNCTION FROM Net::SSH::Admin package Net::SSH::Admin; use warnings; use strict; use Expect; use Socket; use Carp; use fields qw( host user password port ssh_option raw_pty exp_internal exp_debug ti +meout log_stdout expect ostype prompt ); use Net::SSH::Admin::AddAccount; # I REMOVED sub new, sub connect, etc... sub add_account { my Net::SSH::Admin $self = shift; my %args = @_; my $os = $self->{ostype}; my $sub = $ssh->can("${os}_mod_account"); if ( !$sub ) { print "Don't know how to mod_account for $os\n"; } else { $sub->( $ssh, %args ); } }

Replies are listed 'Best First'.
Re: Calling subroutines from nested modules
by ikegami (Patriarch) on Aug 28, 2009 at 16:54 UTC

    I have a set of modules like this: Net::SSH::Admin, Net::SSH::Admin::AddAccount, Net::SSH::Admin::DelAccount, etc...

    The design is inherently problematic. We'll be able to help you better if you'd explain why your class split across so many files.

      I have it split across so many files because I have hundreds of functions and keeping them all in one file creates a huge mess of a module. I am doing this to keep them organized. I am doing this for 8 different flavors of UNIX, each often with their own output that needs to be interpreted.