I was wondering, do all of the modules I use in a perl script need to be at the beginning of the a script?
The reason I am wonder is I have a place in my script where it “splits” one way is the script connects with SSH using authorized keys. The other way is the script connects with SSH using a username and password. I am using the modules Net::OpenSSH to do SSH. According the cpan doc if I want all username and password authentication I need IO::Pty.
Well I wanted to only check the requirement of IO::Pty if the subroutine set to login with username and password is triggered. Is that possible? To load the module only if a certain subroutine is triggered?
The
use IO::Pty;line means the following to Perl:So first Perl loads the module with require (which will die if it can’t find the module), and then it calls the
->importmethod if it exists.You can turn this into a runtime conditional as follows:
Packages import into other packages, not into other scopes, so the import will be visible outside of the subroutine that performs it. It is probably best to skip the import all together and just use fully qualified names.