Emulate `enum' types? Check presence of args? 
Author Message
 Emulate `enum' types? Check presence of args?

I build operator trees, so I have a class BoolOp (for instance) with a
constructor which is used like this:

$x = new BoolOp( type  => 'and',
                 left  => $left,
                 right => $right );

Writing several classes like this leads to the following replicated
boilerplate-style code:

* Check that all of `type', `left', and `right' are present in the arg
  list (except if `type' is `not', then `left' is not given).

* Check that `type' has one of the allowed values `and', `or', `not'.

Right now, I have a %parm hash for the constructor args and do things
like this:

$parm{type} or die "Missing arg `type'";
$parm{type} eq 'and' or $parm{type} eq 'or' or $parm{type} eq 'not'
  or die "Wrong arg `type', must be one of `and', `or', `not'";
$parm{type} eq 'not' or $parm{left} or die "Missing arg `left'";
$parm{right} or die "Missing arg `right'";
$self->{type} = $parm{type};
$parm{type} eq 'not' or $self->{left} = $parm{left};
$self->{right} = $parm{right};

As you can see, this gets rather tedious rather quickly.  Is there a
way to ease the pain?

(I `use fields' and there is some stuff in the documentation about
initializing pseudo-hashes; I think this does not apply to the above
question, but maybe I have misunderstood the documentation?)

kai (happy user of Perl 5.6.0 on a Unix system)
I like BOTH kinds of music.

Sat, 04 Jan 2003 03:00:00 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Accounting Package in Delphi with source code.

2. Accessing Access 2.0 with ODBC Express

3. Checking image presence and properties

4. how to do type checking on cgi's

5. XS: Accessing 'enum' constants

6. How do you emulate 'source'?


8. Emulating 'script' with perl

9. emulating the shell '&' in perl

10. Help on sequential seach on TTable!!!

11. recuper le Mime type d'un fichier - Content-type of file

12. type id's for Oracle types


Powered by phpBB® Forum Software