Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

possible minor bug in constant.pm #824

Closed
p5pRT opened this issue Nov 6, 1999 · 2 comments
Closed

possible minor bug in constant.pm #824

p5pRT opened this issue Nov 6, 1999 · 2 comments

Comments

@p5pRT
Copy link

p5pRT commented Nov 6, 1999

Migrated from rt.perl.org#1752 (status was 'resolved')

Searchable as RT1752$

@p5pRT
Copy link
Author

p5pRT commented Nov 6, 1999

From mike808@mo.net

The relevant section of code from constant.pm v1.00 from perl 5.005_03​:

sub import {
  my $class = shift;
  my $name = shift or return; # Ignore 'use constant;'
  croak qq{Can't define "$name" as constant} .
  qq{ (name contains invalid characters or is empty)}
  unless $name =~ /^[^\W_0-9]\w*$/;

The RE used to validate names is broken for several perfectly
valid "constants", namely those that start with underbars.

  use constant qw( _MYCONSTANT => 4 ); # busted

I propose an improved RE as follows​:

  unless $name =~ /^(\w{2,}|[^\W_0-9AB])$/;

I added in preventing A and B being used as constants, as I don't like the
ability to say 'use constant qw( A => 4 );' since you're messing with the
globs associated with $A and $B used in the infamous 'sort' operator.
A slip of the $ will hose your sort. We're really trying to prevent
constants that conflict with the builtin global variables, right?
($_, $0, $1, $2, $3, $4, $5, $6, $7, $8, $9)
I'd just like to see $A and $B in that list, too.

The background on how I found this problem is that I use a derived
constant.pm that also exports the appropriate value as the appropriate
symbol, allowing faster access directly via the symbol for those who
want to.

i.e. 'use MyConstant TEN => 10;' also does an extra '$TEN = 10;'
step. It also creates the appropriate types when handed refs.
i.e. 'use MyConstant DIGITS => \[ 0 .. 9 ];' also does an extra
'@​DIGITS = (0..9);' so you don't have to access only via a function
interface.

But, that's *my* version of constant.pm, and I found it handy, and
not really anything to do with the CORE constant.pm. Just some background
on how I found the bug.

Michael
mike808@​mo.net

@p5pRT
Copy link
Author

p5pRT commented Nov 6, 1999

From [Unknown Contact. See original ticket]

On Sat, 6 Nov 1999, Michael King wrote​:

croak qq\{Can't define "$name" as constant\} \.
        qq\{ \(name contains invalid characters or is empty\)\}
    unless $name =~ /^\[^\\W\_0\-9\]\\w\*$/;

The RE used to validate names is broken for several perfectly
valid "constants", namely those that start with underbars.

Not broken; intentional. (Although I can't recall at the moment why in the
world I didn't simply use [A-Z] as that character class.... Maybe I was
thinking obfuscated thoughts. :-) As the docs say​:

  Constant names must begin with a letter.

Names beginning with an underbar are implicitly "reserved for future
expansion".

I propose an improved RE as follows​:

unless $name =~ /^(\w{2,}|[^\W_0-9AB])$/;

I added in preventing A and B being used as constants, as I don't like the
ability to say 'use constant qw( A => 4 );' since you're messing with the
globs associated with $A and $B used in the infamous 'sort' operator.

You seem to be confused; sort doesn't use $A and $B.

A slip of the $ will hose your sort. We're really trying to prevent
constants that conflict with the builtin global variables, right?

Nope. I merely wanted to reserve names starting with an underscore, so
that we'd have a class of such names to draw upon. Note that this
precludes a constant named __LINE__, for example. On the other hand, names
like BEGIN and ARGV are not precluded by this mechanism, but I still hope
no one will choose those as constant names....

Cheers!

--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case​: http​://www.rahul.net/jeffrey/ovs/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant