Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extended superclass object functions not accessible from subclass? 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
0
0
GB
Hi,

I have a superclass where I have added a
Code:
use Decode qw(encode decode);

I then extend to a subclass, but when I try to use encode in the subclass I get an error that the routine is undefined?

I've tried the following syntax's

Code:
encode('UTF8',$my_string);

SUPERCLASS_PACKAGE_NAME::encode('UTF8',$my_string);

SUPERCLASS_PACKAGE_NAME->encode('UTF8',$my_string);

What am I doing wrong?

Thanks,
1DMF


"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Well at first I thought I had forgotten the 'me/this' paradigm, but using
Code:
$this->encode('UTF8',$my_string);
didn't work either.

So I just decided to implement a helper method in the super class.

Code:
#################
# encode helper #
#################
sub _encode 
{
    my ($self, $code, $string) = @_;
    
    return encode($code, $string);
}

Now in all my sub classes I don't have to keep adding the encode module, I can simply use...
Code:
$self->_encode('UTF-8',$my_string);

I know implementation of classes is meant to be a black box and all that, so i guess I'm not meant to get access to modules used in the super class without exposing the methods like I now have.

Is that why it wouldn't work?

Thanks,

1DMF.


"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Having spoken with the perl IRC guru's.

I was correct, you can't and shouldn't be trying to access superclass module functions from subclasses, OOP is about exposed methods / object protocol.

If I want this type of functionality so you don't keep repeating the use of the encode module in the subclasses, you implement a class helper / wrapper method.

I refactored so I have a default of UTF-8 or can pass an optional argument for the encoding if desired.

Code:
#################
# encode helper #
#################
sub _encode 
{
    my ($self, $string, $code) = @_;
    
    if($code)
    {        
        return encode($code, $string);
    }
    else
    {
        return encode('UTF-8',$string);
    }
    
}

I can now simply use
Code:
$self->_encode($my_string,[encoding])
anywhere in my subclasses, and it's working as desired :)

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top