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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:58:38+00:00 2026-06-17T16:58:38+00:00

I wanted to store a file handle in a Perl Object. Here is how

  • 0

I wanted to store a file handle in a Perl Object. Here is how I went about it.

sub openFiles {
     my $self = shift;
     open (my $itemsFile, "<", "items.txt")     or die $!;
     open (my $nameFile,  "<", "FullNames.txt") or die $!;      
     $self->{itemsFile} = $itemsFile;                   
     $self->{nameFile}  = $nameFile;
     return $self;
}

Then I’m looking to access some information from one of these files. Here is how I go about it.

sub getItemDescription {
     my $self = @_;
     chomp(my $record = $self->{itemsFile});
     return $record;
}

I attempt to access it in another procedure as follows:

print "Test 3: $self->getItemDescription()\n";

My questions are as follows:

  1. Is the way I’m saving the file handle in the object correct? If not, how is it wrong?
  2. Is the way I’m reading the lines of the file correct? If not, how can I get it right?
  3. Finally, is the way I’m printing the returned object correct?

This is really important to me. If there is any way that I can improve the structure of my code, i.e. making a global variable for file handling or changing the structure of the object, please let me know.

  • 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-17T16:58:39+00:00Added an answer on June 17, 2026 at 4:58 pm

    The openFiles piece is correct.

    The errors occur primarily getItemDescription method.

    First as previously mentioned my $self = @_; should be my ($self) = @_;.

    However, the crux of the question is solved in the following fashion:

    Change chomp(my $record = $self->{itemsFile}); to two lines:

    $file1 = $self->{itemsFile};

    chomp(my $record = $file1);

    To clarify you must (in my experience and I tried all the solutions suggested) use a scalar value.

    Finally, see the last two paragraphs in ikagami’s answer.

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

Sidebar

Related Questions

I am using scala 2.7.7, and wanted to parse CSV file and store the
I wanted to store 10 in 4th position of array of 5 positions. How
i have several classes with members called 'Id'. Originally i wanted to store these
i wanted to know where we should store the db in our project and
I needed to store various strings in a map but I wanted to keep
We have a multi-store set up and I wanted to change the template slightly
I am needing to store easily parsable data in a file as an alternative
I'm trying to use Sesame repository to store my ontology. I wanted to write
I need to write some data in a temporary file and to store this
I have a method that generates a PDF file using Reportlab library: def obtenerPDFNuevoPedido(self,

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.