Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: How do I bury some code ?

by rbi (Monk)
on Aug 31, 2007 at 14:04 UTC ( #636332=note: print w/ replies, xml ) Need Help??


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

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 :)


Comment on Re^2: How do I bury some code ?
Select or Download Code
Re^3: How do I bury some code ?
by Anno (Deacon) on Aug 31, 2007 at 14:55 UTC
    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;
        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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (15)
As of 2015-07-07 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (89 votes), past polls