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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hi All,

It's been years since I posted here... I've been away with the fairies in Python and Ruby land for so long I've forgotten almost everything I ever knew about Perl OO...

I have no doubt that the following question is extremely basic..

I want to extend a class for a Perl API with one additional method.

I have the method but I don't want to edit the core API files. I would like to simply add the method in my script.

So, to test this: filename: SPEAK/ITALIAN.pm
package SPEAK::ITALIAN; use strict; use warnings; use Exporter; use vars qw(@ISA @EXPORT); @ISA = qw( Exporter ); @EXPORT = qw(hello); use constant HELLO => "Hello"; use constant GOODBYE => "Goodbye"; sub hello { print HELLO . " in Italiano\n"; } return 1;

Now, in my script (in this case "test.pl") I simply want to add a method to the SPEAK::ITALIAN class with all constants and other good stuff in the correct namespaces and so on

filename: test.pl
#!/usr/bin/perl use strict; use warnings; use SPEAK::ITALIAN; sub goodbye{ print GOODBYE . " in Italiano\n"; } hello(); goodbye();
Running this gives:
Hello in Italiano! GOODBYE in Italiano!

As you've noticed, GOODBYE is incorrect. It's not picking up the constant from the SPEAK::ITALIAN module.

I know I'm missing something very basic. Do I need "use base qw(SPEAK::ITALIAN)"?

Any pointers?

Thanks in advance and apologies for the awfulness of the question...

S


In reply to Very basic package / module question... by smullis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2022-09-26 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (116 votes). Check out past polls.

    Notices?