开发者

Installing Term::ReadLine::Gnu while the Perl script is running and using the module in the script

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 fil开发者_如何学JAVAe 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;


Most of the users will not have this installed and I want to install it while the program is running.

You're swimming against the flow here. No one else does it this way, and changing a system after installation would also upset most system admins I know.

Simply declare the dependency, so when your program is installed, T::R::G is also installed. I link to the pertinent documentation in How do I create a build for a legacy system?.

The toolchain gives you already all the necessary bits to make this painless for everyone who is involved, do learn about it.


You can learn from the "cpan" command itself. cpan can install (upgrade) himself and reload all used modules afterward. That should be a good starting point for learning.


I see in the code for Term::ReadLine that it determines which implementation it is going to use at load time, as opposed to when new is called. So I would suggest the following sequence:

  1. test for availability of Term::ReadLine::Gnu just like you are currently, but before loading any ReadLine modules
  2. install Term::ReadLine::Gnu if not present
  3. require Term::ReadLine
  4. create your object with Term::ReadLine->new

Things are made more complicated because Term::ReadLine::Gnu throws an error on an attempt to load it directly with use or require, so the straightforward eval test 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 %INC doesn't seem great either, but should work as long as the error on a direct load of Term::ReadLine::Gnu doesn't go away.

my $readline_installed = 1;
unless (eval { require Term::ReadLine::Gnu; }) {
    # Term::ReadLine::Gnu throws an error that it can't be loaded directly,
    # even when it's installed.
    $readline_installed = exists ($INC{"Term/ReadLine/Gnu.pm"});

    # Needed so that it will be reloaded after installation
    delete $INC{"Term/ReadLine/Gnu.pm"};
}

unless ($readline_installed) {
    print "Installing Term::ReadLine::Gnu\n";
    # ...
}

require Term::ReadLine;

my $term = Term::ReadLine->new ("app");
$term->addhistory ("blah");
print $term->readline ("> "), "\n";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜