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 3722454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:54:38+00:00 2026-05-19T05:54:38+00:00

In Perl, I’m trying to read a file line by line and process each

  • 0

In Perl, I’m trying to read a file line by line and process each line, modifying it as required.
So far, the only way I’m reading to be able to do this is read the file into an array, modify each element of the array as needed, then when it’s finished, output it back to the file.

Is there a better way to do this, perhaps some way I can replace single lines as I go along?

Right now, my processing code looks like this:

while (my $line = <FILE>)
{
    # process line here
    # ...........

    print FILE $line;
}

My code seems to be very close, except that it’s replacing one line after the line I’m currently in, so it seems that if I could back the file pointer up by one line, it would write to the correct place in the file.

Am I on the right track? What would I need to do from here to back up the file pointer so it writes to the same line I’m reading from?


Edit:

Out of the answers I received, both using local $^I and Tie::File worked nicely.
I ended up going with Tie::File so I wouldn’t have to print out every line of the file. This way if something happens midway through the script, my file won’t be messed up.

My new code looks like this:

use Tie::File;

chomp(my $filename = $ARGV[0]);
tie my @array, 'Tie::File', $filename or die $!;

foreach my $line(@array)
{
    # ...... line processing happens here .......
    # ...... $line is automatically written to file if $line is changed .......
}
  • 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-19T05:54:39+00:00Added an answer on May 19, 2026 at 5:54 am

    I don’t think it’s a good idea to read from a file and write to it at the same time like you do.

    You could use Tie::File. It ties the lines of a file to an array. You can modify the array as you need which in turn modifies the file transparently in the background.

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

Sidebar

Related Questions

Perl question for you: #!/usr/bin/perl use File::Find; #Find files find(\&wanted, $dir); sub wanted {
Perl automatically initializes variables to undef by default. Is there a way to override
Perl has a conditional operator that is the same a C's conditional operator .
Perl newbie here...looking for help to reformat a datafile. Data looks like this: num:3460381591
perl script.pl --f1=t1 --f2=t2 --f3=t4 --f4 < /home/joe/a.txt script.pl use Getopt::Long; my ($f1, $f2,
Perl has the \u operator to lowercase a match when using string replacement and
Perl has long been my choice scripting language but I've run into a horrible
In Perl, how would one efficiently parse the output of unix's date command, taking
In perl, I want to check if the given variable holds a floating point
In Perl, by using Template Toolkit, here is what I do Perl my $vars

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.