sub get_num_lines_method1 { my ($file_name) = @_; my $num_lines = 0; local *FILE; open(FILE, $file_name); $num_lines++ while (); close(FILE); return $num_lines; } sub get_num_lines_method2 { # Really same as get_num_lines_method1. my ($file_name) = @_; my $num_lines = 0; local *FILE; open(FILE, $file_name); while () { $num_lines++; } close(FILE); return $num_lines; } sub get_num_lines_method3 { # Loads the whole file into an array. my ($file_name) = @_; local *FILE; open(FILE, $file_name); my @file = ; close(FILE); return scalar(@file); } sub get_num_lines_method4 { # Loads the whole file into a scalar. my ($file_name) = @_; local *FILE; open(FILE, $file_name); local $/; my $file = ; close(FILE); return $file =~ tr/\n/\n/; } printf("Method 1: %d\n", get_num_lines_method1($0)); printf("Method 2: %d\n", get_num_lines_method2($0)); printf("Method 3: %d\n", get_num_lines_method3($0)); printf("Method 4: %d\n", get_num_lines_method4($0)); __END__ output: ======= Method 1: 62 Method 2: 62 Method 3: 62 Method 4: 62