> -----Original Message-----
> From: Mr. Shawn H. Corey [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, 07 June, 2006 07:16
> To: beginners@perl.org
> Subject: Re: reading Perl syntax
>
>
> On Tue, 2006-06-06 at 22:23 -0500, [EMAIL PROTECTED] wrote:
> > 46:  $self->{file} = $args->{file};
>
> This is the line were $self->{file} is set to undef.
>
> > 48:  $self->_read_file($self->{file});
> >
> > ** => 214:  my $file = shift;
>
> And here $file is set to $self->{file}, which is undef

Merely referencing a key in a hash sets it into the hash, though with a
value of undef. A good idea is to check whether it exists in the hash:

if (exists $self->{file}){
# do something about it...
}

Then, if it exists, check for a value:

if (exists $self->{file}){
    if ($self->{file} ne undef){
    # do something about it...
    }
}

Or, if it exists, check for a value other than undef, 0, "" (empty string):

if (exists $self->{file}){
    if (defined $self->{file}){
    # do something about it...
    }
}

There are a million ways to write this out in code, but if a piece of data
is crucial to the smooth operation of your application, you need to check
that it fits your validation expectations before trying to reference it.

Peace -
Ron Goral



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to