App-Unliner
view release on metacpan or search on metacpan
def main {
grep "GET /report.cgi" $@ | ip-extractor | tally | head -n 5
}
Now we can pass in a log file argument to our program (otherwise it will
read input from standard input):
$ unliner log-report access.log
Note that $@ escapes whitespace like bourne shell's "$@". Actually it
just passes the argument array untouched through to the process (grep in
this case) so the arguments can contain any characters. The bourne
equivalent of unquoted $@ and $* are not supported because they cause
way too many bugs (use templates if you need to do this).
We can parameterise other aspects of the unliner program too. For
example, suppose you wanted to control the number of lines that are
included in the report. To do this add a "prototype":
def main(head|h=i, junkarg=s) {
code. For example:
def body-size-extractor : perl {
while (<STDIN>) {
## body size is the last field in the log
print "$1\n" if /(\d+)$/;
}
}
This def could also have been written in sh, but dealing with shell
escapes is sometimes annoying:
def body-size-extractor {
perl -e 'while(<STDIN>) { ... }'
}
Def modifiers themselves sometimes take arguments. For example, perl
defs can take the "-n" switch which implicitly adds a loop (just like
the perl binary):
def body-size-extractor : perl -n {
lib/App/Unliner/Grammar/PostProc.pm view on Meta::CPAN
sub brace_block {
my $o = shift;
$o =~ s/^[{]|[}]$//g;
return $o;
}
sub arg {
my $o = shift;
## FIXME: handle \ escapes properly
$o =~ s/^'|'$//g if $o =~ /^'/;
$o =~ s/^"|"$//g if $o =~ /^"/;
return $o;
}
1;
lib/App/Unliner/Intro.pm view on Meta::CPAN
To fix this the arguments passed in to our log-report program are available in the variable C<$@>, just like in a shell script:
def main {
grep "GET /report.cgi" $@ | ip-extractor | tally | head -n 5
}
Now we can pass in a log file argument to our program (otherwise it will read input from standard input):
$ unliner log-report access.log
Note that $@ escapes whitespace like bourne shell's C<"$@">. Actually it just passes the argument array untouched through to the process (grep in this case) so the arguments can contain any characters. The bourne equivalent of unquoted C<$@> and C<$*...
We can parameterise other aspects of the unliner program too. For example, suppose you wanted to control the number of lines that are included in the report. To do this add a "prototype":
def main(head|h=i, junkarg=s) {
grep "GET /report.cgi" $@ | ip-extractor | tally | head -n $head
}
The prototype indicates that the main def requires arguments. Since the main def is the entry-point, these arguments must come from the command line:
$ unliner log-report access.log --head 5
lib/App/Unliner/Intro.pm view on Meta::CPAN
Def modifiers can be used to change how the def body is interpreted by changing the language to something other than Shell. Modifiers go in between the def name/prototype and the body. One language modifier that can be used is C<perl>. It causes the ...
def body-size-extractor : perl {
while (<STDIN>) {
## body size is the last field in the log
print "$1\n" if /(\d+)$/;
}
}
This def could also have been written in sh, but dealing with shell escapes is sometimes annoying:
def body-size-extractor {
perl -e 'while(<STDIN>) { ... }'
}
Def modifiers themselves sometimes take arguments. For example, perl defs can take the C<-n> switch which implicitly adds a loop (just like the perl binary):
def body-size-extractor : perl -n {
print "$1\n" if /(\d+)$/;
}
( run in 1.226 second using v1.01-cache-2.11-cpan-5467b0d2c73 )