bradcathey has asked for the wisdom of the Perl Monks concerning the following question:
Fellow Monasterians,
I'm attempting to reuse code. I have several scripts, that all have the same exact sub routines. I want to put the common subs in one file and give all the scripts access to it. What's the best way?
SCRIPT 1
#!/usr/bin/perl use warnings; use strict; use CGI::Carp qw(fatalsToBrowser); require "common.lib"; my $name = "frodo"; $name = get_uppercase($name);
COMMON 1
sub get_uppercase { my $name = shift; return uc($name); } 1;
or
SCRIPT 2
#!/usr/bin/perl use warnings; use strict; use CGI::Carp qw(fatalsToBrowser); use Common; my $name = "frodo"; $name = Common->get_uppercase($name);
COMMON 2
package Common; sub get_uppercase { my ($class, $name) = @_; return uc($name); } 1;
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Library file or module for sharing code?
by choedebeck (Beadle) on Dec 27, 2005 at 23:44 UTC | |
by kwaping (Priest) on Dec 27, 2005 at 23:58 UTC | |
|
Re: Library file or module for sharing code?
by sgifford (Prior) on Dec 28, 2005 at 04:10 UTC | |
by bradcathey (Prior) on Dec 28, 2005 at 13:23 UTC | |
by tye (Sage) on Dec 28, 2005 at 15:47 UTC | |
by bradcathey (Prior) on Dec 28, 2005 at 16:22 UTC | |
by tye (Sage) on Dec 28, 2005 at 17:40 UTC | |
by kwaping (Priest) on Dec 28, 2005 at 14:30 UTC | |
|
Re: Library file or module for sharing code?
by ff (Hermit) on Dec 28, 2005 at 23:44 UTC |