| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::Rad::Exclude; | 
| 2 | 2 |  |  | 2 |  | 1854 | use Carp qw/carp croak/; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 147 |  | 
| 3 | 2 |  |  | 2 |  | 12 | use strict; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 64 |  | 
| 4 | 2 |  |  | 2 |  | 11 | use warnings; | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 1403 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '0.01'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | sub load { | 
| 9 | 0 |  |  | 0 | 1 |  | my ($self, $c) = @_; | 
| 10 | 0 |  |  |  |  |  | $c->register('exclude', \&exclude, 'completely erase command from your program'); | 
| 11 |  |  |  |  |  |  | } | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | # removes given sub from the | 
| 14 |  |  |  |  |  |  | # main program | 
| 15 |  |  |  |  |  |  | sub _remove_code_from_file { | 
| 16 | 0 |  |  | 0 |  |  | my $sub = shift; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | #TODO: I really should be using PPI | 
| 19 |  |  |  |  |  |  | #if the user has it installed... | 
| 20 | 0 | 0 |  |  |  |  | open my $fh, '+<', $0 | 
| 21 |  |  |  |  |  |  | or croak "error updating file $0: $!\n"; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | #    flock($fh, LOCK_EX) or carp "could not lock file $0: $!\n"; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 0 |  |  |  |  |  | my @file = <$fh>; | 
| 26 | 0 |  |  |  |  |  | my $ret = _remove_code_from_array(\@file, $sub); | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | # TODO: only change the file if it's eval'd without errors | 
| 29 | 0 | 0 |  |  |  |  | seek ($fh, 0, 0) or croak "error seeking file $0: $!\n"; | 
| 30 | 0 | 0 |  |  |  |  | print $fh @file or croak "error writing to file $0: $!\n"; | 
| 31 | 0 | 0 |  |  |  |  | truncate($fh, tell($fh)) or croak "error truncating file $0: $!\n"; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 0 |  |  |  |  |  | close $fh; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 0 |  |  |  |  |  | return $ret; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub _remove_code_from_array { | 
| 39 | 0 |  |  | 0 |  |  | my $file_array_ref = shift; | 
| 40 | 0 |  |  |  |  |  | my $sub = shift; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 0 |  |  |  |  |  | my $index = 0; | 
| 43 | 0 |  |  |  |  |  | my $open_braces = 0; | 
| 44 | 0 |  |  |  |  |  | my $close_braces = 0; | 
| 45 | 0 |  |  |  |  |  | my $sub_start = 0; | 
| 46 | 0 |  |  |  |  |  | while ( $file_array_ref->[$index] ) { | 
| 47 | 0 | 0 |  |  |  |  | if ($file_array_ref->[$index] =~ m/\s*sub\s+$sub(\s+|\s*\{)/) { | 
| 48 | 0 |  |  |  |  |  | $sub_start = $index; | 
| 49 |  |  |  |  |  |  | } | 
| 50 | 0 | 0 |  |  |  |  | if ($sub_start) { | 
| 51 |  |  |  |  |  |  | # in order to see where the sub ends, we'll | 
| 52 |  |  |  |  |  |  | # try to count the number of '{' against | 
| 53 |  |  |  |  |  |  | # the number of '}' available | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | #TODO:I should use an actual LR parser or | 
| 56 |  |  |  |  |  |  | #something. This would be greatly enhanced | 
| 57 |  |  |  |  |  |  | #and much less error-prone, specially for | 
| 58 |  |  |  |  |  |  | #nested symbols in the same line. | 
| 59 | 0 |  |  |  |  |  | $open_braces++ while $file_array_ref->[$index] =~ m/\{/g; | 
| 60 | 0 |  |  |  |  |  | $close_braces++ while $file_array_ref->[$index] =~ m/\}/g; | 
| 61 | 0 | 0 |  |  |  |  | if ( $open_braces > 0 ) { | 
| 62 | 0 | 0 |  |  |  |  | if ( $close_braces > $open_braces ) { | 
|  |  | 0 |  |  |  |  |  | 
| 63 | 0 |  |  |  |  |  | croak "Error removing $sub: could not parse $0 correctly."; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | elsif ( $open_braces == $close_braces ) { | 
| 66 |  |  |  |  |  |  | # remove lines from array | 
| 67 | 0 |  |  |  |  |  | splice (@{$file_array_ref}, $sub_start, ($index + 1 - $sub_start)); | 
|  | 0 |  |  |  |  |  |  | 
| 68 | 0 |  |  |  |  |  | last; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | continue { | 
| 74 | 0 |  |  |  |  |  | $index++; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 0 | 0 |  |  |  |  | if ($sub_start == 0) { | 
| 78 | 0 |  |  |  |  |  | return "Error finding '$sub' command. Built-in?"; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | else { | 
| 81 | 0 |  |  |  |  |  | return "Command '$sub' successfuly removed."; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub exclude { | 
| 86 | 0 |  |  | 0 | 1 |  | my $c = shift; | 
| 87 | 0 | 0 |  |  |  |  | if ( $c->argv->[0] ) { | 
| 88 | 0 | 0 |  |  |  |  | if ( $c->is_command( $c->argv->[0] ) ) { | 
| 89 | 0 |  |  |  |  |  | return _remove_code_from_file($c->argv->[0]); | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | else { | 
| 92 | 0 |  |  |  |  |  | return $c->argv->[0] . ' is not an available command'; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  | else { | 
| 96 | 0 |  |  |  |  |  | return "Sintax: $0 exclude command_name" | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | 42; | 
| 101 |  |  |  |  |  |  | __END__ |