in reply to constant name in variable

Thanks for all the suggestions. I believe i have a working version. This is for part of a name string analyzer. Here is exactly what im doing:
use strict; use Carp; use constant { PREFIX => 1, SUFFIX => 2, TRUST => 3, DATE => 4, BUSINESS => 5, }; sub AUTOLOAD { my $self = shift @_; my $method = $AUTOLOAD; $method =~ s/.*:://; return if $method eq "DESTROY"; if ( $method !~ m/^Is(.+)$/ ) { confess "method = $method is not supported by AUTOLOAD!\n"; } my $token_type_value = eval( uc($1) ); if ( $@ ) { confess "invalid token type $1!\n"; } my ($token) = @_; return $self->_IsToken($token, $token_type_value); }
then i can make method calls like:
$analyzer->IsSuffix("PhD");
And _IsToken() does various tests, mostly against a list of known identifiers to determine if the token is of the type being asked about.

Replies are listed 'Best First'.
Re: Re: constant name in variable
by rir (Vicar) on Dec 02, 2002 at 22:01 UTC
    This seems like heavy sugar to avoid calling $analyzer->_IsToken() directly. Other than a preference for:
    $object->IsSuffix( "PhD");
    over
    $object->Is( SUFFIX, "PhD");
    I'm not seeing any reason to do this. Preference is a sufficient reason.

    Am I missing something?

      Preference is actually the main reason I've done it this way. It also allows an easy way to write an explicit IsSuffix(), to perform differently than what _IsToken(SUFFIX, ...) might do, if i need to create more in-depth analyzers than the trivial hash lookups that _IsToken() will be doing. That isn't a particularly great reason to do it this way though.
      I think i like practicing writing weird AUTOLOAD methods also, after having coded C++ for a number of years, which was much more annoying :)
      I am going to consider redesigning though.