Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: What's the right way to write a method which returns one line at a time from a file?

by haukex (Archbishop)
on Nov 29, 2020 at 12:16 UTC ( [id://11124359]=note: print w/replies, xml ) Need Help??


in reply to Re^2: What's the right way to write a method which returns one line at a time from a file?
in thread What's the right way to write a method which returns one line at a time from a file?

I instantiate the module without naming the file, then call get_lines with the file name. It doesn't need to be passed as an argument to get_filehandle because it's already there.

Yes, you're right, because you call get_filehandle inside of get_lines, it actually could make sense to pass the filename to the get_lines call. However, I think it could potentially still be confusing because with the code you showed, if all the user uses is get_lines, it'll only ever open one file - say I call my $x = $obj->get_lines("foo.txt"), and then my $y = $obj->get_lines("bar.txt"), now $y contains the second line of foo.txt. So that's why it might be better to separate the two actions - opening the file and reading from it - into two methods.

Update: The reason I questioned whether it makes sense to pass a filename to get_filehandle is that I was imagining $self->{file} to be an object property that might deserve its own setter, but that's not as important. BTW, you might want to consider renaming get_filehandle to something like open_file to make it more clear what the method is doing.

  • Comment on Re^3: What's the right way to write a method which returns one line at a time from a file?
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124359]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-25 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found