| 1 | package Text::Prefix; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Prepend strings with timestamps and potentially other contextually-relevant information. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Sys::Hostname; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use File::Valet qw(rd_f wr_f ap_f); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Time::HiRes; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Time::TAI::Simple; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use vars qw(@EXPORT @EXPORT_OK @ISA $VERSION); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | $VERSION = '1.00'; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | @EXPORT = @EXPORT_OK = (); | |||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=25 SCAR=35.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 5 | my ($class, %opt_hr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 5 | my $self = { | |||||||||||||||||||||||||||||||||||||||||
| 25 | opt_hr => \%opt_hr | ||||||||||||||||||||||||||||||||||||||||||
| 26 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 5 | bless ($self, $class); | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 5 | foreach my $k0 (keys %{$self->{opt_hr}}) { | |||||||||||||||||||||||||||||||||||||||||
| 30 | 10 | my $k1 = join('_', split(/-/, $k0)); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 10 | ▶ | next if ($k0 eq $k1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | $self->{opt_hr}->{$k1} = $self->{opt_hr}->{$k0}; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | delete $self->{opt_hr}->{$k0}; | |||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 5 | $self->{data_label} = $self->opt('label', 'd'); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 5 | $self->{format} = $self->opt('format','space'); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | ▶ | $self->{format} = 'kvp' if ($self->opt('kvp')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 5 | $self->{host} = hostname(); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | ▶ | $self->{host} = $1 if ($self->{host} =~ /(.+?)\./); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | ▶ | if (my $mask = $self->opt('host_sans')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | $self->{host} = $1 if ($self->{host} =~ /(.+?)$mask$/); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 5 | $self->{perlcode} = $self->opt('perl', ''); | |||||||||||||||||||||||||||||||||||||||||
| 45 | 5 | ▶ | if (my $pf = $self->opt('perlf')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | die "no such file (passed via perlf): '$pf'" unless (-e $pf); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $self->{perlcode} = File::Valet::rd_f($pf); | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | ▶ | $ENV{HOSTNAME} = $ENV{HOST} = $self->{host} if ($self->{perlcode} ne ''); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 5 | ▶ | if ($self->opt('order')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | $self->{order_ar} = [split(/\s*,\s*/, $self->opt('order'))]; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | my @order_list; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | ▶ | push @order_list, 'lt' unless ($self->opt('no_date') || $self->opt('no_time') || $self->opt('no_human_date')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$self->opt("no_date") or $self->opt("no_time") or $self->opt("no_human_date")
$self->opt("no_date") or $self->opt("no_time")
MC/DC33%1/3
$self->opt("no_date") or $self->opt("no_time") or $self->opt("no_human_date")
$self->opt("no_date")
$self->opt("no_time")
$self->opt("no_human_date")
Truth table50%2/4
A or B or C
A= $self->opt("no_date")B= $self->opt("no_time")C= $self->opt("no_human_date")
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | ▶ | push @order_list, 'tm' unless ($self->opt('no_date') || $self->opt('no_time') || $self->opt('no_epoch')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$self->opt("no_date") or $self->opt("no_time") or $self->opt("no_epoch")
$self->opt("no_date") or $self->opt("no_time")
MC/DC33%1/3
$self->opt("no_date") or $self->opt("no_time") or $self->opt("no_epoch")
$self->opt("no_date")
$self->opt("no_time")
$self->opt("no_epoch")
Truth table50%2/4
A or B or C
A= $self->opt("no_date")B= $self->opt("no_time")C= $self->opt("no_epoch")
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | ▶ | push @order_list, 'hn' if ($self->opt('host') || $self->opt('host_sans')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->opt("host") or $self->opt("host_sans")
MC/DC0%0/2
$self->opt("host") or $self->opt("host_sans")
$self->opt("host")
$self->opt("host_sans")
Truth table33%1/3
A or B
A= $self->opt("host")B= $self->opt("host_sans")
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | push @order_list, 'st' if ($self->opt('with')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | ▶ | push @order_list, 'pl' if ($self->opt('perl') || $self->opt('perlf')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->opt("perl") or $self->opt("perlf")
MC/DC0%0/2
$self->opt("perl") or $self->opt("perlf")
$self->opt("perl")
$self->opt("perlf")
Truth table33%1/3
A or B
A= $self->opt("perl")B= $self->opt("perlf")
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | push @order_list, $self->{data_label}; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | $self->{order_ar} = \@order_list; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | ▶ | if (my $tai = $self->opt('tai')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | my $tai_mode = 'tai10'; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | $tai_mode = 'tai35' if ($tai eq '35'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | ▶ | $tai_mode = 'tai' if ($tai eq '0'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | $self->{tai_or} = Time::TAI::Simple->new(mode => $tai_mode); | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 5 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | sub prefix { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prefix: called CC=20 SCAR=32.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 3 | my ($self, $s) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 3 | ▶ | ap_f($self->opt('pretee'), $s) if ($self->opt('pretee')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | chomp($s); | |||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | my $pl = ''; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | $pl = join(' ', split(/[\r\n]+/, eval($self->{perlcode}))) if ($self->{perlcode} ne ''); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | my $hr = {$self->{data_label} => $s}; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | ▶ | $hr->{tm} = $self->_tm() unless ($self->opt('no_date') || $self->opt('no_epoch')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->opt("no_date") or $self->opt("no_epoch")
MC/DC50%1/2
$self->opt("no_date") or $self->opt("no_epoch")
$self->opt("no_date")
$self->opt("no_epoch")
Truth table66%2/3
A or B
A= $self->opt("no_date")B= $self->opt("no_epoch")
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | ▶ | $hr->{lt} = $self->_lt() unless ($self->opt('no_date') || $self->opt('no_human_date')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->opt("no_date") or $self->opt("no_human_date")
MC/DC50%1/2
$self->opt("no_date") or $self->opt("no_human_date")
$self->opt("no_date")
$self->opt("no_human_date")
Truth table66%2/3
A or B
A= $self->opt("no_date")B= $self->opt("no_human_date")
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | ▶ | $hr->{hn} = $self->{host} if ($self->opt('host') || $self->opt('host_sans')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->opt("host") or $self->opt("host_sans")
MC/DC50%1/2
$self->opt("host") or $self->opt("host_sans")
$self->opt("host")
$self->opt("host_sans")
Truth table66%2/3
A or B
A= $self->opt("host")B= $self->opt("host_sans")
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | $hr->{st} = $self->opt('with') if ($self->opt('with')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | $hr->{pl} = $pl if ($self->opt('perl') || $self->opt('perlf')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->opt("perl") or $self->opt("perlf")
MC/DC50%1/2
$self->opt("perl") or $self->opt("perlf")
$self->opt("perl")
$self->opt("perlf")
Truth table66%2/3
A or B
A= $self->opt("perl")B= $self->opt("perlf")
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 3 | my $output = ''; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | ▶ | my $pad = $self->opt('no_space') ? '' : ' '; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | foreach my $k (@{$self->{order_ar}}) { | |||||||||||||||||||||||||||||||||||||||||
| 88 | 9 | ▶ | next unless(defined($hr->{$k})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 9 | my $v = $hr->{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 9 | ▶ | if ($self->{format} eq 'kvp') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $output .= "$k=$v\t"; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | elsif ($self->{format} eq 'csv') { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $output .= "\"$v\","; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | elsif ($self->{format} eq 'tab') { | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | $output .= "$v\t"; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | else { # assume 'space' | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 9 | $output .= "$v$pad"; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | ▶ | chop($output) if ($pad); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | ▶ | ap_f($self->opt('tee'), "$output\n") if ($self->opt('tee')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 3 | return $output; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub _isotime { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_isotime: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my ($self, $tm) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $tm = $self->_tm() unless(defined($tm)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($tm); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $iso_date = sprintf('%04d-%02d-%02d', $year + 1900, $mon + 1, $mday); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my $iso_time = sprintf('%02d:%02d:%02d', $hour, $min, $sec); | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | $iso_time .= substr($tm-int($tm), 1, 5) if ($self->opt('hires') || $self->opt('tai')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->opt("hires") or $self->opt("tai")
MC/DC0%0/2
$self->opt("hires") or $self->opt("tai")
$self->opt("hires")
$self->opt("tai")
Truth table0%0/3
A or B
A= $self->opt("hires")B= $self->opt("tai")
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | return "$iso_date $iso_time"; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | ▶ | sub _tm { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tm: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 4 | my $tm; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 4 | ▶ | $tm = $self->{tai_or}->time() if (defined($self->{tai_or})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 4 | ▶ | $tm = Time::HiRes::time() if ($self->opt('hires')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 4 | ▶ | $tm = time() unless($tm); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 4 | ▶ | if ($tm =~ /\.\d+/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 2 | ▶ | if (length($tm) >= 15) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | $tm = substr($tm, 0, 15); | |||||||||||||||||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $tm .= '0'x(15-length($tm)); | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 4 | return $tm; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 2 | ▶ | sub _lt { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_lt: called CC=5 SCAR=17.0
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 2 | my ($self, $tm) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 2 | ▶ | $tm = $self->_tm() unless(defined($tm)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 2 | ▶ | return $self->_isotime($tm) if ($self->opt('iso')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 2 | my $lt = localtime($tm); | |||||||||||||||||||||||||||||||||||||||||
| 139 | 2 | ▶ | return substr($lt, 4, 15) if ($self->opt('short')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 2 | ▶ | return substr($lt, 11, 5) if ($self->opt('shorter')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 2 | return $lt; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 115 | ▶ | sub opt { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
opt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 115 | my ($self, $name, $default_value, $alt_hr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 115 | return _def($self->{opt_hr}->{$name}, $alt_hr->{$name}, $default_value); | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 115 | ▶ | sub _def { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_def: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 115 | ▶ | foreach my $v (@_) { return $v if (defined($v)); } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 83 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | Text::Prefix - Prepend strings with timestamps and potentially other contextually-relevant information. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | use Text::Prefix; | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | # Simple case: prepend strings with timestamps. | ||||||||||||||||||||||||||||||||||||||||||
| 165 | # | ||||||||||||||||||||||||||||||||||||||||||
| 166 | my $px = Text::Prefix->new(); # default just prepends timestamp | ||||||||||||||||||||||||||||||||||||||||||
| 167 | my $s = $px->prefix("some string"); | ||||||||||||||||||||||||||||||||||||||||||
| 168 | # | ||||||||||||||||||||||||||||||||||||||||||
| 169 | # $s is now: "Fri Jun 9 16:45:25 2017 1497051925 some string" | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | # More complex case: ISO timestamp, no epoch timestamp, high-resolution | ||||||||||||||||||||||||||||||||||||||||||
| 172 | # TAI-10 time, hostname, and length of string, in CSV format | ||||||||||||||||||||||||||||||||||||||||||
| 173 | # | ||||||||||||||||||||||||||||||||||||||||||
| 174 | my $px = Text::Prefix->new( | ||||||||||||||||||||||||||||||||||||||||||
| 175 | format => 'csv', | ||||||||||||||||||||||||||||||||||||||||||
| 176 | host => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 177 | iso => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 178 | no_epoch => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 179 | perl => 'length($s)', | ||||||||||||||||||||||||||||||||||||||||||
| 180 | tai => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 181 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 182 | my $s = $px->prefix("another string"); | ||||||||||||||||||||||||||||||||||||||||||
| 183 | # | ||||||||||||||||||||||||||||||||||||||||||
| 184 | # $s is now: '"2017-06-09 16:50:59.9161","xiombarg","14","another string"' | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | B<Text::Prefix> contains the logic implementing the B<prefix(1)> utility (included in this package). It takes arbitrary strings as | ||||||||||||||||||||||||||||||||||||||||||
| 189 | input and produces output with various contextually-relevant information preceding the string. A variety of output formats are also | ||||||||||||||||||||||||||||||||||||||||||
| 190 | supported, as well as output field reordering. | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | This is handy, for instance, when tailing a logfile which does not contain timestamps. B<prefix> adds a timestamp prefix to each | ||||||||||||||||||||||||||||||||||||||||||
| 193 | line it is given. | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | There are only two methods provided by this package, C<new> and C<prefix>. | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | =item B<new> (%options) | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | (Class method) Returns a new instance of B<Text::Prefix>. The object's default attributes are overridden by any options given. | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | Currently the following attributes may be set: | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | B<format> =E<gt> kvp, tab, csv, space | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | Format the output as a kvp (tab-delimited "key=value" pairs), tab-delimited, comma-delimited, or space-delimited values. | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | (default: "space") | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | B<hires> =E<gt> 0, 1 | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | Set to 1 to use high-resolution timestamps. | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | (default: 0) | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | B<host> =E<gt> 0, 1 | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | Set to 1 to prefix output with the local hostname. | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | (default: 0) | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | B<host_sans> =E<gt> regular expression string | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | Set to a string to exclude the matching part of the hostname from prefix. Implies setting B<host>. | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | (default: none) | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | B<iso> =E<gt> 0, 1 | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | Set to 1 to use ISO-8601 formatted timestamps (more or less). | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | (default: 0) | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | B<label> =E<gt> string | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | When output format is "kvp", use the provided string as the key value for the field containing the input string. | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | (default: "d") | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | B<no_date> =E<gt> 0, 1 | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | Set to 1 to omit any timestamps from prefixed text (corresponding to output fields "lt" and "tm"). | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | (default: 0) | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | B<no_human_date> =E<gt> 0, 1 | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | Set to 1 to omit human-readable timestamps from prefixed text (corresponding to output field "lt"). | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | (default: 0) | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | B<no_epoch> =E<gt> 0, 1 | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | Set to 1 to omit epoch timestamps from prefixed text (corresponding to output field "tm"). | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | (default: 0) | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | B<order> =E<gt> CSV string | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | Given a comma-separated list of key names, change the ordering of the named output fields. | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | Currently supported output fields are: | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | B<lt> - Human-readable timestamp string (mnemonic, "localtime") | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | B<tm> - Epoch timestamp | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | B<hn> - Hostname | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | B<st> - Literal string provided via passing C<with> parameter to C<new> | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | B<pl> - Value returned by evaluating perl provided via C<perl> or C<perlf> parameters passed to C<new> | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | B<d> - Original input string, potentially modified via C<perl> or C<perlf> side-effects. Key may be renamed via C<label> parameter. | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | (default: "lt, tm, hn, st, pl, d") | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | B<perl> =E<gt> string containing perl code | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | The provided string will be C<eval()>'d for every line of input, and its return value included in the output prefix. The input string is available to this code in the variable "$s". | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | (default: none) | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | B<perlf> =E<gt> filename | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | Just like B<perl> except the perl code is read from the given file. | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | (default: none) | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | B<pretee> =E<gt> filename | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | When provided, input is appended to the file of the given name before C<perl> evaluation or any other reformatting. | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | (default: none) | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | B<short> =E<gt> 0, 1 | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | Set to 1 to shorten the human-readable timestamp field somewhat. | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | (default: 0) | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | B<shorter> =E<gt> 0, 1 | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | Set to 1 to shorten the human-readable timestamp to only the hour and minute (HH:MM). | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | (default: 0) | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | B<tai> =E<gt> 0, 10, 35 | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | When provided, timestamps will reflect TAI-0, TAI-10, or TAI-35 time instead of system time. If option's value is anything other than 0 or 10 or 35, TAI-10 will be assumed. See also: L<https://metacpan.org/pod/Time::TAI::Simple>. TAI time is a high-resolution time, so a fractional second will be included in prefix timestamps. | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | (default: none) | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | B<tee> =E<gt> filename | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | Just like C<pretee>, but the output string will be appended to the named file. | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | (default: none) | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | B<with> =E<gt> string | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | When provided, the output will include the literal string in its prefix. | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | (default: none) | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | =item B<prefix> (string) | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | Returns the given string after applying the formatting and prefixing rules passed to C<new>. | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | =head1 TODO | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | Since this module was implemented specifically to support the functionality of the C<prefix(1)> tool, it lacks some obvious features which a programmer using the module directly might expect: | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | C<new> should probably take a C<coderef> option, to supplement C<perl> and C<perlf>. | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | C<new> should support a format option which causes C<prefix> to return a hashref or arrayref instead of a string. | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | =head1 HISTORY | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | C<prefix(1)> started life in 2001 as an extremely simple throwaway script. Like many "throwaway" scripts, this one grew haphazardly with little | ||||||||||||||||||||||||||||||||||||||||||
| 368 | regard to best practices. The author has used it almost every day since then, and was intensely embarrassed by the state of its source code, but | ||||||||||||||||||||||||||||||||||||||||||
| 369 | it took him until 2017 to get around to refactoring it into C<Text::Prefix>. | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | TTKCIAR <ttk@ciar.org> | ||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | Copyright (C) 2017 Bill "TTK" Moyer. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | This library is free software. You may use it, redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | =back | ||||||||||||||||||||||||||||||||||||||||||