http://qs321.pair.com?node_id=245302


in reply to Loading a function from another file dynamically?!

Using a module, you can run a function defined in that module. The module must be loaded at compile time.

However, Perl allows you to define subs in other files and load them in several ways. I'll show you some.

Here's the main file

#!/usr/bin/perl -w use strict; my $method = shift || 1; my $filename = shift || 'other.pl'; my $sub = shift || 'hello'; if ($method == 1) { print "First method\n"; { local $/; open OTHER, "< $filename" or die "can't open\n"; my $other = <OTHER>; close OTHER; eval $other . ";$sub"; } } elsif ($method == 2) { print "Second method\n"; scalar eval `cat $filename`; eval $sub; } else { print "Third method\n"; do $filename; eval $sub; }

In this file we define one sub.

#cat other.pl sub hello { print "hello world\n"; }

And one more in this other file.

# cat other2.pl sub hi { print "hi, world!\n"; }

As an example, you can run this script

$ perl test_runtime.pl 1 other.pl hello

Where "1" is the method to use, "other.pl" is the file containing your function, "hello" is the function name.

Try also

$ perl test_runtime.pl 2 other2.pl hi $ perl test_runtime.pl 3 other2.pl hi

And see for yourself what happens.

CAVEAT. Using eval you are compiling and running code at run time. When you run a normal script, the Perl compiler will catch the mistakes and inform you about them. With eval, you should catch the errors by checking eval's return value. See the docs for more info.

_ _ _ _ (_|| | |(_|>< _|

Replies are listed 'Best First'.
Re: Re: Loading a function from another file dynamically?!
by chromatic (Archbishop) on Mar 24, 2003 at 00:06 UTC
    Using a module, you can run a function defined in that module. The module must be loaded at compile time.

    Partly true. More accurately, you can cause perl to compile things at run-time. The important bit is that it knows how to do the thing you're going to ask it to do when you ask to do it. require, in this case, can be quite useful.

Re: Re: Loading a function from another file dynamically?!
by Anonymous Monk on Mar 23, 2003 at 19:18 UTC
    thanks. that worked well :).. I think I'll just use number 3 since it's the shortest. eval() return values aren't that important to me, since it's only for my wwwsite. There aren't any real differences between these methods, are there?

      The first method loads the file into a string, then evals it.

      The second method uses an external program (cat) to read the file, taking up a bit more resources.

      The third method is the "preferred" way of doing it, but has a couple of differences: the file is looked for in all the directories listed in the @INC array; the code in the file will not be able to access lexical variables declared (with my) in the scope in which the do is executed. (for details, perldoc -f do).

      -- 
              dakkar - Mobilis in mobile