Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3840842
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:34:35+00:00 2026-05-19T15:34:35+00:00

This question is similar to my last one , with one difference to make

  • 0

This question is similar to my last one, with one difference to make the toy script more similar to my actual one.

Here is the toy script, replace.pl (Edit: now with ‘use strict;’, etc)

#! /usr/bin/perl -w

use strict;

open(REPL, "<", $ARGV[0]) or die "Couldn't open $ARGV[0]: $!!";
my %replacements;
while(<REPL>) {
   chomp;
   my ($orig, $new, @rest) = split /,/;
   # Processing+sanitizing of orig/new here
   $replacements{$orig} = $new;
}
close(REPL) or die "Couldn't close '$ARGV[0]': $!";

print "Performing the following replacements\n";
while(my ($k,$v) = each %replacements) {
   print "\t$k => $v\n";
}

open(IN, "<", $ARGV[1]) or die "Couldn't open $ARGV[1]: $!!";
while ( <IN> ) {
   while(my ($k,$v) = each %replacements) {
      s/$k/$v/gee;
   }
   print;
}
close(IN) or die "Couldn't close '$ARGV[1]': $!";

So, now lets say I have two files, replacements.txt (using the best answer from the last question, plus a replacement pair that doesn’t use substitution):

(f)oo,q($1."ar")
cat,hacker

and test.txt:

foo
cat

When I run perl replace.pl replacements.txt test.txt I would like the output to be

far
hacker

but instead it’s ‘$1."ar"‘ (too much escaping) but the results are anything but (even with the other suggestions from that answer for the replacement string). The foo turns into ar, and the cat/hacker is eval’d to the empty string, it seems.

So, what changes do I need to make to replace.pl and/or replacements.txt? Other people will be creating the replacements.txt’s, so I’d like to make that file as simple as possible (although I acknowledge that I’m opening the regex can of worms on them).

If this isn’t possible to do in one step, I’ll use macros to enumerate all possible replacement pairs for this particular file, and hope the issue doesn’t come up again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:34:36+00:00Added an answer on May 19, 2026 at 3:34 pm

    Please don’t give us non-working toy scripts that don’t use strict and warnings. Because one of the first things people will do in debugging is to turn those on, and you’ve just caused work.

    Second tip, use the 3-argument version of open rather than the 2-argument version. It is safer. Also in your error checking do as perlstyle says (see http://perldoc.perl.org/perlstyle.html for the full advice) and include the file name and $!.

    Anyways your problem is that the code you were including was q($1.”ar”). When executed this returns the string $1.”ar”. Get rid of the q() and it works fine. BUT it causes warnings. That can be fixed by moving the quoting into the replace script, and out of the original script.

    Here is a fixed script for you:

    #! /usr/bin/perl -w
    use strict;
    
    open(REPL, "<", $ARGV[0]) or die "Couldn't open '$ARGV[0]': $!!";
    my %replacements;
    while(<REPL>) {
       chomp;
       my ($orig, $new) = split /,/;
       # Processing+sanitizing of orig/new here
       $replacements{$orig} = '"' . $new . '"';
    }
    close(REPL) or die "Couldn't close '$ARGV[0]': $!";
    
    print "Performing the following replacements\n";
    while(my ($k,$v) = each %replacements) {
       print "\t$k => $v\n";
    }
    
    open(IN, "<", $ARGV[1]) or die "Couldn't open '$ARGV[1]': $!!";
    while ( <IN> ) {
       while(my($k,$v) = each %replacements) {
          s/$k/$v/gee;
       }
       print;
    }
    close(IN) or die "Couldn't close '$ARGV[1]': $!";
    

    And the modified replacements.txt is:

    (f)oo,${1}ar
    cat,hacker
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is similar to this other one , with the difference that the
My question is similar to this one , but more complicated. I am trying
This is similar to this question I asked last week. My dataGrid is populated
This question is similar to this one How do I add options to a
My Question is similar to This SO question , but with the main difference
This is similar to my last question, however I feel it has to do
I have a question similar to the one here: Event handlers inside a Javascript
I've asked a question similar to this but thought I would ask a more
I'm having a similar problem to the one described on this question . However,
This question is similar to Getting Emacs fill-paragraph to play nice with javadoc-like comments

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.