sub num_month{ my $date = shift; ... foreach ...{ my $date = ... # <----this is a new $date scalar, which does not exist outside of this foreach loop } return $date; #<----this is the $date from before the foreach loop -- ie, exactly what was passed to the sub }