On Sat, 2004-10-16 at 00:30, John V. Pataki wrote:
> Hmm....
>
> I think most of it is working -- however ....
>
> Is there a way to have sub routines that are in my main program
> available to be called from code in the module?
Bill gave one solution in a previous email (passing a code ref), but the
best solution is to simply re-work the subroutines to not be dependent
upon any subroutine that isn't in the module. The point of the module is
that you keep duplicating code, if that code can't run independently
then re-factor it till it can.
> This is where I was thinking that blessing would make the code in the
> module act as if it were a part of my main program... but i am not to
> sure how to implement this.
Ultimately bless has nothing to do with modules. It simply associates a
data structure with a package name-space (aka a class). If you don't
understand what that means don't worry about it because you won't need
it to create modules. When/if you get into Object Oriented programming
then you'll need to know what bless does, but until then just ignore it.
> The code I want to pull out of my main and put into a module
> establishes a connection to another program -- I then send command
> from that app back to my Perl program by calling subroutine names.
> However if the code for establishing the connection is now not apart
> of the main -- as in my module now -- the subroutine is not available
> to me now relative to the module? Or is it?
You have several choices:
1) You can change the way you pass the data back, like say returning it
or storing it into a common Data structure
2) You can copy the bits that you call into your module
3) You can re-factor into the code ref thing that Bill suggested
4) You can do something I haven't thought of here
Of these 1 is the most common. If you can rework your call so that
you're returning data instead of making more subroutine calls you may
find the code is easier to re-use later.
For example (and I'm purely making this up)
Say you had
sub main {
bar()
}
sub bar {
$data = ([call an external program]);
process($data);
}
sub process {
my $data = shift;
print $data;
}
and you wanted to move bar() into a Module you could easily re-factor it
to return $data instead of passing it to process like so:
use MyPackage; # exports bar()
sub main {
my $data = bar();
process($data);
}
sub process {
my $data = shift;
print $data;
}
package MyPackage;
use base qw(Exporter);
our @EXPORT = qw(bar);
sub bar {
$data = ([call an external program]);
return $data;
}
-Chris
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs