in reply to Constant names come into conflict with Perl style?

It is a good point. All caps subs are reserved, so it's better to use "constants" with at least a non-cap (underscore etc...).

use constant MY_PACKAGE => 'blah blah'; use constant APPROX_PI => 3.1415926; use constant _PI_ => 3; # ugly?
cheers --stephan

Replies are listed 'Best First'.
Re^2: Constant names come into conflict with Perl style?
by grinder (Bishop) on Feb 08, 2007 at 09:25 UTC
    All caps subs are reserved

    Unfortunately, at some point last year the Perl 5 Porters accidentally broke this rule. There was a problem with cloning in threaded environments, and a magic subroutine named CLONE_SKIP was ushered in to deal with it.

    Despite all the eyes of the people who worked on it, it escaped into the wild and it wasn't until a couple of months later that someone noticed that the name violated the guidelines (in that it really should have been called CLONESKIP).

    Such is life.

    • another intruder with the mooring in the heart of the Perl

      Thanks for pointing this out. I actually remember the discussion on p5p and it was interesting, see the CLONE_SKIP story.

      So the underscore is not so good actually :(. The next best thing could be a simple "postfix".

      use constant MYPACKAGE_c => 'Universe::Tiny'; use constant RWXu => 0700; use constant MYLOGGERc => \do{local *FH; *FH};

      Actually a postfix like "c" or "_c" (or even "const" if you don't feel lazy) is good *visually* to remember they are gotchas like remembering when {}ing is actually necessary! (stressed by PBP)

    • MYPACKAGE_c->factory();
    • print {LOGGERc} "important login data";
    • #!/usr/bin/perl use strict; use warnings; use constant LOGGERc => \do{local *FH; *FH}; # silence warnings open LOGGERc, ">mylog" or die; # gotcha print {LOGGERc} "info...", "\n" or die; print {\*STDOUT} qx(cat mylog);
      decent_shell% perl -MLWP::UserAgent -Mconstant=PKGc,LWP::UserAgent -e +'print +PKGc->new();'
      cheers --stephan