Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Ugly syntax

by chipmunk (Parson)
on Dec 04, 2001 at 10:27 UTC ( #129279=note: print w/replies, xml ) Need Help??


in reply to Ugly syntax

The results you get are caused by the special bareword-quoting feature of =>. You can either avoid giving the constant to => as a bareword, or switch to a regular comma:
%nums = ( @{[FIRST]} => 'Ein', SECOND() => 'Zwei', &THIRD => 'Drei', (FOURTH) => 'Vier', FIFTH, 'Funf', );

Replies are listed 'Best First'.
Re: Re: Ugly syntax
by Dr. Mu (Hermit) on Dec 04, 2001 at 10:43 UTC
    Yup, SECOND through FIFTH are much cleaner; thanks! Quick response, too!

    I'm wondering about the long-term portability of SECOND and THIRD, though, as they seem to rely on the particular way that constant is implemented.

      In a nutshell, this is what sucks about constant. It's not really a built-in, but rather more of a hack based on an implementation detail.

      I used it on a big project, and I don't think I would do it again. The syntax for using constants in interpolated strings and HERE docs is horrible, and you have to be constantly alert for subtle bugs caused by the problem you desribe here. I think you're better off with globals.

        Some of the mod_perl scripts I inherited used loads of constants. I think they suck because a) interpolation doesn't work properly and b) you get an annoying 'redefinition' warning under mod_perl first time you run the script

        I prefer using a stack of variables with all caps at the top of each script, for example:

        my $SMTP = 'blah'; my $EMAIL = 'blah@blah.com'

        All my other variables are all lower case, and while these aren't 'true' constants I don't ever modify them.

        Gav.

(tye)Re: Ugly syntax
by tye (Sage) on Dec 04, 2001 at 22:40 UTC

    The use of &THIRD can lead to problems so it isn't a habit I encourage people to get into. The most common way that I see problems with this are typical constants exported from modules where &THIRD can cause a warning if $_[0] contains a non-numeric string. Yes, part of the reason that such a warning is possible is because the default way of making constants in the XS code of modules doesn't actually make constants like use constant does (and I think that is changing).

    Anyway, using & without () is usually a bad idea, so I don't encourage its casual use. See (tye)Re: A question of style for more on why.

            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2020-01-21 23:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?