Re: perl - calling more code from a separate file

  • From: "Octavian Rasnita" <orasnita@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Fri, 24 Oct 2008 22:13:18 +0300

The usual way of doing this is to create a perl module which is called in your scripts.


For example, here is a simple perl module that you could put in a directory that sits in the same directory with your perl scripts that use it:

lib/My/Module.pm

package My::Module;

use strict;

sub do_something {
print "ok";
}

1;

Then in your scripts you can use something like:

use strict;
use lib "lib";
use My::Module;

My::Module::do_something();

It should print "ok", but you could do everything you want in the subroutines from the module.

Or you could use the "Exporter" module in your perl module if you don't want to need to specify the whole class path like My::Module::do_something, but only do_something().

Or you could create an object oriented module, using something like:

# In lib/My/Module.pm

package My::Module;

use strict;

sub new {
my $class = shift;
my $self = {}; #The object

#Here you can add some properties to your object like:
$self->{some_property} = "some value";
$self->{another_property} = ['an', 'arrayref', 'of', 'values'];

return bless $self, $class;
}

sub do_something {
my $self = shift;
print $self->{some_property};
}

1;

And you could use such a module like:

use strict;
use lib "lib";
use My::Module;

my $object = My::Module->new;
#Or you can also use:
# my $object = new My::Module;

Then apply the defined method of the object:

$object->do_something;

This should print the value of some_property which was defined in the constructor (the new() subroutine).

Of course, you can send parameters to the constructor or to the methods, using:

sub new {
my $class = shift;
my @properties = @_;

Then add those properties to the object.

and:

sub do_something {
my ($self, @properties) = @_;
# Here you can use the properties from @properties as you like

And you can call the constructor or the methods with parameters like scalars, array refs, hashrefs, or other types:

my $object = My::Module->new("first item", ['an', 'arrayref'], {key1 => 1, key2 => 2});

$object->do_something(1, 2, 3);

You should read:

perldoc perlboot
perldoc perltoot
perldoc perltooc
perldoc perlbot
perldoc perlobj

and especially:
perldoc perlmod
perldoc perlmodlib
perldoc perlmodstyle

Octavian

----- Original Message ----- From: "Lamar Upshaw" <lupshaw@xxxxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Friday, October 24, 2008 7:05 PM
Subject: perl - calling more code from a separate file


I have a few scripts which each do different things, however each of the scripts have the same code in a particular area. I need to be able to update that code on occasion, but I don't want to have to try and remember to do it in all the scripts, so I was wondering, is there a way I can put the code in one file, and have all the scripts call the code at the time it's needed, instead of putting a copy of the code in each script? This way, I can update the code in the one file, and in turn all the scripts will continue to function smoothly without my having to touch them.

With All Respect,
Upshaw, Lamar T
__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: