P is for Practical | |
PerlMonks |
Re: Loading a function from another file dynamically?!by gmax (Abbot) |
on Mar 23, 2003 at 19:07 UTC ( [id://245302]=note: print w/replies, xml ) | Need Help?? |
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
In this file we define one sub.
And one more in this other file.
As an example, you can run this script
Where "1" is the method to use, "other.pl" is the file containing your function, "hello" is the function name. Try also
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.
In Section
Seekers of Perl Wisdom
|
|