Example:
Create a file called foo.pm in your script directory containing:
package foo;sub bar {
my ($firstname, $surname) = @_;
print "$firstname $surname\n";
}
1;
The external code is called from your main script by telling Perl once at the start of the script that you want to 'use' the new package, and then using the syntax
your_package_name::subroutine_name("parameter1","parameter2","etc");
#!/usr/bin/perluse foo;
foo::bar("John","Smith");
exit;
For instance, I use this technique all the time to generate navigation bars. I pass the name of the calling page to the package, which uses it to generate the navbar but with the current page highlighted on it in some way. The benefit is that you only have a single piece of navbar code to maintain, and any changes you make in it will affect the whole site. (Cascading NavBars anyone?)
You could also use it to generate a standard page footer, but with page-specific taglines at the bottom, or to generate whole <head> sections of HTML with the one command passing the title, kw and desc as parameters.
I was reading "Programming Perl" by O'Reilly, and one of the things I was reading was the section on packages, classes, and object oriented programming.
Must say, these code snippets are great!!! I'm not actually into coding my own work (yet), but getting there. And having these things posted into the forums is fantastic!!!!
Thank you thank you thank you, when I get around to actually coding, I know I'm good posting errors, because there is so much talent here for things like this.
Cheers,
Han Solo
Incidentally, for people interested in PHP, there is the 'include' command which does the same job.
<? include("myfile.php"); ?>