in reply to need nextline() sub

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.

Replies are listed 'Best First'.
Re: (Ovid) Re: need nextline() sub
by mutagen (Novice) on Nov 22, 2001 at 06:04 UTC
    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