On Sat, 2002-06-22 at 21:27, drieux wrote:
> 
> volks,
> 
> I'm playing around with a little perl module and I came
> across to different solutions - thought I would ask for
> opinions as to which would be more Kosher - so why not
> ask folks who might have an opinion:
> 
> Plan A: The simple 'undef' trick
> 
>       #------------------------
>       sub get_Lock {
>          my ($self, $name) = @_;
>       (exists $self->{$name}) ? $self->{$name}->{var_lock} : undef;
>       } # end of get_Lock
> 
> Plan B: your basic Croak Play
> 
>       #------------------------
>       sub get_Lock {
>               my ($self, $name) = @_;
>               croak "No such variable $name \n"
>                       unless (exists $self->{$name}) ;
>               $self->{$name}->{var_lock};
>       } # end of get_Lock
> 
> Under Plan A - one would code it like:
>       
>       my $lock = $global->get_Lock('var1');
> 
>       BailOut("bad var1\n") unless ( defined( $lock ) ) ;
> 
> since, well - it is possible that 'var1' does not exist...
> 
> Under Plan B - one gets the SCREAM from croak right off
> 
>       [jeeves:pbl/bloopers/dumbDrieux] drieux% perl OO_GlobalGame.txt
>       No such variable var1
>        at OO_GlobalGame.txt line 14
>       [jeeves:pbl/bloopers/dumbDrieux] drieux%
> 
> and one could go look at line 14 of their code and go....
> "Oh dear, never really meant that one - meant to check 'var'
> or what ever it should have been....
> 
> Opinions?
> 
I think Plan A is a more general solution, because maybe there will be 
some times when you don't want a warning, instead you'll want to be able
to make some test:

if ($lock = $global->get_Lock('var1') {
    # var1 exists
    # run this code
} else {
    # var1 doesn't exist
    # do something else
}

just my 2 cents



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to