in reply to How do I bury some code ?

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

Replies are listed 'Best First'.
Re^2: How do I bury some code ?
by rbi (Monk) on Aug 31, 2007 at 14:04 UTC
    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

        If I don't use Class:Struct it works the way I want (as in the following code). Here ./Word.pm only "knows" about ./WordExposed/Languages.pm.
        I cannot get something similar with structures.
        # ./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 warnings; use base 'WordExposed::Languages'; sub new { my $class = shift; my $self = {}; bless($self, $class); return $self; } sub set { my $self = shift(); my $english = shift(); my $language = shift(); $self->{english} = $english; $self->{language} = $language; $self->_translate(); } 1; # ./WordExposed/Languages.pm package WordExposed::Languages; use WordExposed::LanguagesPrivate::French; use WordExposed::LanguagesPrivate::Italian; sub _translate { my $self = shift(); if ($self->{language} eq 'fr') { $self->_traduction(); } elsif ($self->{language} eq 'it') { $self->_traduzione(); } } 1; # ./WordExposed/LanguagesPrivate/French.pm package WordExposed::LanguagesPrivate::French; our @ISA = qw(Exporter); our @EXPORT = qw( _traducion ); our @EXPORT_OK = qw(); sub _traducion { my $self = shift(); warn 'pomme' if ($self->{english} eq 'apple'); } 1; # ./WordExposed/LanguagesPrivate/Italian.pm package WordExposed::LanguagesPrivate::Italian; our @ISA = qw(Exporter); our @EXPORT = qw( _traduzione ); our @EXPORT_OK = qw(); sub _traduzione { my $self = shift(); warn 'mela' if ($self->{english} eq 'apple'); } 1;