in reply to managing constants created with constant.pm

Cheeky, but effective
sub UNIVERSAL::which_constants { my($class, @constants) = @_; return grep exists $constant::declared{"$class\::$_"}, @constants; }
Now all your classes will have the which_consants class method which given a list of constant names returns the names which exist as constants in the given package. As you can see that's simply implemented by looking into the %constants::declared variable, so that's just one approach to the problem.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: managing constants created with constant.pm
by antirice (Priest) on Jul 14, 2003 at 11:14 UTC

    broquaint, I respect you very much. However, I was just wondering if you were aware that @constants will contain numbers and that you'll be checking whether or not a constant by that name exists...which it won't. Of course, you could declare the constants as X => "X" instead of X=>number to get it to work. I dunno. It's 4am and am being a bit dense I guess. :-/

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      that @constants will contain numbers and that you'll be checking whether or not a constant by that name exists...which it won't.
      Hmm, well maybe my function doesn't do what the OP requested (quite possible since it was written pre-1pm ;) but it will do what I intended - given a list of strings return if they exist in a given package e.g
      sub UNIVERSAL::which_constants { my($class, @constants) = @_; return grep exists $constant::declared{"$class\::$_"}, @constants; } use constant FOO => "a string"; use constant BAR => [ qw/ an array / ]; use constant BAZ => { qw/ a hash / }; print "constant exists - $_\n" for main->which_constants( qw/ ichi FOO bar BAZ one DEUX / ); __output__ constant exists - FOO constant exists - BAZ
      And if you want to go from constant values to constant names then check out chromatic's Devel::Constants module.
      HTH

      _________
      broquaint