in reply to How to create a "CONSTANT"

If you really want a $const (not "use constant"), then I'd go with a tie:
sub CONST::TIESCALAR { bless \(my $v = pop), shift } sub CONST::FETCH { ${(shift)} } tie $x, CONST => 4; print "$x\n"; $x = 7; # run-time error
--Dave

Replies are listed 'Best First'.
Re: Re: How to create a "CONSTANT"
by freddo411 (Chaplain) on Jun 09, 2003 at 20:52 UTC
    Thanks to kelan, perrin, sauoq, WBT, larsen, dpuu, and nite_man for the replies. You all were correct in assuming that I didn't want to use
    use constant PIE => "Warm Apple";
    I find
    print "The value of PIE is ${ \PIE }.\n";
    or even
    print "pie: ", PIE, "\n";
    not pleasing to read or write. The former is truly ugly, and the later looks a bit like a filehandle.

    Also, I have an existing code base with lots of scalar variables that are actually constants, so dpuu's solution is easiest to implement for me.

    Cheers

    --Fred


    Nothing is too wonderful to be true
    -- Michael Faraday