0
votes

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?

1
What's line 73? (I'm guessing you haven't shown it, because you don't use $columns in a string anywhere in your snippet) - ThisSuitIsBlackNot
I read console size and write it into $columns and $rows variables. They are numbers I hope, not a strings. On windows everything works fine and I don't get any errors. - Developus
@allocer Again, please show which line is line 73, since that's the one the warning mentioned. - ThisSuitIsBlackNot
Re "These errors say that I didn't initialize variable $columns", If you actually read the message, it doesn't say that at all. It says $columns has an undefined value, not that it wasn't "initialized". (In fact, it's impossible not to initialize in Perl. Scalars are automatically initialized to undef, and arrays and hashes are initialized empty.) - ikegami
@allocer In the future, please copy and paste the exact code you're actually running or create a minimal reproducible example. Otherwise it makes it harder for us to find your issue. - ThisSuitIsBlackNot

1 Answers

4
votes

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.