×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Extended superclass object functions not accessible from subclass?

Extended superclass object functions not accessible from subclass?

Extended superclass object functions not accessible from subclass?

(OP)
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

RE: Extended superclass object functions not accessible from subclass?

(OP)
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

RE: Extended superclass object functions not accessible from subclass?

(OP)
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 smile

"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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close