in reply to Re: managing constants created with constant.pm
in thread managing constants created with constant.pm

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

  • Comment on Re: Re: managing constants created with constant.pm

Replies are listed 'Best First'.
Re: Re: Re: managing constants created with constant.pm
by broquaint (Abbot) on Jul 14, 2003 at 11:25 UTC
    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