use warnings; use strict; my $search = 'blah'; open my $fh, '<', 'file.txt' or die $!; my @lines = ( "this is line 1 of aref\n", "this is line 2 of aref blah\n", ); parse_line($fh); parse_line(\@lines); sub parse_line { my $arg = shift; if (ref $arg eq 'GLOB'){ # file handle while (<$arg>){ if (/$search/){ print; last; } } } elsif (ref $arg eq 'ARRAY'){ # aref for (@$arg){ if (/$search/){ print "$_\n"; last; } } } else { print "unsupported type...\n"; exit; } }