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

  • Home
  • SEARCH
  • 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 8516967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:34:10+00:00 2026-06-11T05:34:10+00:00

My Question: If I’m given C:\text_file.txt as a parameter on the command line when

  • 0

My Question: If I’m given C:\text_file.txt as a parameter on the command line when my PERL script is called and the text_file.txt contains the text “Hello World World World”, how do I replace all instances of “World” with “Earth”?

I’m new to PERL. I’m writing a script which accepts a parameter of a filepath. I want to do a find replace on the contents of the file. I know I could do something like this: $string =~ s/World/Earth/g; but I don’t want to read the file into a string if I can help it. Is there a way to do this directly on the file without reading it in as a string? Thanks!

  • 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-06-11T05:34:12+00:00Added an answer on June 11, 2026 at 5:34 am

    This is what Perl’s -i (“inplace edit”) switch is for:

    $ perl -i.bak -pe 's/World/Earth/g' text_file.txt
    

    EDIT: To incorporate this identical functionality into a larger Perl script:

    {
        local $^I = '.bak';
        local @ARGV = ('text_file.txt');
        local $_;
        while (<>) {
            s/World/Earth/g;
            print;
        }
    }
    

    The $^I variable reflects the value of the -i command-line switch; here, it’s being set manually. Also, @ARGV is being manually set to the list of files that would ordinarily be taken from the command line. The whole thing is wrapped in a block so that $^I and @ARGV, set with local, resume their original values afterwards; this may not be strictly necessary, depending on the rest of the program. I also localized $_ just to keep the while loop from clobbering the value it previously held; this also may not be necessary.

    Note that you can use just -i instead of -i.bak from the command line if you want to avoid creating a backup file. The corresponding code would be $^I = "" instead of $^I = '.bak'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question is quite simple. Let's say I have an URL config with line: url(r'^models/(?P<model_group_id>[0-9]+)/(?P<page>\d+)/$',
Question: Given a Font object, how do I draw all the glyphs/symbols/characters of the
question origin Given an unsorted array of size n containing objects with ids of
Question: Considering the hello-gl2 example from Android NDK r6b, is this example correct when
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Question: What's the best approach to grab text from .Net UI controls in a
Question Is it possible to stop a Message Driven Bean (programmatically), so that it
Question context: let say that there is some really important row in config/locales/en.yml that
Question from Object-Oriented JavaScript book: Imagine Array() doesn't exist and the array literal notation
QUESTION: What am I missing or doing wrong? I'm trying to migrate fully functional

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.