| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::sh2p::Here; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # I expect only one active here doc at a time, | 
| 4 |  |  |  |  |  |  | # but I guess they could be in nested loops | 
| 5 |  |  |  |  |  |  | #     while read var1 | 
| 6 |  |  |  |  |  |  | #     do | 
| 7 |  |  |  |  |  |  | #        while read var2 | 
| 8 |  |  |  |  |  |  | #        do | 
| 9 |  |  |  |  |  |  | #           ... | 
| 10 |  |  |  |  |  |  | #        done << HERE | 
| 11 |  |  |  |  |  |  | #           ... | 
| 12 |  |  |  |  |  |  | #        HERE | 
| 13 |  |  |  |  |  |  | #     done << HERE | 
| 14 |  |  |  |  |  |  | #        ... | 
| 15 |  |  |  |  |  |  | #     HERE | 
| 16 |  |  |  |  |  |  | # This would create a problem, since the filename | 
| 17 |  |  |  |  |  |  | # is based on the here label - TODO | 
| 18 |  |  |  |  |  |  | # | 
| 19 | 1 |  |  | 1 |  | 4 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 25 |  | 
| 20 | 1 |  |  | 1 |  | 5 | use Carp; | 
|  | 1 |  |  |  |  | 17 |  | 
|  | 1 |  |  |  |  | 59 |  | 
| 21 | 1 |  |  | 1 |  | 5 | use Scalar::Util qw(refaddr); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 88 |  | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 1 |  |  | 1 |  | 4 | use App::sh2p::Utils; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 1055 |  | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | our $VERSION = '0.06'; | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | ################################################################################# | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | my %handle; | 
| 30 |  |  |  |  |  |  | my %name; | 
| 31 |  |  |  |  |  |  | my %access; | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | my $g_last_opened_here_name; | 
| 34 |  |  |  |  |  |  | my $g_last_opened_file_name; | 
| 35 |  |  |  |  |  |  | my $g_write_subroutines = 0; | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | ################################################################################# | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub store_sh2p_here_subs { | 
| 40 | 0 |  |  | 0 | 0 |  | $g_write_subroutines = 1; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | ################################################################################# | 
| 44 |  |  |  |  |  |  | # January 2009 | 
| 45 |  |  |  |  |  |  | sub abandon_sh2p_here_subs { | 
| 46 | 0 |  |  | 0 | 0 |  | $g_write_subroutines = 0; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | ################################################################################# | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub get_last_here_doc { | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 0 |  |  | 0 | 0 |  | my $name = $g_last_opened_here_name; | 
| 54 | 0 |  |  |  |  |  | $g_last_opened_here_name = undef; | 
| 55 | 0 |  |  |  |  |  | return $name | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | ################################################################################# | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub get_last_file_name { | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 0 |  |  | 0 | 0 |  | my @caller = caller(); | 
| 64 | 0 |  |  |  |  |  | print STDERR "get_last_file_name: <$g_last_opened_file_name> @caller\n"; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 |  |  |  |  |  | my $name = $g_last_opened_file_name; | 
| 67 | 0 |  |  |  |  |  | $g_last_opened_file_name = undef; | 
| 68 | 0 |  |  |  |  |  | return $name | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | ################################################################################# | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | sub _get_dir { | 
| 75 | 0 |  |  | 0 |  |  | my $dir; | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 0 | 0 |  |  |  |  | if (defined $ENV{SH2P_HERE_DIR}) { | 
| 78 | 0 |  |  |  |  |  | $dir = $ENV{SH2P_HERE_DIR} | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | else { | 
| 81 | 0 |  |  |  |  |  | $dir = '.' | 
| 82 |  |  |  |  |  |  | } | 
| 83 | 0 |  |  |  |  |  | return $dir; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | ################################################################################# | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub gen_filename { | 
| 89 | 0 |  |  | 0 | 0 |  | my $name = shift; | 
| 90 | 0 |  |  |  |  |  | my $dir  = _get_dir(); | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 0 |  |  |  |  |  | return "$dir/$name.here"; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | ################################################################################# | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | sub open { | 
| 98 | 0 |  |  | 0 | 0 |  | my ($class, $name, $access) = @_; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 0 |  |  |  |  |  | my $this = bless \do{my $some_scalar}, $class; | 
|  | 0 |  |  |  |  |  |  | 
| 101 | 0 |  |  |  |  |  | my $key = refaddr $this; | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 0 |  |  |  |  |  | $name  {$key} = $name; | 
| 104 | 0 |  |  |  |  |  | $access{$key} = $access; | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 0 |  |  |  |  |  | $g_last_opened_here_name = $name; | 
| 107 | 0 |  |  |  |  |  | my $full_name = gen_filename($name); | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 0 |  |  |  |  |  | error_out ("Writing $full_name"); | 
| 110 | 0 | 0 |  |  |  |  | open ($handle{$key}, $access{$key}, "$full_name") || | 
| 111 |  |  |  |  |  |  | carp "Unable to open $full_name: $!\n"; | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 0 |  |  |  |  |  | $g_write_subroutines = 1; | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 0 |  |  |  |  |  | return $this | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | ################################################################################# | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | sub open_rd { | 
| 121 | 0 |  |  | 0 | 0 |  | my ($class, $filename, $access) = @_; | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 0 |  |  |  |  |  | my $this = bless \do{my $some_scalar}, $class; | 
|  | 0 |  |  |  |  |  |  | 
| 124 | 0 |  |  |  |  |  | my $key = refaddr $this; | 
| 125 |  |  |  |  |  |  |  | 
| 126 | 0 |  |  |  |  |  | $name  {$key} = $filename; | 
| 127 | 0 |  |  |  |  |  | $access{$key} = $access; | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 0 |  |  |  |  |  | $g_last_opened_file_name = $filename; | 
| 130 | 0 |  |  |  |  |  | $g_write_subroutines = 1; | 
| 131 |  |  |  |  |  |  |  | 
| 132 | 0 |  |  |  |  |  | return $this | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | ################################################################################# | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | sub write { | 
| 138 | 0 |  |  | 0 | 0 |  | my ($this, $buffer) = @_; | 
| 139 | 0 |  |  |  |  |  | my $key = refaddr $this; | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 0 |  |  |  |  |  | my $handle = $handle{$key}; | 
| 142 |  |  |  |  |  |  |  | 
| 143 | 0 | 0 |  |  |  |  | print $handle ("$buffer\n") or | 
| 144 |  |  |  |  |  |  | carp "Unable to write to $name{$key}: $!"; | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | } | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | ################################################################################# | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | sub read { | 
| 151 | 0 |  |  | 0 | 0 |  | my ($this) = @_; | 
| 152 | 0 |  |  |  |  |  | my $key = refaddr $this; | 
| 153 |  |  |  |  |  |  |  | 
| 154 | 0 |  |  |  |  |  | return <$handle{key}> | 
| 155 |  |  |  |  |  |  | } | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | ################################################################################# | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | sub close { | 
| 160 | 0 |  |  | 0 | 0 |  | my ($this) = @_; | 
| 161 | 0 |  |  |  |  |  | my $key = refaddr $this; | 
| 162 |  |  |  |  |  |  |  | 
| 163 | 0 |  |  |  |  |  | my $retn = close $handle{$key}; | 
| 164 | 0 |  |  |  |  |  | delete $handle{$key}; | 
| 165 | 0 |  |  |  |  |  | delete $name  {$key}; | 
| 166 | 0 |  |  |  |  |  | delete $access{$key}; | 
| 167 |  |  |  |  |  |  |  | 
| 168 | 0 |  |  |  |  |  | return $retn; | 
| 169 |  |  |  |  |  |  | } | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | ################################################################################# | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | sub DESTROY { | 
| 174 | 0 |  |  | 0 |  |  | my ($this) = @_; | 
| 175 | 0 |  |  |  |  |  | my $key = refaddr $this; | 
| 176 |  |  |  |  |  |  |  | 
| 177 | 0 | 0 |  |  |  |  | if (exists $name{$key}) { | 
| 178 | 0 |  |  |  |  |  | close_here_doc ($this); | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  | } | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | ################################################################################# | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | sub write_here_subs { | 
| 185 |  |  |  |  |  |  |  | 
| 186 | 0 | 0 |  | 0 | 0 |  | if ($g_write_subroutines) { | 
| 187 |  |  |  |  |  |  |  | 
| 188 | 0 |  |  |  |  |  | $g_write_subroutines = 0; | 
| 189 |  |  |  |  |  |  |  | 
| 190 | 0 |  |  |  |  |  | out ""; | 
| 191 |  |  |  |  |  |  |  | 
| 192 | 0 |  |  |  |  |  | out << 'END'; | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | ###################################################### | 
| 196 |  |  |  |  |  |  | # sh2p_read_from_handle | 
| 197 |  |  |  |  |  |  | # Arguments: | 
| 198 |  |  |  |  |  |  | #       1. Handle | 
| 199 |  |  |  |  |  |  | #	2. Value of $IFS | 
| 200 |  |  |  |  |  |  | #	3. Prompt string | 
| 201 |  |  |  |  |  |  | #	4. List of scalar references | 
| 202 |  |  |  |  |  |  | #	Any may be undef | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | sub sh2p_read_from_handle { | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | my ($handle, $sh2p_IFS, $prompt, @refs) = @_; | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | return 0 if eof($handle); | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | if (!defined $sh2p_IFS) { | 
| 211 |  |  |  |  |  |  | $sh2p_IFS = " \t\n"; | 
| 212 |  |  |  |  |  |  | } | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | if (defined $prompt) { | 
| 215 |  |  |  |  |  |  | print $prompt | 
| 216 |  |  |  |  |  |  | } | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | my $line = <$handle>; | 
| 219 |  |  |  |  |  |  | my $sh2p_REPLY; | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | chomp $line; | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | my (@vars) = split /[$sh2p_IFS]+/, $line; | 
| 224 |  |  |  |  |  |  | my $i; | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | # Assign values to variables | 
| 227 |  |  |  |  |  |  | for ($i = 0; $i < @refs; $i++) { | 
| 228 |  |  |  |  |  |  | if ($i > $#vars) { | 
| 229 |  |  |  |  |  |  | ${$refs[$i]} = ''; | 
| 230 |  |  |  |  |  |  | } | 
| 231 |  |  |  |  |  |  | else { | 
| 232 |  |  |  |  |  |  | ${$refs[$i]} = $vars[$i]; | 
| 233 |  |  |  |  |  |  | } | 
| 234 |  |  |  |  |  |  | } | 
| 235 |  |  |  |  |  |  |  | 
| 236 |  |  |  |  |  |  | # If not enough variables supplied | 
| 237 |  |  |  |  |  |  | if ($i < @vars || !@refs) { | 
| 238 |  |  |  |  |  |  | my $IFS1st = substr($sh2p_IFS,0,1); | 
| 239 |  |  |  |  |  |  | $sh2p_REPLY = join $IFS1st, @vars[$i..$#vars]; | 
| 240 |  |  |  |  |  |  | } | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  | if (@refs > 0 && defined $sh2p_REPLY) { | 
| 243 |  |  |  |  |  |  | # Concat extra values onto the element | 
| 244 |  |  |  |  |  |  | ${$refs[-1]} .= " $REPLY"; | 
| 245 |  |  |  |  |  |  | } | 
| 246 |  |  |  |  |  |  |  | 
| 247 |  |  |  |  |  |  | return 1; | 
| 248 |  |  |  |  |  |  | } | 
| 249 |  |  |  |  |  |  |  | 
| 250 |  |  |  |  |  |  | ###################################################### | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  | sub sh2p_read_from_stdin { | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  | my (@args) = @_; | 
| 255 |  |  |  |  |  |  |  | 
| 256 |  |  |  |  |  |  | return sh2p_read_from_handle (*STDIN, @args); | 
| 257 |  |  |  |  |  |  | } | 
| 258 |  |  |  |  |  |  |  | 
| 259 |  |  |  |  |  |  | ###################################################### | 
| 260 |  |  |  |  |  |  | { | 
| 261 |  |  |  |  |  |  | # No 'state' variables in 5.8 | 
| 262 |  |  |  |  |  |  | my $handle; | 
| 263 |  |  |  |  |  |  |  | 
| 264 |  |  |  |  |  |  | sub sh2p_read_from_file { | 
| 265 |  |  |  |  |  |  |  | 
| 266 |  |  |  |  |  |  | my ($filename, @args) = @_; | 
| 267 |  |  |  |  |  |  |  | 
| 268 |  |  |  |  |  |  | if (!defined $handle) { | 
| 269 |  |  |  |  |  |  | open ($handle, '<', $filename) or | 
| 270 |  |  |  |  |  |  | die "Unable to open $filename: $!"; | 
| 271 |  |  |  |  |  |  | } | 
| 272 |  |  |  |  |  |  |  | 
| 273 |  |  |  |  |  |  | my $retn = sh2p_read_from_handle ($handle, @args); | 
| 274 |  |  |  |  |  |  | if (!$retn) { | 
| 275 |  |  |  |  |  |  | close $handle; | 
| 276 |  |  |  |  |  |  | undef $handle; | 
| 277 |  |  |  |  |  |  | } | 
| 278 |  |  |  |  |  |  |  | 
| 279 |  |  |  |  |  |  | return $retn; | 
| 280 |  |  |  |  |  |  | } | 
| 281 |  |  |  |  |  |  |  | 
| 282 |  |  |  |  |  |  | } | 
| 283 |  |  |  |  |  |  |  | 
| 284 |  |  |  |  |  |  | ###################################################### | 
| 285 |  |  |  |  |  |  | #  End of subroutines added by sh2p | 
| 286 |  |  |  |  |  |  | ###################################################### | 
| 287 |  |  |  |  |  |  | END | 
| 288 |  |  |  |  |  |  | # End of here document | 
| 289 |  |  |  |  |  |  |  | 
| 290 |  |  |  |  |  |  | } | 
| 291 |  |  |  |  |  |  | } | 
| 292 |  |  |  |  |  |  |  | 
| 293 |  |  |  |  |  |  | ################################################################################# | 
| 294 |  |  |  |  |  |  | 1; | 
| 295 |  |  |  |  |  |  |  |