Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > "my $x;" in while or for block?

Reply
Thread Tools

"my $x;" in while or for block?

 
 
Am Nym
Guest
Posts: n/a
 
      11-10-2012
Bad practice to declare a variable w/in a while or for block?

Is this OK?:

$ cat bar
#!/usr/bin/perl
use strict;
use warnings;

my %foo = (
bob => "green",
sue => "blue",
);

for (keys %foo)
{
my $x;
# acceptable?? (want $x to be undefined at start of each iteration)

printf "hi\n" unless(defined ($x));
$x = 1;
printf "HO\n" unless(defined ($x));

print "$_\n\n";
}
$ ./bar
hi
sue

hi
bob

--
thanks

 
Reply With Quote
 
 
 
 
Am Nym
Guest
Posts: n/a
 
      11-11-2012
Ben Morrow <(E-Mail Removed)> writes:

> Quoth Am Nym <(E-Mail Removed)>:
>> Bad practice to declare a variable w/in a while or for block?

>
> No, absolutely not. You should declare your variables in the smallest
> scope you can.


Thanks, appreciate the help.

>> Is this OK?:
>>
>> $ cat bar
>> #!/usr/bin/perl
>> use strict;
>> use warnings;
>>
>> my %foo = (
>> bob => "green",
>> sue => "blue",
>> );
>>
>> for (keys %foo)
>> {
>> my $x;
>> # acceptable?? (want $x to be undefined at start of each iteration)

>
> Yes, this is good practice.


alright ;->
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
While deploying application on AIX I got this error. while it's working fine on windows Sam Java 0 06-21-2006 06:26 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
while 1 vs while True Timothy Fitz Python 16 12-15-2004 09:27 PM
Help while error "Error while trying to run project:" David ASP .Net 1 07-19-2004 08:41 PM
Got error msg while Debugging : Error while trying to run project: ... ^CrazyCoder^ ASP .Net 3 09-15-2003 09:40 AM



Advertisments