Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 79 lines (56 sloc) 1.967 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
NAME
    File::fgets - Read either one line or X characters from a file

SYNOPSIS
      use File::fgets;

      open my $fh, $file;

      # Read either one line or the first 10 characters, which ever comes first
      my $line = fgets($fh, 10);

DESCRIPTION
    An implementation of the C fgets() function.

    fgets
        my $string = fgets($fh, $limit);

    Reads either one line or at most $limit bytes from the $fh.

    Returns undef at end of file.

    NOTE: unlike C's fgets, this will read $limit characters not $limit - 1.
    Perl doesn't have to leave room for a null byte.

EXAMPLE
    The following example demonstrates using fgets() to read in at most 5
    characters at a time.

      use File::fgets;

      open my $write_fh, ">", $file;
      print $write_fh <<END;
      this is
      an example
      of use
      END
      close $write_fh;

      open my $fh, "<", $file;
      while( my $string = fgets($fh, 5) ) {
          $string =~ s{\n}{\\n}; # make newlines show up
          print "--$string--\n";
      }

    The result will be:

      --this --
      --is\n--
      --an ex--
      --ample--
      --\n--
      --of us--
      --e\n--

NOTES
    This is implemented as a wrapper around the C fgets() function and is
    extremely efficient UNLESS the filehandle does not have an underlying
    fileno. For example, if its given a tied filehandle. Then it falls back
    to a Perl implementation.

LICENSE
    Copyright 2010 by Michael G Schwern <schwern@pobox.com>.

    This program is free software; you can redistribute it and/or modify it
    under the same terms as Perl itself.

    See http://www.perl.com/perl/misc/Artistic.html

    Send bugs, feedback, ideas and suggestions via
    https://rt.cpan.org/Public/Dist/Display.html?Name=File-fgets or
    <bugs-File-fgets@rt.cpan.org>

    The latest version of this software can be found at
    http://github.com/schwern/File-fgets

SEE ALSO
    File::GetLineMaxLength

Something went wrong with that request. Please try again.