File Coverage

blib/lib/PDL/Dbg.pm
Criterion Covered Total %
statement 13 37 35.1
branch 2 18 11.1
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 19 60 31.6


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