I think when you include the parentheses in your pattern $1 is being set to the 
value inside the partentheses.  It's as if you typed:
 
    if(/(dir)ectory/gi)
 
which will set $1 to 'dir', which is probably not what you want.
 
I think you can avoid this if you change the line to:
 
    if(/($pattern)/gi)
 

        -----Original Message----- 
        From: Adriano Allora [mailto:[EMAIL PROTECTED] 
        Sent: Mon 1/2/2006 3:48 AM 
        To: beginners@perl.org 
        Cc: 
        Subject: about eval and stdin
        
        

        hi to all,
        a friend of mine ask me for a perl script to change regexp patterns in
        some texts (he can learn regexp, but I suppose he won't learn perl). So
        I start write this one to him.
        I have a problem:
        
        ==> with pattern = (dir)ectory and replacement = $1, why the script
        does not eval $1 as "dir"? how I can change the script to make it works
        as I want?
        
        
        #!/usr/bin/perl -w
        $^I ='_old';
        print "pattern: ";
        $pattern = <STDIN>;
        chomp($pattern);
        print "replacement: ";
        $replacement = <STDIN>;
        chomp($replacement);
        print "\n print \"s\" to confirm substitution; print \"n\" to avoid it
        print \"n\" \n\n";
        while(<>)
                {
                $choice = "";
                if(/$pattern/gi)
                        {
                        eval $replacement if $replacement eq "$1";
                        print STDOUT "I'd subsitute this one => ". $_;
                        $choice = <STDIN>;
                        chomp($choice);
                        }
                 s/$pattern/$replacement/gi if $choice eq "s";
                print;
                }
        
        thank you all,
        
        alladr
        
        

Reply via email to