Alex Rivera | Logout

Why does perl warn that open my $fh, $file is missing parentheses?

Asked 2011-04-08T14:51:58.923
15

It is my first day to perl, and I find this warning very confusing.

Parentheses missing around "my" list at ./grep.pl line 10.

It seems

open FILE, $file;

works fine.

What is wrong with

open my $fh, $file;

Thanks!

#!/usr/bin/perl

use strict;
use warnings;

sub grep_all {
        my $pattern = shift;

        while (my $file = shift) {
                open my $fh, $file;
                while (my $line = <$fh>) {
                        if ($line =~ m/$pattern/) {
                                print $line;
                        }   
                }   
        }   
}

grep_all @ARGV;
Edit
Report

1 Answer

0

You may be doing a school or learning project. But when I want to do something like this is perl, I will usually use this more concise version of your program.

perl -ne 'print if /your_regex/' your_file_list

For more info try

perldoc perlrun

and look for the explanations of -n and -p.

answered 2011-04-17T19:32:20.577

Your Answer