NAME

App::linerange - Retrieve line ranges from a filehandle

VERSION

This document describes version 0.005 of App::linerange (from Perl distribution App-linerange), released on 2020-10-11.

FUNCTIONS

linerange

Usage:

linerange(%args) -> [status, msg, payload, meta]

Retrieve line ranges from a filehandle.

Examples:

  • By default, if spec is empty, get all lines:

    linerange();
  • Get every other lines:

    linerange(spec => "/2");
  • Get lines 1-10:

    linerange(spec => "1-10");
  • Get lines 1 to 10, .. is synonym for -:

    linerange(spec => "1 .. 10");
  • Get lines 1-10 as well as 21-30:

    linerange(spec => "1-10, 21 - 30");
  • You can specify negative number, get the 5th line until 2nd last:

    linerange(spec => "5 .. -2");
  • You can specify negative number, get the 10th last until last:

    linerange(spec => "-10 .. -1");
  • Instead of N1-N2, you can use N1+M to mean N1-(N1+M), get 3rd line:

    linerange(spec => "3+0");
  • Instead of N1-N2, you can use N1+M to mean N1-(N1+M), get 3rd to 5th line:

    linerange(spec => "3+2");
  • Instead of N1-N2, you can use N1+M to mean N1-(N1+M), get 3rd last to last line:

    linerange(spec => "-3+2");

The routine performs a single pass on the filehandle, retrieving specified line ranges.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • fh* => filehandle

  • spec => str (default: "")

    Line range specification.

    A comma-separated list of empty strings ("", which means all lines), specific line numbers ("N") or line ranges ("N1..N2" or "N1-N2", or "N1+M" which means N2 is set to N1+M), where N, N1, and N2 are line number specification. Line number begins at 1; it can also be a negative integer (-1 means the last line, -2 means second last, and so on). N1..N2 is the same as N2..N1. Each line or range can optionally be followed by "/M" to mean every M'th line (where M is an integer starting from 1).

    Examples:

    • 3 (third line)

    • 1..5 (first to fifth line)

    • 3+0 (third line)

    • 3+1 (third to fourth line)

    • -3+1 (third last to second last)

    • 5..1 (first to fifth line)

    • -5..-1 (fifth last to last line)

    • -1..-5 (fifth last to last line)

    • 5..-3 (fifth line to third last)

    • -3..5 (fifth line to third last)

    • /3 (every 3rd line, i.e. 3, 6, 9, ...)

    • /2 (every other line, i.e. 2, 4, 6, ...)

    • 2..-1/3 (every 3rd line starting from line 2, i.e. 4, 7, 10, ...)

Returns an enveloped result (an array).

First element (status) is an integer containing HTTP status code (200 means OK, 4xx caller error, 5xx function error). Second element (msg) is a string containing error message, or 'OK' if status is 200. Third element (payload) is optional, the actual result. Fourth element (meta) is called result metadata and is optional, a hash that contains extra information.

Return value: (any)

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/App-linerange.

SOURCE

Source repository is at https://github.com/perlancar/perl-App-linerange.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=App-linerange

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2020, 2019 by perlancar@cpan.org.

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