in reply to Arcitectural considerations with modules
The library file can export functions using Exporter, and another module can import them and they will be available as methods. It's not a problem if multiple modules all do this, and you can tell what class the method is called from by the first parameter passed in, which will be the name of the class. Here's some sample code:
#!/usr/bin/perl use warnings; use strict; package TestBase; use base 'Exporter'; our @EXPORT_OK = qw(new do_this); sub new { warn "Calling Test1::new\n"; bless {}, shift; } sub do_this { my $self = shift; warn "doing_this to $self\n"; } package Test1; TestBase->import(qw(new do_this)); package Test2; TestBase->import(qw(new do_this)); package main; my $obj1 = Test1->new(); $obj1->do_this(); my $obj2 = Test2->new(); $obj2->do_this();
You can also re-export symbols you have imported, if you need to do that for some reason. Doing this along with inheritance should allow you to inherit both methods and library functions. For example:
#!/usr/bin/perl use warnings; use strict; package TestBase; use base 'Exporter'; our @EXPORT_OK = qw(do_this); sub do_this { my $self = shift; warn "doing_this to $self\n"; } package Test1; TestBase->import(qw(do_this)); use base 'Exporter'; our @EXPORT_OK = (@TestBase::EXPORT_OK); package main; Test1->import(qw(do_this)); do_this("some string");
Generally, a Perl object will keep all of its data in a hash or array, so having do_this change a field in the object instead of a global variable might help some of your problems.
Hope that's helpful!
|
|---|