in reply to why is this code bad under -w option?

Use of uninitialized value in string eq at sourcefile, line 32.

'Use of uninitialized value' means that there's a variable that has no value (is undef) when that's not appropriate. Without -w, undef is usually converted to '' as necessary, but with -w, it complains.

Where is the complaint? Line 32 of your source code. Go there and see what you see. It might be an approximation, given the loose syntax of statements that take up multiple lines.

What operator is complaining? 'string eq'. One side of the 'eq' is undef. $block_type eq 'KB' You should wager it's the side that is a variable, since the constant is never undef.

What should you do about it? Go back and look at where you last modified the variable in question. What if $1 is undefined? What should you do?

$block_size =~ m{([a-zA-Z]{2}$)}; $block_type = $1 || '';

Your pattern captures any two adjacent letters at the end of the string. If there are not two adjacent letters at the end of the string, then $1 is undef. The above uses $1 if defined, or an empty string '' otherwise. You might decide to fall back on 'KB' as a default instead of an empty (invalid) block type.

--
[ e d @ h a l l e y . c c ]

Replies are listed 'Best First'.
Re: Re: why is this code bad under use strict?
by Grygonos (Chaplain) on Jul 28, 2003 at 14:03 UTC
    This code now generates no warnings... thanks for all your insight.. I thought it was griping because I used a quoted string for comparisons. I think this code has much better error checking after some insight from fellow monks... thanks for all your help. You guys are amazing as per usual.
    #!/Perl/bin/perl -w use strict; use IO::File; use constant{KB => 2**10, MB => 2**20, GB => 2**30}; print "Enter the pathname and filename of the file to split: "; my $filename = <STDIN>; #Format filename chomp($filename); $filename =~ s{\\}{\\\\}g; #Declare variables for use in until loop my $valid_block_type= 0; my $block_size = 0; #Generate a valid block size number until($valid_block_type) { #Declare local variables my $block_quantity = 0; my $block_type = 'B'; print "Enter the block size \n (example: 400KB,10MB,1GB): "; $block_size = <STDIN>; chomp($block_size); if($block_size =~ m{(^[0-9]+)}) { $block_quantity = $1; } if($block_size =~ m{([a-zA-Z]{2}$)}) { $block_type = $1; } BLOCK_TYPE: { $block_type eq 'KB' && do{$block_size = $block_quantity * KB; + $valid_block_type = 1; last BLOCK_TYPE;}; $block_type eq 'MB' && do{$block_size = $block_quantity * MB; $valid_block_type = 1; last BLOCK_TYPE;}; $block_type eq 'GB' && do{$block_size = $block_quantity * GB; $valid_block_type = 1; last BLOCK_TYPE;}; DEFAULT_BLOCK_TYPE: {print "Invalid block type specified!\n\n" +;} } if($block_quantity == 0) { print "Invalid block size specified!\n\n"; $valid_block_type = 0; } }