Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

need nextline() sub

by mutagen (Novice)
on Nov 22, 2001 at 05:18 UTC ( #126916=perlquestion: print w/replies, xml ) Need Help??

mutagen has asked for the wisdom of the Perl Monks concerning the following question:

I have a situation where the traditional
for (FILEHANDLE) { # do stuff }
is not appropriate. I need to abstract the filehandle away in module, so that code using this module can just call a sub called nextline() that returns a line and advances the file pointer to the next \n. I looked at seek(), and I can understand how it's used to move the file pointer, but I don't get how I can return a line's worth of characters each time. thanks!

Replies are listed 'Best First'.
Re: need nextline() sub
by Zaxo (Archbishop) on Nov 22, 2001 at 05:36 UTC

    perldoc -f readline

    After Compline,
    Zaxo

      Exactly, thank you :)
(Ovid) Re: need nextline() sub
by Ovid (Cardinal) on Nov 22, 2001 at 05:42 UTC

    If I read what you wrote correctly, you want to pass a filename to a sub in another module and then later, be able to read the file one line at a time, right? If so, I think this might work:

    package Foo; use strict; my $sep; sub set_file { my $file = shift; $sep = shift || "\n"; open FH, "< $file" or die "Cannot open $file for reading: $!"; } sub get_line { local $/ = $sep; my $temp = <FH>; return $temp; } 1;

    In your main program:

    use strict; use Foo; Foo::set_file( $some_file ); for ( 0..10 ) { print Foo::get_line };

    If that's not what you're looking for, let us know. It seems to me that seek is not what you're needing here.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Zaxo was on the money with readline() - all i needed was to get underneath the abstraction of the <FH> operator.
      package FileClerk; open (FH,$file); sub next_line { return (readline(*FH)); }
      calling program:
      use FileClerk; for (FileClerk::next_line()) { print "$_\n"; }
      The calling program doesn't need to care about the filehandle, which was my aim. Thanks everyone.

        Zaxo was on the money with readline() - all i needed was to get underneath the abstraction of the <FH> operator.

        The spaceship operator (<>) is just a wrap around the readline function. You can get the same behaviour like this:

        sub next_line { return scalar <FH>; }

        Another remark, it might be worth setting $/ before using readline. That way you don't get unexpected behaviour if someone fiddled with it. So put something like this at the start of your sub:

        local $/ = "\n";

        -- Hofmator

Re: need nextline() sub
by Kanji (Parson) on Nov 22, 2001 at 05:36 UTC

    You might want to consider using or subclassing IO::File (alt.), which already does something similar ($fh->getline).

        --k.


Re: need nextline() sub
by kwoff (Friar) on Nov 22, 2001 at 05:30 UTC
    First approach, you could just read one byte at a time. Then when you find a "\n", voila you have a line.

    Then you might start thinking about more efficient ways to do it if that wasn't good enough. Try to read in an 8k buffer, then parse that buffer.

    I'm still not sure why you couldn't use <FILEHANDLE>, though. You're talking about using seek, and that uses a FILEHANDLE too.

Re: need nextline() sub
by broquaint (Abbot) on Nov 30, 2001 at 17:31 UTC
    This sounds like the perfect situation for a closure
    use strict; sub nextline { my $fh = shift; return sub { return scalar <$fh>; } } open(FOO, "somefile.txt") or die("Doh - $!"); my $nl = nextline(*FOO{IO}); print $nl->() for 0..3; close(FOO); __END__ =INPUT foo bar baz quux xxx yy z =OUTPUT foo bar baz quux
    Now we have a fly-weight object (tilly's words I think) which will return the next line from the filehandle you initially provided to nextline(). There's loads of info about closures around the Monastery that are well worth the read if you're unsure about them (although having read a bunch of stuff on closures I only clicked recently having read tye's node on Finding all Combinations). You can also do funky stuff like grokking the symbol table and dynamically creating subs, but I think that's better left to a later node.
    HTH

    broquaint

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2022-08-14 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?