| 1 | package Term::ShellKit::Dev; | ||
| 2 | |||
| 3 | require Term::ShellKit; | ||
| 4 | |||
| 5 | ###################################################################### | ||
| 6 | |||
| 7 | sub require { | ||
| 8 | die "No module name provided" unless ( scalar @_ ); | ||
| 9 | map { Term::ShellKit::require_package( $_ ) } @_; | ||
| 10 | } | ||
| 11 | |||
| 12 | ###################################################################### | ||
| 13 | |||
| 14 | use vars '%LibLastLoaded'; | ||
| 15 | |||
| 16 | sub reload { | ||
| 17 | if ( scalar @_ ) { | ||
| 18 | my $lib = shift; | ||
| 19 | ( $lib .= '.pm' ) =~ s|::|/|go unless ( $lib =~ /\.\w{2,3}$/ ); | ||
| 20 | delete $INC{$lib}; | ||
| 21 | CORE::require( $lib ); | ||
| 22 | return "Reloaded $lib"; | ||
| 23 | } | ||
| 24 | my @libs; | ||
| 25 | while(my($lib, $file) = each %INC) { | ||
| 26 | local $^W = 0; | ||
| 27 | |||
| 28 | my $mtime = (stat $file)[9]; | ||
| 29 | |||
| 30 | # warn and skip the files with relative paths which | ||
| 31 | # can't be locate by applying @INC; | ||
| 32 | unless ( defined $mtime and $mtime ) { | ||
| 33 | warn "Can't locate $file" ; | ||
| 34 | next; | ||
| 35 | } | ||
| 36 | |||
| 37 | # Assume all files loaded at startup | ||
| 38 | $LibLastLoaded{$file} ||= $^T; | ||
| 39 | |||
| 40 | if($mtime > $LibLastLoaded{$file}) { | ||
| 41 | delete $INC{$lib}; | ||
| 42 | eval { | ||
| 43 | local $SIG{__DIE__}; | ||
| 44 | CORE::require( $lib ); | ||
| 45 | }; | ||
| 46 | if ( $@ ) { | ||
| 47 | $INC{$lib} = $file; | ||
| 48 | die $@; | ||
| 49 | } | ||
| 50 | push @libs, $lib; | ||
| 51 | $LibLastLoaded{$file} = $mtime; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | scalar(@libs) ? "Reloaded " . join(', ', @libs ) : 'No changes found in @INC'; | ||
| 56 | } | ||
| 57 | |||
| 58 | ###################################################################### | ||
| 59 | |||
| 60 | sub current_package () { | ||
| 61 | $Term::ShellKit::CurrentPackage; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub package ($) { | ||
| 65 | Term::ShellKit::package( @_ ); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub show_package (;$) { | ||
| 69 | my $package = shift || $Term::ShellKit::CurrentPackage; | ||
| 70 | |||
| 71 | no strict; | ||
| 72 | my $symbols = \%{ $package . '::' }; | ||
| 73 | |||
| 74 | my (%scalars, %arrays, %hashes, %codes, %packages); | ||
| 75 | foreach my $name ( sort keys %$symbols ) { | ||
| 76 | local *symbol = $symbols->{ $name }; | ||
| 77 | |||
| 78 | if ( defined $symbol ) { | ||
| 79 | $scalars{ $name } = "$symbol"; | ||
| 80 | } | ||
| 81 | if ( defined @symbol ) { | ||
| 82 | $arrays{ $name } = join(', ', @symbol); | ||
| 83 | } | ||
| 84 | if ( my $coderef = *Term::ShellKit::Dev::symbol{CODE} ) { | ||
| 85 | my $prototype = prototype($coderef); | ||
| 86 | $codes{ $name } = "$coderef" . ( $prototype ? " ($prototype)" : '' ); | ||
| 87 | } | ||
| 88 | |||
| 89 | if ( defined %symbol ) { | ||
| 90 | if ( $name =~ /(.*)\:\:\Z/ ) { | ||
| 91 | $packages{ $1 } = "PACKAGE"; | ||
| 92 | } else { | ||
| 93 | $hashes{ $name } = join(', ', map { "$_ => $symbol{$_}" } (sort | ||
| 94 | keys %symbol)); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | my @out = ( "Package Stash for $package" ); | ||
| 100 | foreach my $output ( | ||
| 101 | [ 'Scalars', \%scalars, '$', ], | ||
| 102 | ["Arrays", \%arrays, '@'], | ||
| 103 | [ 'Hashes', \%hashes, '%'], | ||
| 104 | ['Subs', \%codes, 'sub '], | ||
| 105 | ['Packages', \%packages, '::'] | ||
| 106 | ) { | ||
| 107 | next unless scalar keys %{$output->[1]}; | ||
| 108 | push @out, "$output->[0]:"; | ||
| 109 | foreach ( sort keys %{$output->[1]} ) { | ||
| 110 | push @out, " $output->[2]$_ = \"$output->[1]{$_}\""; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | join "\n", @out; | ||
| 114 | } | ||
| 115 | |||
| 116 | ###################################################################### | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | ###################################################################### | ||
| 121 | |||
| 122 | |||
| 123 | =head1 NAME | ||
| 124 | |||
| 125 | Term::ShellKit::Commands - Basic shell functions | ||
| 126 | |||
| 127 | |||
| 128 | =head1 SYNOPSIS | ||
| 129 | |||
| 130 | > perl -Iblib/lib -MTerm::ShellKit -eshell "kit Dev" | ||
| 131 | Term::ShellKit: Starting interactive shell; commands include help, exit. | ||
| 132 | Activating Term::ShellKit::Commands | ||
| 133 | Activating Term::ShellKit::Dev | ||
| 134 | |||
| 135 | Term::ShellKit> require MyClass | ||
| 136 | MyClass | ||
| 137 | |||
| 138 | Term::ShellKit> show_package MyClass | ||
| 139 | Package Stash for MyClass | ||
| 140 | Subs: | ||
| 141 | sub smee = "CODE(0x73fa4)" | ||
| 142 | sub twiddle = "CODE(0xc8530)" | ||
| 143 | |||
| 144 | |||
| 145 | =head1 COMMANDS | ||
| 146 | |||
| 147 | The following commands are available. | ||
| 148 | |||
| 149 | |||
| 150 | =head2 require | ||
| 151 | |||
| 152 | Load a Perl module or library. | ||
| 153 | |||
| 154 | =over 4 | ||
| 155 | |||
| 156 | =item * | ||
| 157 | |||
| 158 | require I<module> | ||
| 159 | |||
| 160 | =back | ||
| 161 | |||
| 162 | |||
| 163 | =head2 reload | ||
| 164 | |||
| 165 | Reload any Perl modules which have changed since they were last loaded. | ||
| 166 | |||
| 167 | =over 4 | ||
| 168 | |||
| 169 | =item * | ||
| 170 | |||
| 171 | reload | ||
| 172 | |||
| 173 | =back | ||
| 174 | |||
| 175 | You can use the shell reload command to read in changes to your modules while continuing to work in the same environment. | ||
| 176 | |||
| 177 | Start with the following code in MyObject.pm: | ||
| 178 | |||
| 179 | package MyObject; | ||
| 180 | |||
| 181 | sub new { | ||
| 182 | my $class = shift; | ||
| 183 | bless { }, $class; | ||
| 184 | } | ||
| 185 | |||
| 186 | 1; | ||
| 187 | |||
| 188 | Then start your shell and load your module: | ||
| 189 | |||
| 190 | ~> perl lib/Shell/Shell.pm | ||
| 191 | Term::ShellKit: Starting interactive shell | ||
| 192 | Term::ShellKit> require MyObject | ||
| 193 | |||
| 194 | You can now start creating instances of your class: | ||
| 195 | |||
| 196 | Term::ShellKit> $example = MyObject->new() | ||
| 197 | $example = MyObject->new(): MyObject=HASH(0x1e5118) | ||
| 198 | |||
| 199 | Your class doesn't do anything else yet, so trying to call other methods on your new object will result in an error: | ||
| 200 | |||
| 201 | Term::ShellKit> $example->twiddle | ||
| 202 | $example->twiddle: Failed. | ||
| 203 | shell_cmd_method: | ||
| 204 | shell_cmd_eval: Can't locate object method "twiddle" via package "MyObject" at (eval 12) line 1. | ||
| 205 | |||
| 206 | Let's define that method -- leave the shell running, and add the following method to your package: | ||
| 207 | |||
| 208 | sub twiddle { | ||
| 209 | my $self = shift; | ||
| 210 | return "Song and dance goes here..."; | ||
| 211 | } | ||
| 212 | |||
| 213 | Then return to the shell and run the "reload" command to load your changes. You can now start calling your new method, even on objects that were created earlier: | ||
| 214 | |||
| 215 | Term::ShellKit> reload | ||
| 216 | Term::ShellKit: reload MyObject.pm | ||
| 217 | Term::ShellKit> $example->twiddle | ||
| 218 | $example->twiddle: Song and dance goes here... | ||
| 219 | |||
| 220 | Subsequent additions or revisions to the module will be available the next time you run the "reload" command. (Note that if you remove a method from your module code, it will not be deleted from the live workspace; you'll need to quit and restart the shell to achieve this.) | ||
| 221 | |||
| 222 | If there's an error in your code, you'll get a message similar to this when you try to reload: | ||
| 223 | |||
| 224 | Term::ShellKit> reload | ||
| 225 | reload: Failed. | ||
| 226 | shell_cmd_method: Type of arg 1 to shift must be array (not return) | ||
| 227 | at /tmp/MyObject.pm line 10, near ""Song and dance goes here...";" | ||
| 228 | |||
| 229 | To view the problematic line, you can copy and paste in the file and line number, taking advantage of the default alias that maps "at" to "show_file": | ||
| 230 | |||
| 231 | Term::ShellKit> at /tmp/MyObject.pm line 10 | ||
| 232 | > show_file /tmp/MyObject.pm line 10 | ||
| 233 | > show_file /tmp/MyObject.pm window 2 line 10 | ||
| 234 | |||
| 235 | my $self = shift | ||
| 236 | return "Song and dance goes here..."; | ||
| 237 | } | ||
| 238 | |||
| 239 | If you need to see more of the code you can re-run the show_file command with a window argument that's larger than the default of 2, but that's generally enough to spot errors like semicolon missing from the above. | ||
| 240 | |||
| 241 | =head2 show_package | ||
| 242 | |||
| 243 | =over 4 | ||
| 244 | |||
| 245 | =item * | ||
| 246 | |||
| 247 | show_package I<package_name> | ||
| 248 | |||
| 249 | =back | ||
| 250 | |||
| 251 | > perl -Iblib/lib -MTerm::ShellKit -eshell "kit Dev" | ||
| 252 | Term::ShellKit: Starting interactive shell; commands include help, exit. | ||
| 253 | Activating Term::ShellKit::Commands | ||
| 254 | Activating Term::ShellKit::Dev | ||
| 255 | |||
| 256 | Term::ShellKit> show_package Carp | ||
| 257 | Package Stash for Carp | ||
| 258 | Scalars: | ||
| 259 | $CarpLevel = "0" | ||
| 260 | $MaxArgLen = "64" | ||
| 261 | $MaxArgNums = "8" | ||
| 262 | $MaxEvalLen = "0" | ||
| 263 | $Verbose = "0" | ||
| 264 | Arrays: | ||
| 265 | @EXPORT = "confess, croak, carp" | ||
| 266 | @EXPORT_FAIL = "verbose" | ||
| 267 | @EXPORT_OK = "cluck, verbose" | ||
| 268 | @ISA = "Exporter" | ||
| 269 | Hashes: | ||
| 270 | %EXPORT = "carp => 1, cluck => 1, confess => 1, croak => 1, verbose => 1" | ||
| 271 | %EXPORT_FAIL = "&verbose => 1, verbose => 1" | ||
| 272 | Subs: | ||
| 273 | sub carp = "CODEREF" | ||
| 274 | sub cluck = "CODEREF" | ||
| 275 | sub confess = "CODEREF" | ||
| 276 | sub croak = "CODEREF" | ||
| 277 | sub export_fail = "CODEREF" | ||
| 278 | sub longmess = "CODEREF" | ||
| 279 | sub shortmess = "CODEREF" | ||
| 280 | |||
| 281 | Term::ShellKit> exit | ||
| 282 | |||
| 283 | |||
| 284 | =head1 SEE ALSO | ||
| 285 | |||
| 286 | L<Term::ShellKit> | ||
| 287 | |||
| 288 | =cut |