Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Module ( method ) alias ?

by exilepanda (Monk)
on Oct 04, 2012 at 05:14 UTC ( #997181=perlquestion: print w/ replies, xml ) Need Help??
exilepanda has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a module set to manage Video files for our Knowledge Management system. The interface is something like this

use ManVideo; my $mv = new ManVideo( DB => $dbhRef ); $mv -> newVideoRecord ( Trainer => "someone", Year => 2011, VideoType +=> "training" ); $mv -> save();

And now, my next task is almost the same, and yet identical , but this time is for audio. So I may use the same framework, by giving different DB handle. However, although I can completely use ManVideo::* to do the job, however this is odd to use the same interface to write my source code for Audio like this : $audio = newVideoRecord ( ... )

So, is there any simple (and safe) hack, so that I can create another module set, namely ManAudio, looks like :

my $aud = new ManAudio; $aud -> newAudioRecord( ..., AudioType => "meeting" ) ;# implement by +ManVideo::newVideoRecord;

One thing that might be a little complex is that I might need to do some pre-process on the arguments. As shown in above code is AudioType=>"meeting". I have to change AudioType to VideoType before passing to newVideoRecord(). ( This return me a list of authorize staffs to view/listen )

Thank you very much in advance for any clues.

Comment on Module ( method ) alias ?
Select or Download Code
Re: Module ( method ) alias ?
by Anonymous Monk on Oct 04, 2012 at 05:36 UTC
    call'em inheritance
    package AudioRecord; use parent qw' ManVideo '; sub new { my $class = shift; ... my $obj = $class->SUPER::new( @_ ); ... return $obj; }

    my $rec = AudioRecord->new( ... );

    perlootut, Modern Perl

      Thanks for answering. However seems work out a bit weird..

      ModB.pm

      package ModB; use parent qw'ModA'; sub new { my $c = shift; return $c->SUPER::new ( @_ ) ; } 1;

      ModA.pm

      package ModA; sub new { return bless {}, shift } sub p { print @_ } 1;

      main.pl

      use ModB; my $b = ModB->new(); $b -> p ( "World!" ) ;

      It prints ModB=HASH(0x182a5e4)World!. Don't know what am I missed. Also, could you explain a little what is SUPER:: referring to? Is it refer to where parent declared? And how can I inherit other methods with other method name?

      Thanks again!

        The first two lines do it all, all the others are only if you want to change something , like swap AudioType with VideoType ), Modern Perl and perlootut explain everything else, just use Ctrl+F to search
Re: Module ( method ) alias ?
by NetWallah (Abbot) on Oct 04, 2012 at 05:36 UTC
    Subclass and ALias (or call):
    package ManAudio; use base ManVideo; sub newAudioRecord {$_[0]->{VideoType}=$_[0]->{AudioType}; ManVideo::newVideoRecord(@_)} # There is probably a way to use __SUPER__ .. (Aha + - Anonymonk has obliged) # Too lazy/too late to figure out the *Alias syntax , # which would be an alternative way of calling subs in the other pac +kage
    *Untested*

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

Re: Module ( method ) alias ?
by scorpio17 (Monsignor) on Oct 04, 2012 at 13:32 UTC
    A better approach would be to create a ManMedia base class, then derive both ManAudio and ManVideo from that. ManMedia should contain everything common to both. Video specific stuff goes into ManVideo, and audio specific stuff goes into ManAudio.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997181]
Approved by davido
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (15)
As of 2014-09-02 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (25 votes), past polls