Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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

#!/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.

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

In reply to Re: Loading a function from another file dynamically?! by gmax
in thread Loading a function from another file dynamically?! by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found