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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:59:11+00:00 2026-05-10T23:59:11+00:00

Please note – I am not looking for the right way to open/read a

  • 0

Please note – I am not looking for the ‘right’ way to open/read a file, or the way I should open/read a file every single time. I am just interested to find out what way most people use, and maybe learn a few new methods at the same time :)*

A very common block of code in my Perl programs is opening a file and reading or writing to it. I have seen so many ways of doing this, and my style on performing this task has changed over the years a few times. I’m just wondering what the best (if there is a best way) method is to do this?

I used to open a file like this:

my $input_file = '/path/to/my/file'; open INPUT_FILE, '<$input_file'  || die 'Can't open $input_file: $!\n'; 

But I think that has problems with error trapping.

Adding a parenthesis seems to fix the error trapping:

open (INPUT_FILE, '<$input_file')  || die 'Can't open $input_file: $!\n'; 

I know you can also assign a filehandle to a variable, so instead of using ‘INPUT_FILE’ like I did above, I could have used $input_filehandle – is that way better?

For reading a file, if it is small, is there anything wrong with globbing, like this?

my @array = <INPUT_FILE>; 

or

my $file_contents = join( '\n', <INPUT_FILE> ); 

or should you always loop through, like this:

my @array; while (<INPUT_FILE>) {   push(@array, $_); } 

I know there are so many ways to accomplish things in perl, I’m just wondering if there are preferred/standard methods of opening and reading in a file?

  • 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. 2026-05-10T23:59:12+00:00Added an answer on May 10, 2026 at 11:59 pm

    There are no universal standards, but there are reasons to prefer one or another. My preferred form is this:

    open( my $input_fh, '<', $input_file ) || die 'Can't open $input_file: $!'; 

    The reasons are:

    • You report errors immediately. (Replace ‘die’ with ‘warn’ if that’s what you want.)
    • Your filehandle is now reference-counted, so once you’re not using it it will be automatically closed. If you use the global name INPUT_FILEHANDLE, then you have to close the file manually or it will stay open until the program exits.
    • The read-mode indicator ‘<‘ is separated from the $input_file, increasing readability.

    The following is great if the file is small and you know you want all lines:

    my @lines = <$input_fh>; 

    You can even do this, if you need to process all lines as a single string:

    my $text = join('', <$input_fh>); 

    For long files you will want to iterate over lines with while, or use read.

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

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You need to properly escape the text. You don't say… May 11, 2026 at 2:40 pm
  • added an answer Here are some examples of doing a basic html email… May 11, 2026 at 2:40 pm
  • added an answer Yep, as I thought, me being a Friday n00b. Didn't… May 11, 2026 at 2:40 pm

Related Questions

Please note the Edit below for a lot more information, and a possible solution
Please note - I am not looking for the right way to open/read a
Please note: In each step I describe below I'm logged in as the same
Please note: by a pure function, I don't mean pure virtual I'm referring to
Please note the question is about using an asynchronous callback mode only on sockets

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.