Perl functions always returns a value.
Either explicitely by  calling return or 
the result of the last computation.
examples/subroutines/return_value.pl
#!/usr/bin/perl

sub loop {
    $i = 0;
    while ($i < 10) {
       $i++;
    }
}
print loop();            # what is this value ?
print $i;                # 10

Solving the above problem: always use return
Add    return $i;   before the end of the subroutine.