[Chicago-talk] constants and base pragma

JT Smith jt at plainblack.com
Thu Mar 16 13:34:26 PST 2006


I don't often use constants, but I found myself using them today and noticed something 
strange. I thought I'd ask if this was a bug in perl, or if I just didn't understand 
what's going on. Here's a made up example:

package Dog;

use strict;
use base 'Exporter';
our @EXPORT = qw/BARK/;
use constant BARK => 1000;

sub new {
   bless {}, $_[0];
}

1;

package Rover;

use strict;
use base 'Dog';

sub getBark {
     return BARK;
}

1;


perl -MRover -e 'print Rover->new->getBark."\n"';


I'd expect the above command to print "1000", but instead I get this:

Bareword "BARK" not allowed while "strict subs" in use at Rover.pm line 7.
Compilation failed in require.
BEGIN failed--compilation aborted.

If I do this in a non-OO manner everything works as expected. Is it not possible to 
export constants from a base class? Is there a better way?

For the time being I just created some methods that return these static values. But it's 
not as clean as I'd like because I have to call:

$dog->BARK

instead of just

BARK

Thanks for your help.


JT ~ Plain Black
ph: 703-286-2525 ext. 810
fax: 312-264-5382
http://www.plainblack.com

I reject your reality, and substitute my own. ~ Adam Savage


More information about the Chicago-talk mailing list