while() { if(/matches/) # use of $_, everything is ok { $object->doSomething($part_of_line); } print "$_\n"; # here $_ has a value of undef and causes problems } #### sub doSomething { ... open(FILE2, $file2); while() { # do more stuff } # At this point in the code, $_ is undef and since $_ is # global... }