Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Using constants. What am I doing wrong?

by saskaqueer (Friar)
on Feb 22, 2005 at 04:50 UTC ( [id://433249]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://433249]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found