Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I bury some code ?

by rbi (Monk)
on Aug 31, 2007 at 10:25 UTC ( #636287=perlquestion: print w/ replies, xml ) Need Help??
rbi has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I have a module that provides some exposed methods and has other methods that are private. Some of the private methods themselves use some private methods that are private just to them. An conceptual example would be the following:
directories structure: . |-- Word.pm |-- WordExposed | |-- Languages.pm | `-- LanguagesPrivate | |-- French.pm | `-- Italian.pm files: # foo.pl use strict; use warnings; use lib ('/path_to_Word_pm'); use Word; my $word = Word->new(); $word->set('apple','it'); # Word.pm package Word; use strict; use Class::Struct; use WordExposed::Languages; struct Word => { english => '$', language => '$', translation => '$', }; sub set { my $self = shift(); my $english = shift(); my $language = shift(); $self->english($english); $self->language($language); $self->_translate(); } 1;
The Word module is using the Languages modulethat provides the _translate() method. Languages is using some other methods that are private and used only there.
# Languages.pm package WordExposed::Languages; our @ISA = qw(Exporter); our @EXPORT = qw( _translate ); our @EXPORT_OK = qw(); sub _translate { my $self = shift(); if ($self->language eq 'fr') { $self->_traduction(); } elsif ($self->language eq 'it') { $self->_traduzione(); } } 1; # French.pm package WordExposed::LanguagesPrivate::French; our @ISA = qw(Exporter); our @EXPORT = qw( _traducion ); our @EXPORT_OK = qw(); sub _traducion { my $self = shift(); } 1; # Italian.pm package WordExposed::LanguagesPrivate::Italian; our @ISA = qw(Exporter); our @EXPORT = qw( _traduzione ); our @EXPORT_OK = qw(); sub _traduzione { my $self = shift(); } 1;
I'd like to come up with using WordExposed::LanguagesPrivate::French; and WordExposed::LanguagesPrivate::Italian; only in Languages.pm, so that I can simply put use WordExposed::Languages; in Word.pm and bury everything within Languages.pm.

I hope this example is clear enough. Thanks for suggestions.

Comment on How do I bury some code ?
Select or Download Code
Re: How do I bury some code ?
by Anno (Deacon) on Aug 31, 2007 at 13:25 UTC
    I'd like to come up with using WordExposed::LanguagesPrivate::French; and WordExposed::LanguagesPrivate::Italian; only in Languages.pm, so that I can simply put use WordExposed::Languages; in Word.pm and bury everything within Languages.pm.

    I hope this example is clear enough. Thanks for suggestions.

    Sorry, but your example is not at all clear. Currently you're not using the modules WordExposed::LanguagesPrivate::French and WordExposed::LanguagesPrivate::Italian at all. That makes it hard to come up with suggestions how to fit them in because we don't know their intended function.

    I notice that you are combining function exportation with an overall OO approach. That can occasionally make sense, but it isn't the norm. Do you have a particular reason for that approach?

    Anno

      Sorry. I meant that the above works only if I put use WordExposed::LanguagesPrivate::French; and use WordExposed::LanguagesPrivate::Italian; in Word.pm, while I'd like to put those two instructions in Languages.pm only
      # Word.pm package Word; use strict; use Class::Struct; use WordExposed::Languages; use WordExposed::LanguagesPrivate::French; # IT WORKS use WordExposed::LanguagesPrivate::Italian; # IT WORKS struct Word => { english => '$', language => '$', translation => '$', }; sub set { my $self = shift(); my $english = shift(); my $language = shift(); $self->english($english); $self->language($language); $self->_translate(); } 1;
      My idea is to separate the bunch of methods I have around for a given structure into some different modules/files, without having to refer to all of them in the Module that defines the structure (Word.pm above). In such a way that only Languages.pm deals with Fench.pm and Italian.pm and I only have to make changes to Languages.pm if I want to add - say - German.pm

      I thought that the above is the only way to make available the translate() method to the Word structure, if it is defined in another file. I understand it is not :)
        It seems to me you're missing out on inheritance. Make Word a subclass of WordExposed::Languages. Then methods that are defined in  WordExposed::Languages become callable through Word objects. In particular:
        # Word.pm package Word; use strict; use base 'WordExposed::Languages'; use Class::Struct; # etc
        should allow you to relegate use WordExposed::LanguagesPrivate::French; etc. to WordExposed::Languages

        The _translate method (presumably defined in  WordExposed::Languages) should then respond to Word objects.

        Anno

Re: How do I bury some code ?
by dsheroh (Parson) on Aug 31, 2007 at 15:09 UTC
    I'm not sure I understand your question either, but it sounds like you want to put multiple packages into a single .pm file. You can do this with
    package A; # some code here package B; # more code here package C; # and still more code
    Save that as Foo.pm and then use Foo; will load packages A, B, and C (and not package Foo, since it's not defined). Packages normally have the same name as the .pm file that contain them and have a one-to-one relationship, but that's by convention (and a very good convention!), not because the language requires it. You can put many packages into a single file or split a single package across many files. But remember that just because you can doesn't necessarily mean you should.

    Given the description of what you want to accomplish, you may want to take a look at how the DBI and DBD modules are structured and call each other, since they have a similar design where you just use DBI, then it loads up whichever DBD module(s) it needs behind the scenes without needlessly combining them into a single source file.

      Thanks but I want files to be separated into a directory structure. Following your idea, I'd be happy if I could have one file for the package that can include a list of files, and each of these again include other files. The result after inclusion (at compile time) would be the file you suggest.
      Something like Filter::Macro does, but for files with just a set of instructions to be included, not modules.
Re: How do I bury some code ?
by hossman (Prior) on Sep 01, 2007 at 05:29 UTC

    you seem to have a fundamental problem in that you want to do object oriented kinds of things, but you wnat to use a "struct" based approach.

    once you resolve this discrepancy, you should look into using Module::Pluggable instead of needing a giant "if" switch to pick which module to use.

    (for that matter, if the only reason you created WordExposed::Languages was to have a _translate method with that giant if statement you can eliminate the whole thing and use Module::Pluggable directly from your Word class.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-07-30 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls