in reply to Possible Stupid Perl Trick - Importing method into object class?
If you want to require "use MyCode::ProcessC" be added to the "data type" class, then that is just standard, run-of-the-mill Exporter.pm work with @EXPORT= qw( process_c ).
But it sounds like you want the "use MyCode::ProcessC" to be somewhere else. Since you are useing and not requireing, you could put the exporting code into MyCode::ProcessC::import (rather than a BEGIN block), which means that it would be called multiple times if you use MyCode::ProcessC multiple times. But that might be a good thing if you want to be "general purpose".
For example, you could require: use MyCode::ProcessC( "DataType::Class" ); in the main script and have MyCode/ProcessC.pm contain:
- tye (but my friends call me "Tye")package MyCode::ProcessC; use strict; sub import { if( 2 != @_ ) { require Carp; Carp::croak( 'Usage: use ',__PACKAGE__, '( "DataType::Class" )' ); } my( $self, $class )= @_; for my $meth ( qw( process_c ) ) { no strict 'refs'; *{$class."::".$meth}= \&$meth; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (tye)Re: Possible Stupid Perl Trick - Importing method into object class?
by Masem (Monsignor) on Dec 08, 2001 at 00:51 UTC | |
by tye (Sage) on Dec 08, 2001 at 03:53 UTC | |
by Aristotle (Chancellor) on Dec 10, 2001 at 05:30 UTC | |
by dragonchild (Archbishop) on Dec 08, 2001 at 01:02 UTC | |
by tye (Sage) on Dec 08, 2001 at 04:22 UTC |