I’m writing a program for others to use. One of the design specs is to use the Term::ReadLine::Gnu Perl library. Most of the users will not have this installed and I want to install it while the program is running.
So, when the user starts the program they do not have the library installed. My program will install it for them while they are using the program using the OS package manager.
This is how I’m checking for the module
require Term::ReadLine;
my $Readline_Support = 1;
eval { require Term::ReadLine::Gnu }
or $Readline_Support = 0;
I use the $Readline_Support variable to redirect the terminal, use the history file etc.
$OUT = $TERMINAL->OUT if $readline_installed;
if ($readline_installed)
{
# save every answer and default, good or not, to the history file
$TERMINAL->add_history($Ans);
$TERMINAL->append_history(1, HIST_FILE);
}
Unfortunately, I get this error when I try to use the history file:
Can’t locate object method “using_history” via package “Term::ReadLine::Stub” at ./msi.pl line 618, line 2.
line 618 is
$TERMINAL->using_history();
Which is the first use of the $TERMINAL object.
Has any one had experience with installing Perl modules while the script is running and then using the modules in that same script?
Ok… Thanks to Andy if the module is not installed this works
# I removed the require Term::ReadLine; here
my $Readline_Support = 1;
eval { require Term::ReadLine::Gnu }
or $Readline_Support = 0;
below in the code
if ($readline_installed)
{
# Required for the dynamic loading of Term::ReadLine::Gnu
require Term::ReadLine;
$TERMINAL = Term::ReadLine->new ('ProgramName')
if $Interactive or $Brief
}
Now, however, the check for the installed mod always fails, I think because
require Term::ReadLine::Gnu;
needs
require Term::ReadLine;
early in the code, like the old
require Term::ReadLine;
my $Readline_Support = 1;
eval { require Term::ReadLine::Gnu }
or $Readline_Support = 0;
I see in the code for
Term::ReadLinethat it determines which implementation it is going to use at load time, as opposed to whennewis called. So I would suggest the following sequence:Term::ReadLine::Gnujust like you are currently, but before loading anyReadLinemodulesTerm::ReadLine::Gnuif not presentrequire Term::ReadLineTerm::ReadLine->newThings are made more complicated because
Term::ReadLine::Gnuthrows an error on an attempt to load it directly withuseorrequire, so the straightforwardevaltest fails even when it’s installed. One way to deal with that is to parse$@, but I don’t like parsing diagnostic messages because there’s a risk they may change over time. Deleting from%INCdoesn’t seem great either, but should work as long as the error on a direct load ofTerm::ReadLine::Gnudoesn’t go away.