in reply to Re^2: How do I bury some code ?
in thread How do I bury some code ?

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

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

      Ah, I see. Sorry, I haven't used Class::Struct, so I can hardly comment. Some confrater please take over.

      Update: A look at the documentation shows this rather serious limitation:

      The class created by "struct" must not be a subclass of another class other than "UNIVERSAL".
      In other words: The simple (and standard) solution I proposed won't work with Class::Struct

      You could try this: Make an extra class, say Word::Struct that contains the Class::Struct-related stuff. Then, in class Word, inherit from both Word::Struct and WordExposed::Languages.

      Meanwhile, your best course may be be to work out the different behavior of a "standard" class and a Class::Struct based one in a small, independently runnable example and repost to SoPW under a new subject (containing Class::Struct).

      Anno