For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2008 > Variables initialization









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Variables initialization
Krzysztof Chodak

2008-03-17, 8:05 am

How is it possible to initialize variable using 0 or '' and not having
undef warning later on using them?

Chas. Owens

2008-03-17, 7:08 pm

On Mon, Mar 17, 2008 at 6:51 AM, <Krzysztof.Chodak@gmail.com> wrote:
> How is it possible to initialize variable using 0 or '' and not having
> undef warning later on using them?

snip

Well, you can say

my $var = 0;

but that doesn't prevent the variable from being set to undef later,
so if it is possible for the variable to be undef you should guard
against it with the defined function (or, if you have Perl 5.10, the
// operator):

print "\$var is ", (defined $var ? $var : "undef"), "\n"; #Perl 5.8 and below

say "\$var is ", $var // "undef"; #Perl 5.10


--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
T Baetzler

2008-03-17, 7:08 pm

Krzysztof.Chodak@gmail.com asked;
> How is it possible to initialize variable using 0 or '' and=20
> not having undef warning later on using them?


my $variable =3D 0; # <=3D initial value goes here

HTH,
Thomas

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com