smullis has asked for the wisdom of the Perl Monks concerning the following question:
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.pmpackage 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.plRunning this gives:#!/usr/bin/perl use strict; use warnings; use SPEAK::ITALIAN; sub goodbye{ print GOODBYE . " in Italiano\n"; } hello(); goodbye();
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