| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | =head1 NAME | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | PDL::Dbg - functions to support debugging of PDL scripts | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | use PDL; | 
| 8 |  |  |  |  |  |  | use PDL::Dbg; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | $c = $x->slice("5:10,2:30")->px->diagonal(3,4); | 
| 11 |  |  |  |  |  |  | PDL->px; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | These packages implements a couple of functions that should come in | 
| 16 |  |  |  |  |  |  | handy when debugging your PDL scripts. They make a lot of sense while | 
| 17 |  |  |  |  |  |  | you're doing rapid prototyping of new PDL code, let's say inside the | 
| 18 |  |  |  |  |  |  | perldl or pdl2 shell. | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =cut | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | #' fool emacs | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | package PDL::Dbg; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # used by info | 
| 27 |  |  |  |  |  |  | $PDL::Dbg::Title = "Type   Dimension       Flow  State          Mem"; | 
| 28 |  |  |  |  |  |  | $PDL::Dbg::Infostr = "%6T %-15D  %3F   %-5S  %12M"; | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | package PDL; | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =head1 FUNCTIONS | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | =head2 px | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | =for ref | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | Print info about a piddle (or all known piddles) | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | =for example | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | pdl> PDL->px | 
| 43 |  |  |  |  |  |  | pdl> $y += $x->clump(2)->px('clumptest')->sumover | 
| 44 |  |  |  |  |  |  | pdl> $x->px('%C (%A) Type: %T') # prints nothing unless $PDL::debug | 
| 45 |  |  |  |  |  |  | pdl> $PDL::debug = 1 | 
| 46 |  |  |  |  |  |  | pdl>  $x->px('%C (%A) Type: %T') | 
| 47 |  |  |  |  |  |  | PDL (52433464) Type: Double | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | This function prints some information about piddles. It can be invoked | 
| 51 |  |  |  |  |  |  | as a class method (e.g. Cpx> ) or as an instance method (e.g. | 
| 52 |  |  |  |  |  |  | C<$pdl-Epx($arg)>). If | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | =over 2 | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | =item invoked as a class method | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | it prints info about all piddles found in the current package | 
| 59 |  |  |  |  |  |  | (I C variables). This comes in quite handy when you are | 
| 60 |  |  |  |  |  |  | not quite sure which pdls you have already defined, what data they | 
| 61 |  |  |  |  |  |  | hold , etc. C is supposed to support inheritance and prints info | 
| 62 |  |  |  |  |  |  | about all symbols for which an C is true. An optional | 
| 63 |  |  |  |  |  |  | string argument is interpreted as the package name for which to print | 
| 64 |  |  |  |  |  |  | symbols: | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | pdl> PDL->px('PDL::Mypack') | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | The default package is that of the caller. | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =item invoked as an instance method | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | it prints info about that particular piddle if C<$PDL::debug> is | 
| 73 |  |  |  |  |  |  | true and returns the pdl object upon completion. It accepts an | 
| 74 |  |  |  |  |  |  | optional string argument that is simply prepended to the default info | 
| 75 |  |  |  |  |  |  | if it doesn't contain a C<%> character. If, however, the argument | 
| 76 |  |  |  |  |  |  | contains a C<%> then the string is passed to the C method to | 
| 77 |  |  |  |  |  |  | control the format of the printed information. This can be used to | 
| 78 |  |  |  |  |  |  | achieve customized output from C. See the documentation of | 
| 79 |  |  |  |  |  |  | C for further details. | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | =back | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | The output of px will be determined by the default formatting string | 
| 84 |  |  |  |  |  |  | that is passed to the C method (unless you pass a string | 
| 85 |  |  |  |  |  |  | containing C<%> to px when invoking as an instance method, see | 
| 86 |  |  |  |  |  |  | above). This default string is stored in C<$PDL::Dbg::Infostr> and the | 
| 87 |  |  |  |  |  |  | default output format can be accordingly changed by setting this | 
| 88 |  |  |  |  |  |  | variable.  If you do this you should also change the default title | 
| 89 |  |  |  |  |  |  | string that the class method branch prints at the top of the listing | 
| 90 |  |  |  |  |  |  | to match your new format string. The default title is stored in the | 
| 91 |  |  |  |  |  |  | variable C<$PDL::Dbg::Title>. | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | For historical reasons C is an alias for C. | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | =cut | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | sub px { | 
| 98 | 8 |  |  | 8 | 0 | 406 | my $arg = shift; | 
| 99 | 8 |  |  |  |  | 18 | my $str=""; | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 8 | 50 |  |  |  | 26 | if (ref($arg)) { | 
| 102 | 8 | 100 |  |  |  | 27 | return $arg unless $PDL::debug; | 
| 103 | 6 | 0 |  |  |  | 36 | my $info = $arg->info($#_ > -1 ? ($_[0] =~ /%/ ? | 
|  |  | 50 |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | $_[0] : "$_[0] $PDL::Dbg::Infostr") : | 
| 105 |  |  |  |  |  |  | $PDL::Dbg::Infostr); | 
| 106 | 6 |  |  |  |  | 265 | print "$info\n"; | 
| 107 | 6 |  |  |  |  | 45 | return $arg; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | # we have been called as a class method | 
| 111 | 0 | 0 |  |  |  |  | my $package = $#_ > -1 ? shift : caller; | 
| 112 | 0 |  |  |  |  |  | my $classname = $arg; | 
| 113 |  |  |  |  |  |  | # find the correct package | 
| 114 | 0 | 0 |  |  |  |  | $package .= "::" unless $package =~ /::$/; | 
| 115 | 0 |  |  |  |  |  | *stab = *{"main::"}; | 
|  | 0 |  |  |  |  |  |  | 
| 116 | 0 |  |  |  |  |  | while ($package =~ /(\w+?::)/g){ | 
| 117 | 0 |  |  |  |  |  | *stab = $ {stab}{$1}; | 
| 118 |  |  |  |  |  |  | } | 
| 119 | 0 |  |  |  |  |  | print "$classname variables in package $package\n\n"; | 
| 120 | 0 |  |  |  |  |  | my $title = "Name         $PDL::Dbg::Title\n"; | 
| 121 | 0 |  |  |  |  |  | print $title; | 
| 122 | 0 |  |  |  |  |  | print '-'x(length($title)+3)."\n"; | 
| 123 | 0 |  |  |  |  |  | my ($pdl,$npdls,$key,$val,$info) = ((),0,"","",""); | 
| 124 |  |  |  |  |  |  | # while (($key,$val) = each(%stab)) { | 
| 125 | 0 |  |  |  |  |  | foreach $key ( sort { lc($a) cmp lc($b) } keys(%stab) ) { | 
|  | 0 |  |  |  |  |  |  | 
| 126 | 0 |  |  |  |  |  | $val = $stab{$key}; | 
| 127 | 0 |  |  |  |  |  | $pdl = ${"$package$key"}; | 
|  | 0 |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | # print info for all objects derived from this class | 
| 129 | 0 | 0 |  |  |  |  | if (UNIVERSAL::isa($pdl,$classname)) { | 
| 130 | 0 |  |  |  |  |  | $npdls++; | 
| 131 | 0 |  |  |  |  |  | $info = $pdl->info($PDL::Dbg::Infostr); | 
| 132 | 0 | 0 |  |  |  |  | printf "\$%-11s %s %s\n",$key,$info,(ref($pdl) eq $classname ? '' : | 
| 133 |  |  |  |  |  |  | ref($pdl)); | 
| 134 |  |  |  |  |  |  | # also print classname for derived classes | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  | } | 
| 137 | 0 | 0 |  |  |  |  | print "no $classname objects in package $package\n" | 
| 138 |  |  |  |  |  |  | unless $npdls; | 
| 139 | 0 |  |  |  |  |  | return $arg; | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =head2 vars | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =for ref | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | Alias for C | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =cut | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | # make vars an alias | 
| 151 |  |  |  |  |  |  | # I hope this works with inheritance | 
| 152 |  |  |  |  |  |  | *vars = \&px; | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | 1; # return success | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | =head1 BUGS | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | There are probably some. Please report if you find any. Bug reports | 
| 159 |  |  |  |  |  |  | should be sent to the PDL mailing list pdl-general@lists.sourceforge.net. | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =head1 AUTHOR | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | Copyright(C) 1997 Christian Soeller (c.soeller@auckland.ac.nz). | 
| 164 |  |  |  |  |  |  | All rights reserved. There is no warranty. You are allowed | 
| 165 |  |  |  |  |  |  | to redistribute this software / documentation under certain | 
| 166 |  |  |  |  |  |  | conditions. For details, see the file COPYING in the PDL | 
| 167 |  |  |  |  |  |  | distribution. If this file is separated from the PDL distribution, | 
| 168 |  |  |  |  |  |  | the copyright notice should be included in the file. | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  |  |