Hi,

I have been reading "Learning Perl the Hard Way" and I was trying to complete one of the exercises which involved the use of regular expressions. I could get all of the anchors working except for "$". I have my code below with errors. I was just curious if someone could take a look at it for me. Thank you for your time--rgpii

#!/usr/bin/perl
use strict;
use warnings;
my $pattern = "(g$)";
my @fileargs = @ARGV;
foreach my $argument  (@fileargs)
        {       print "File Handle--> $argument\n";
                my $fhandle = $argument;
                open FILE,$fhandle;
                while (my $line = <FILE>)
                {       print "Printing Line --> $line\n";
                        if($line =~ m/$pattern/)
                        {
                        print "Pattern Matched!--> $line\n";
                        }
                }
        }

Here are my errors:

Final $ should be \$ or $name at ./grep.plx line 4, within string
syntax error at ./grep.plx line 4, near "= "(g)$""

my $pattern = "(g$)";

Here '$)' is perl special variable gives the effective group id of the process.
So finally $pattern gets '(g0'. It's not a parsed regular expression.
I assumed your try to match the letter 'g' at end of the line. So modify the below line in your code.

my $pattern = '(g$)';

Here the string assigned with in single quote. Now $pattern gets (g$). Its a parsed regular expression. So you able match your string end with 'g'.

For more information about single quoted string & double quoted string read the 'variable interpolation' concept.

commented: good comment +0

Yes ,$) is special variable.If u print "$)" you will get 0 as the output.So single quotes is used

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.