Beefy Boxes and Bandwidth Generously Provided by pair Networks Ovid
Syntactic Confectionery Delight
 
PerlMonks  

Re^4: How do I bury some code ?

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


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

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;


Comment on Re^4: How do I bury some code ?
Download Code
Re^5: How do I bury some code ?
by Anno (Deacon) on Aug 31, 2007 at 16:30 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.

    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://636365]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2014-04-21 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls