Developus
Developus

Reputation: 1462

"Use of uninitialized value $ in concatenation (.) or string..." error even when values are initialized

I have a problem with initialized values. I have a script which has method:

sub compare_with_terminal {
    use Term::Size::Any qw( chars pixels );
    my $columns = 0; 
    my $rows = 0;
    ($columns, $rows) = chars *STDOUT{IO};  

    print "Columns: ".$columns."\n"; #line 73
    print "Rows: ". $rows."\n";

    if($height > $columns || $width > $rows){
        print "Bigger than terminal: ";
        my $option = <>;
        chomp($option);
        if($option eq "N"){
            print "End.";
            exit 0;
        }
    }
} 

I created and initialized two values $columns and $rows. But, when I run this script, I get:

Use of uninitialized value $columns in concatenation (.) or string at ./perl/script.pl line 73 (#1) (W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables.

$rows has same error.

These errors say that I didn't initialize variable $columns, but I did. Anyone can explain me and try to solve this problem?

Upvotes: 0

Views: 1452

Answers (1)

toolic
toolic

Reputation: 62236

There is a bug report for Term::Size::Any

Using char without any arguments returns values for me on linux (Term::Size::Any version 0.002):

use warnings;
use strict;

use Term::Size::Any qw(chars);
my ($columns, $rows) = chars();
print "$columns $rows\n";

__END__

80 24

You initialized $column to 0, but it is overwritten when you call the chars function.

Upvotes: 4

Related Questions