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

In cshell you can use the command source to run another cshell script. All variables used in the sourced script are taken over into the Main script. What's the way to do this in Perl language. Pieter
  • Comment on How can I source other perl scripts from within a Perl script

Replies are listed 'Best First'.
Re: How can I source other perl scripts from within a Perl script
by goldclaw (Scribe) on Jan 23, 2001 at 15:56 UTC
    I think you'd like to have a look at do, require, use and eval. They all basically do the same thing - execute some code. Now the way they do it is a bit different.

    eval EXPR
    When passed a scalar containing some code, eval evaluates it in the current context. That can be very nice. Of cource it does this at runtime, so its a little slow(as the code in the scalar is compiled at runtime). It also traps any errors that might pop up.

    do FILE
    This is the old way of including files. It basically slurps in the file, and evaluates it. However, the code in the file, cannot see lexical variables in your program(and I guess that lexicals in the included file is not visible in the main code either) It does some nifty things though. It searches @INC directories for the file, and as oposed to the next two commands, it compiles the code each time it is include - which is something you might want if you are using the file for configuration.

    require FILE
    Same as do, except a file is never loaded/compiled twice, and any errors in the file will raise an exception(your program is likely to die...)

    use module list
    This is almost exactly the same as the following code:

    BEGIN { require module; import module list; }
    Since its inside a BEGIN block, the module is included before your program has started to execute, and so any errors will be caught before you actually start doing anything. Which is neat....
    I hope this helps.
    GoldClaw
Re: How can I source other perl scripts from within a Perl script
by davorg (Chancellor) on Jan 23, 2001 at 15:34 UTC

    If you want to run an external command (including another Perl script), then qx//, system, exec or fork may be what you want.

    If, however, you want to make use of variables and functions defined in another file, then you might find use, require or do are more interesting.</> --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: How can I source other perl scripts from within a Perl script
by azatoth (Curate) on Jan 23, 2001 at 15:28 UTC
    Well, you can search the PM site for words like "exec", "fork" and "system". For you to call other scripts, I would concentrate more closely on fork and exec.

    Happy Reading!

    UPDATE : See replies below, as I am limited in my knowledge, whereas people like davorg and arhuman can always be trusted :P

    Azatoth a.k.a Captain Whiplash

    Get YOUR PerlMonks Stagename here!
Re: How can I source other perl scripts from within a Perl script
by arhuman (Vicar) on Jan 23, 2001 at 15:33 UTC
    do would work fine too..
Related question: loading modules at run time
by dash2 (Hermit) on Jan 23, 2001 at 18:56 UTC
    I thought I'd add to this thread rather than start a new one.

    I'm writing a little script to check whois servers. One funky feature will be the ability to check for domains which don't have a whois server, only a web-based look up - like .tv domains. To do this, I have a wrapper function which chooses whether to do a normal whois or a web whois. The web whois uses the LWP module, like a good perl script.

    Now I don't want to load the LWP module - which is not small, right? - every time I run the script, but only when someone is looking up a .tv domain. Is there a simple way to "use" or "require" a module at run time, without getting compile time exceptions because Perl can't find methods and subroutines from the module within the code?

    I have a feeling AUTOLOAD has something to do with this... any deep wisdom would be gratefully received.

    Thanks

    David

      if ($we_want_to_use_lwp_simple) { require LWP::Simple; LWP::Simple->import; }
      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

      Undeclared functions at compile time is only a problem if you call them without paranteheses, that is
      my $result=function_one sub function_one{ #blah, blah }
      If you wish to use the functions in LWP that way, you will have to do something like this:
      eval q( use LWP::UserAgent qw(whatever); my $ua=LWP::UserAgent::new; );
      Ugh, ugly...probably better to try something like this:
      require LWP::UserAgent; import LWP::UserAgent(qw(whatever)); no strict subs; my $ua=LWP::UserAgent::New;
      If you can manage to put a pair of parantheses after each method call, this will do:
      require LWP::UserAgent; import LWP::UserAgent(qw(whatever)); my $ua=LWP::UserAgent::new(); #continue doing what you do with LWP...
      Notice that theres no need for a no strict subs when you tell perl that this is a function call.
      AUTOLOAD is used in packages for handling calls to routines within the package that are not defined in the package. One way to use this feature, is to do some custom error handling.
      In a program I'm writing at the moment, I use it to forward unknown method calls to a different object. Sort of a mix between is-a and has-a relationship.
      However, AUTOLOAD does not automatically load subroutines you need.
      GoldClaw
      The autouse module may do what you want, but unfortunatly the docs don't seem to go into sufficient detail on when the module is actually loaded into memory. But if it works like it seems like it should than it would be better than manually doing require and import or using a custom AUTOLOAD.