[EMAIL PROTECTED] wrote:

> How can I reuse a subroutine?
>
> My environment is redhat 9, apache2, perl-5.8.0-88, mod_perl-1.99_07-5.
> I've tried to put the sub in a separate file and call it from another as
> below. Please modify the snipet below to make it work.
>
> mycommon.pl
> -----------
> #!/usr/bin/perl
> #return a value wrapped by single quotes
> #should this be declare package something?

Okay, I am going to come back around to this, because it's actually been
crawling under my skin for a few days now.

You see, I did some functional modules before I started using package
statements, and they worked just fine.  The subs defined there could be
addressed very simply by their name.  I've al;so done quite a bit of much
more careful OO work using the package statement.  Turns out that I had
never really tried anything between, like functional modules within
packages.

You can do it without the package statement, and a post I sent a few days
ago, but deeper in the thread hierarchy, showed a version that worked.
Thast still left something lacking, and I couldn't see what I was missing.
After quite a bit of bumbling around through old scripts, I noticed the
crucial line that was missing.  In this case, it was:
our @ISA = 'Exporter';
which is what brings the import function into the namespace of the package.
So here is what I've come up with as a sort of bare minimum for using
functional packages without having to prepend each function call with a
package name:

in MyCommon.pm:
package MyCommon;

use strict;
use warnings;

use Exporter;

our @ISA = 'Exporter';
our @EXPORT = qw(do_wrap);

sub do_wrap {
    my $retval;

    if(length($_[0]) == 0) {
      $retval = "null";
    } else {
      $retval = "'" . $_[0] . "'";
    }
    return $retval;
}

1;


In test_my_common.pl:

#!perl

use strict;
use warnings;

use MyCommon;

my $return = do_wrap('hello');
print "$return\n";

Greetings! E:\d_drive\perlStuff>test_my_common.pl
'hello'

Greetings! E:\d_drive\perlStuff>

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to