develooper Front page | perl.beginners | Postings from August 2012

Unexpected token error when providing a regular expression as input

Thread Next
From:
moijes12
Date:
August 1, 2012 08:54
Subject:
Unexpected token error when providing a regular expression as input
Message ID:
3480de5d-3829-47c9-bc53-7b3752fd081f@t9g2000yqg.googlegroups.com
Hi

My program takes is supposed to take regular expression and a file
containing a list of words/lines as input and print all those lines
that match the regular expression. Below is the program


#!/usr/bin/perl

use strict;
use warnings;

sub grep_file {
    my $pattern = shift;
    my @file_names = @_;

    for my $file_name (@file_names) {
        open my ($file_handle), $file_name;
        while(my $line = <$file_handle>) {
            if ($line =~ m/$pattern/) {
                print $line;
            }
         }
         close $file_handle;
    }
}

grep_file @ARGV;

When I execute this program from the command line by running "./
grep.pl  (.es.){3}  /usr/share/dict/words" , I get the error
bash: syntax error near unexpected token `.es.'

But, if execute it as "./grep.pl  \(.es.\){3}  /usr/share/dict/
words" , I get the expected result.

Please help me understand this.


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About