Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

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


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

while(my $line = $reader->get_next_line()){

I just wanted to point out that this suffers from the same issue that AnomalousMonk correctly pointed out deeper in the thread: if the file ends on a line containing just "0" with no newline, this loop won't catch that. You would have to say while( defined( my $line = $reader->get_next_line() ) ) instead. Alternatively, note it's possible to overload the <> operator (note that overloaded <> in list context wasn't implemented until 5.18). See for example my use in Algorithm::Odometer::Tiny; you'd just have to change $self->() to your method call, and then you could write while( my $line = <$reader> ) and Perl will automatically add the defined call.

By the way, in your post here, I don't understand the point of the two $self->{file} = shift; lines, especially the second one? Why change the filename while reading the file?

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

Replies are listed 'Best First'.
Re^2: What's the right way to write a method which returns one line at a time from a file?
by Cody Fendant (Hermit) on Nov 28, 2020 at 03:26 UTC
    By the way, in your post here, I don't understand the point of the two $self->{file} = shift; lines, especially the second one? Why change the filename while reading the file?

    Looking at it, you're right, but I think the second one, if you mean the one further down the page, is the one that needs to exist and the first one is the one which doesn't.

    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.

    My brain was having a very bad day as you can probably tell.

      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-24 23:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found