Should I read ARGV in my own perl module

My Perl extracts and processes data from (several) log files, it currently processes all the files in @ARGV.

The most important part of this script is the log decoding , it contains a lot of knowledge about the log file format. This transforming part from the log (actually into a hash array) turned out to be subject to change (as the log format develops) and will be the basis for further processing steps: often questions arise that can answer decoded records that are best suited for Perl, so I think make it a module.

The main function is the use of nested (or named in it) matching patterns sitting in a loop while (<>):

while (<ARGV>) {
    $totalLines ++;
    if (m/^(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d) L(\d) (.+)/) {
        my $time = $1;
        my $line = $2;
        my $event = $3;
        if ($event =~ m/^connect: (.+)$/) {
            $pendings{$line}{station} = $1;
            ...

... there are more than 200 lines in front of the closing bracket.

I have the feeling that just reading from ARGVwill exceed. Do one thing and do it well . When I searched on the Internet, I did not find anything that speaks explicitly or against reading from ARGVin the module, but maybe my search patterns were just bad. [1] [2]

(How) should I reformat my decoding to place it in the module?
... or do I need to change my feelings about this?


[1] perltrap - perldoc.perl.org
[2] perlmodstyle - perldoc.perl.org

+4
2

<ARGV>,

sub foo {
    my ($iter) = @_;

    # `defined()` should be used explicitly unlike `while (<ARGV>)`
    while (defined (my $line = $iter->())) {
        # if ..
    }
}

foo(sub{ scalar <ARGV> }); # force scalar context; one line/record per call
+4

, . \*ARGV .

, $_. $_ ( ) ( ). (, , local *_;).

+3

Source: https://habr.com/ru/post/1672468/


All Articles