| 1 | package See; | ||
| 2 | ###################################################################### | ||
| 3 | # | ||
| 4 | # See - Debug See | ||
| 5 | # | ||
| 6 | # https://metacpan.org/release/See | ||
| 7 | # | ||
| 8 | # Copyright (c) 2021 INABA Hitoshi <ina@cpan.org> in a CPAN | ||
| 9 | ###################################################################### | ||
| 10 | |||
| 11 | use 5.00503; # Universal Consensus 1998 for primetools | ||
| 12 | # use 5.008001; # Lancaster Consensus 2013 for toolchains | ||
| 13 | |||
| 14 | $VERSION = '0.01'; | ||
| 15 | $VERSION = $VERSION; | ||
| 16 | |||
| 17 | BEGIN { pop @INC if $INC[-1] eq '.' } # CVE-2016-1238: Important unsafe module load path flaw | ||
| 18 | use strict; | ||
| 19 | BEGIN { $INC{'warnings.pm'} = '' if $] < 5.006 } use warnings; local $^W=1; | ||
| 20 | |||
| 21 | use vars qw( $seen ); | ||
| 22 | BEGIN { | ||
| 23 | if (defined($ENV{'PERL_DEBUG_SEE'}) and $ENV{'PERL_DEBUG_SEE'}) { | ||
| 24 | $seen = \do { local *_ } if $] < 5.006; | ||
| 25 | my($year,$month,$day,$hour,$min,$sec) = (localtime)[5,4,3,2,1,0]; | ||
| 26 | open $seen, sprintf(">seen-%04d%02d%02d-%02d%02d%02d-$$.log", 1900+$year, $month+1, $day, $hour, $min, $sec); | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | END { | ||
| 31 | if (defined($ENV{'PERL_DEBUG_SEE'}) and $ENV{'PERL_DEBUG_SEE'}) { | ||
| 32 | close $seen; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | use vars qw( %script ); | ||
| 37 | sub See::see ($) { | ||
| 38 | if (defined($ENV{'PERL_DEBUG_SEE'}) and $ENV{'PERL_DEBUG_SEE'}) { | ||
| 39 | my $got = $_[0] || ''; | ||
| 40 | my($package, $filename, $line) = caller; | ||
| 41 | if (not defined $script{$filename}) { | ||
| 42 | my $fh = \do { local *_ } if $] < 5.006; | ||
| 43 | if (open $fh, $filename) { | ||
| 44 | push @{$script{$filename}}, undef; | ||
| 45 | while (<$fh>) { | ||
| 46 | chomp; | ||
| 47 | s/\A \s+ //x; | ||
| 48 | push @{$script{$filename}}, $_; | ||
| 49 | } | ||
| 50 | close $fh; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | print {$seen} qq{got($got) by $script{$filename}[$line] at $filename($line)\n}; | ||
| 54 | } | ||
| 55 | return $_[0]; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub See::notsee ($) { | ||
| 59 | return $_[0]; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub import { | ||
| 63 | no strict 'refs'; | ||
| 64 | *{caller().'::see'} = \&See::see; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub unimport { | ||
| 68 | no strict 'refs'; | ||
| 69 | *{caller().'::see'} = \&See::notsee; | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; | ||
| 73 | |||
| 74 | __END__ |