my @mat1 = readmatrix ($first); my ($r1, $c1) = ($rows, $cols); ... sub readmatrix { my ($file) = @_; open(IN, "$file") || die "can't open file: $!\n"; while (defined (my $line = )) { my @tmp = split(' ', $line); $cols = scalar @tmp; $rows++; push (@AoA, [@tmp]); } close IN; }