in reply to Using constants. What am I doing wrong?

Others have told you why you get the result you get; here's a couple of ways to get around it:

# this: BEGIN { use vars '$Base'; $Base = 'mybase'; } use constant BASE => $Base; # or this: my $Base = 'mybase'; sub BASE () { $Base }

Replies are listed 'Best First'.
Re^2: Using constants. What am I doing wrong?
by dragonchild (Archbishop) on Feb 22, 2005 at 14:29 UTC
    The correct solution you're looking for is:
    my $Base; BEGIN { $Base = 'mybase' } use constant BASE => $Base;

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      Strange. I could have sworn I'd tried that solution first, but found it to not work. Just tried it again before I was going to post saying 'that does not work', but all of a sudden it works. Strange world :)

Re^2: Using constants. What am I doing wrong?
by Random_Walk (Prior) on Feb 22, 2005 at 11:34 UTC

    The second one does not make a constant

    perl -le'my $BASE="are belong to us"; sub BASE () { $BASE }; $BASE="fred"; print "all your base ", BASE' all your base fred

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!