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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:13:22+00:00 2026-06-05T07:13:22+00:00

I am Newbie to Perl script. I want to do a read and write

  • 0

I am Newbie to Perl script.

I want to do a read and write operation on a file. I will open a file in read and write mode (+<), and will write into a file. Now, I want read the file whatever I have written to it previously. Below is my code:

#!/usr/bin/perl

`touch file.txt`; #Create a file as opening the file in +< mode
open (OUTFILE, "+<file.txt") or die "Can't open file : $!";

print OUTFILE "Hello, welcome to File handling operations in perl\n"; #write into the file

$line = <OUTFILE>; #read from the file

print "$line\n"; #display the read contents.

When I am displaying the read contents it’s showing a blank line. But the file “file.txt” has the data

Hello, welcome to File handling operations in perl

Why am I not able to read the contents. Whether my code is wrong or am I missing something.

  • 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-05T07:13:23+00:00Added an answer on June 5, 2026 at 7:13 am

    The problem is that your filehandle position is located after the line you have written. Use the seek function to move the “cursor” back to the top before reading again.

    An example, with some extra comments:

    #!/usr/bin/env perl
    
    # use some recommended safeguards
    use strict;
    use warnings;
    
    my $filename = 'file.txt';
    `touch $filename`;
    
    # use indirect filehandle, and 3 argument form of open
    open (my $handle, "+<", $filename) or die "Can't open file $filename : $!";
    # btw good job on checking open sucess!
    
    print $handle "Hello, welcome to File handling operations in perl\n";
    
    # seek back to the top of the file
    seek $handle, 0, 0;
    
    my $line = <$handle>;
    
    print "$line\n";
    

    If you will be doing lots of reading and writing you may want to try (and not everyone suggests it) using Tie::File which lets you treat a file like an array; line access by line number (newline written automatically).

    #!/usr/bin/env perl
    
    # use some recommended safeguards
    use strict;
    use warnings;
    
    use Tie::File;
    
    my $filename = 'file.txt';
    tie my @file, 'Tie::File', $filename
      or die "Can't open/tie file $filename : $!";
    
    # note file not emptied if it already exists
    
    push @file, "Hello, welcome to File handling operations in perl";
    push @file, "Some more stuff";
    
    print "$file[0]\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Perl newbie here. I have a log file that I need to parse out
I'm trying to write a perl script to search through a text file, find
I am newbie at Perl. I am trying to write a Perl script that
I am a Perl newbie, and i'm stuck with ths problem: I have a
Perl newbie here...I had help with this working perl script with some HASH code
After asking this perl newbie question , I have a perl newbie follow-up. I
I am very newbie to Perl. I wrote a very simple Perl program (script):
I am a total newbie with Python, I normally use Perl. I have an
I'm a newbie to perl and I found a script to convert a DNA
I am newbie to Perl. I need to parse a tab separated text file.

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.