| 1 | package Debug::ShowStuff::ShowVar; | ||
| 2 | use strict; | ||
| 3 | use Filter::Util::Call; | ||
| 4 | # use Debug::ShowStuff ':all'; | ||
| 5 | # use re 'taint'; | ||
| 6 | use vars qw($VERSION $debug); | ||
| 7 | |||
| 8 | # version | ||
| 9 | $VERSION = '0.11'; | ||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Debug::ShowStuff::ShowVar - shortcuts for Debug::ShowStuff | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | use Debug::ShowStuff ':all'; | ||
| 20 | use Debug::ShowStuff::ShowVar; | ||
| 21 | |||
| 22 | # output the name of the variable followed by the value of the variable | ||
| 23 | showvar $myvar; | ||
| 24 | |||
| 25 | ## outputs this line with println | ||
| 26 | |||
| 27 | ##- outputs this line with prinhr | ||
| 28 | |||
| 29 | ##i indents using indent | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | Debug::ShowStuff::ShowVar is a preprocessor that provides shortcuts for a few | ||
| 34 | Debug::ShowStuff commands. This module modifies your code so that some simple | ||
| 35 | commands are translated into longer Debug::ShowStuff commands. | ||
| 36 | |||
| 37 | Debug::ShowStuff::ShowVar is a B<filter>, so it requires very simple syntax for | ||
| 38 | it to understand your code. Don't get fancy, this module won't understand it. | ||
| 39 | |||
| 40 | =head1 Commands | ||
| 41 | |||
| 42 | =head2 showvar | ||
| 43 | |||
| 44 | showvar translates a line into a println that shows the name of a variable and | ||
| 45 | its value. So, for example, this line: | ||
| 46 | |||
| 47 | showvar $myvar; | ||
| 48 | |||
| 49 | translates into this line | ||
| 50 | |||
| 51 | println '$myvar: ', $myvar; | ||
| 52 | |||
| 53 | =head2 ## | ||
| 54 | |||
| 55 | A double hash followed by text gets translated into a println statement of that | ||
| 56 | text. So, for example, this code: | ||
| 57 | |||
| 58 | ## my text | ||
| 59 | |||
| 60 | is translated into | ||
| 61 | |||
| 62 | println 'my text'; | ||
| 63 | |||
| 64 | Note that there must be exactly two hash marks. Three or more will not be | ||
| 65 | translated. | ||
| 66 | |||
| 67 | ## is handy for when you don't want to repeat yourself documenting code. | ||
| 68 | For examle, this redundant code: | ||
| 69 | |||
| 70 | # open a file | ||
| 71 | println 'open a file'; | ||
| 72 | open_file(); | ||
| 73 | |||
| 74 | can be written more concisely as | ||
| 75 | |||
| 76 | ## open a file | ||
| 77 | open_file(); | ||
| 78 | |||
| 79 | =head2 ##- and ##= | ||
| 80 | |||
| 81 | ##- and ##= work like ##, except that a printhr statement is created instead of | ||
| 82 | println. ##- creates a horizontal rule using dashes (-). ##= creates a | ||
| 83 | horizontal rule using equals (=). | ||
| 84 | |||
| 85 | =head2 ##i | ||
| 86 | |||
| 87 | ##i translates into an indent command, including a variable to hold the lexical | ||
| 88 | indent variable. For example, suppose you want to output a value with println, | ||
| 89 | then indent the rest of the scope. With just Debug::ShowStuff commands you | ||
| 90 | would do this: | ||
| 91 | |||
| 92 | println 'begin'; | ||
| 93 | my $indent = indent(); | ||
| 94 | |||
| 95 | Instead, you can put ##i at the beginning of the first line and get the indent. | ||
| 96 | So the following line accomplishes the same thing as above: | ||
| 97 | |||
| 98 | println 'begin'; ##i | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | #------------------------------------------------------------------ | ||
| 105 | # new | ||
| 106 | # | ||
| 107 | sub new { | ||
| 108 | my ($class, %opts) = @_; | ||
| 109 | my $self = bless({}, $class); | ||
| 110 | |||
| 111 | # return object | ||
| 112 | return $self; | ||
| 113 | } | ||
| 114 | # | ||
| 115 | # new | ||
| 116 | #------------------------------------------------------------------ | ||
| 117 | |||
| 118 | |||
| 119 | #------------------------------------------------------------------ | ||
| 120 | # import routine: creates filter object and adds it | ||
| 121 | # to the filters array | ||
| 122 | # | ||
| 123 | sub import { | ||
| 124 | my ($class, %opts) = @_; | ||
| 125 | |||
| 126 | # add filter if set to do so | ||
| 127 | if ( defined($opts{'filter_add'}) ? $opts{'filter_add'} : 1 ) { | ||
| 128 | filter_add($class->new(%opts)); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | # | ||
| 132 | # import routine | ||
| 133 | #------------------------------------------------------------------ | ||
| 134 | |||
| 135 | |||
| 136 | #------------------------------------------------------------------ | ||
| 137 | # filter: this sub is run for every line in the calling script | ||
| 138 | # | ||
| 139 | sub filter { | ||
| 140 | my $self = shift; | ||
| 141 | my $status = filter_read(); | ||
| 142 | my $line = $_; | ||
| 143 | |||
| 144 | # printhr with - | ||
| 145 | # change to printhr if line starts with a double hash then dash (##-) | ||
| 146 | # followed by non whitespace that isn't a hash | ||
| 147 | if ($line =~ s|^\s*\#\#\-\s*([^\#]+)|$1|s) { | ||
| 148 | $line =~ s|[\n\r]+$||s; | ||
| 149 | $line =~ s|'|\\'|gs; | ||
| 150 | $line = "printhr '$line';\n"; | ||
| 151 | } | ||
| 152 | |||
| 153 | # printhr with = | ||
| 154 | # change to printhr if line starts with a double hash then dash (##=) | ||
| 155 | # followed by non whitespace that isn't a hash | ||
| 156 | elsif ($line =~ s|^\s*\#\#\=\s*([^\#]+)|$1|s) { | ||
| 157 | $line =~ s|[\n\r]+$||s; | ||
| 158 | $line =~ s|'|\\'|gs; | ||
| 159 | $line = "printhr title=>'$line', dash=>'=';\n"; | ||
| 160 | } | ||
| 161 | |||
| 162 | # indent | ||
| 163 | # change to an indentation if line ends with ##i | ||
| 164 | elsif ($line =~ s|\#\#i\s*$||s) { | ||
| 165 | my (@chars, $varname); | ||
| 166 | |||
| 167 | # initialize $varname to empty string | ||
| 168 | $varname = ''; | ||
| 169 | |||
| 170 | # set of variable characters | ||
| 171 | @chars = ('a' .. 'z', 'A' .. 'Z'); | ||
| 172 | |||
| 173 | # generate random variable name | ||
| 174 | for (1 .. 10) { | ||
| 175 | my ($char); | ||
| 176 | $char = rand(); | ||
| 177 | |||
| 178 | $char = int( $char * ($#chars + 1) ); | ||
| 179 | $char = $chars[$char]; | ||
| 180 | |||
| 181 | $varname .= $char; | ||
| 182 | } | ||
| 183 | |||
| 184 | $line .= ";my \$$varname = indent();\n"; | ||
| 185 | } | ||
| 186 | |||
| 187 | # println | ||
| 188 | # change to println if line starts with a double hash (##) | ||
| 189 | # followed by non whitespace that isn't a hash | ||
| 190 | elsif ($line =~ s|^\s*\#\#\s*([^\#]+)|$1|s) { | ||
| 191 | $line =~ s|[\n\r]+$||s; | ||
| 192 | $line =~ s|'|\\'|gs; | ||
| 193 | $line = "println '$line';\n"; | ||
| 194 | } | ||
| 195 | |||
| 196 | # showvar | ||
| 197 | elsif ($line =~ s|^\s*showvar (\$\S+\s*);.*|$1|si) { | ||
| 198 | my $var_name = $line; | ||
| 199 | $line =~ s|[\n\r]+$||s; | ||
| 200 | $line =~ s|'|\\'|gs; | ||
| 201 | $line = "println '$line: ', $var_name;\n"; | ||
| 202 | } | ||
| 203 | |||
| 204 | # set line and return value | ||
| 205 | $_= $line; | ||
| 206 | $status; | ||
| 207 | } | ||
| 208 | # | ||
| 209 | # filter | ||
| 210 | #------------------------------------------------------------------ | ||
| 211 | |||
| 212 | |||
| 213 | # return true | ||
| 214 | 1; | ||
| 215 | |||
| 216 | __END__ |