Skip to content

Commit 02ca8dc

Browse files
committed
simplify usage_string, nicer output
1 parent 09e4f1b commit 02ca8dc

1 file changed

Lines changed: 33 additions & 36 deletions

File tree

‎lib/PDL/Doc/Perldl.pm‎

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -388,33 +388,30 @@ Prints usage information for a PDL function
388388
=cut
389389

390390
sub usage {
391-
die 'Usage: usage $funcname' unless $#_>-1;
392-
print usage_string(@_);
391+
die 'Usage: usage $funcname' unless $#_>-1;
392+
print usage_string(@_);
393+
''
393394
}
394-
sub usage_string{
395-
my $func = shift;
396-
my $str = "";
395+
sub usage_string {
396+
my $func = shift;
397+
my $str = "";
398+
return "no match\n" unless
397399
my @match = search_docs("m/^(PDL::)?$func\$|\:\:$func\$/",['Name']);
398-
my $count = @match;
399-
unless ($count) { $str = "\n no match\n" }
400-
else {
401-
#this sorts by namespace depth by counting colons in the name.
402-
#PDL::Ufunc::max comes before PDL::GSL::RNG::max, for example.
403-
foreach my $m(sort { scalar(()=$a->[1]=~/\:/g) <=> scalar(()=$b->[1]=~/\:/g) } @match){
404-
$str .= "\n" . format_ref( $m );
405-
my ($name,$module,$hash) = @{$m};
406-
#$str .= sprintf ( (' 'x16)."(Module %s)\n\n", $hash->{Module} );
407-
$str.="\n";
408-
die "No usage info found for $func\n" if (!defined $hash->{Example} && !defined $hash->{Sig} &&
409-
!defined $hash->{Usage});
410-
$str .= " Signature: $name($hash->{Sig})\n\n" if defined $hash->{Sig};
411-
for (['Usage','Usage'],['Opt','Options'],['Example','Example']) {
412-
$str .= " $_->[1]:\n".&allindent($hash->{$_->[0]},10)."\n" if defined $hash->{$_->[0]};
413-
}
414-
$str .= '='x20 unless 1==$count--;
415-
}
400+
my $count = @match;
401+
#this sorts by namespace depth by counting colons in the name.
402+
#PDL::Ufunc::max comes before PDL::GSL::RNG::max, for example.
403+
foreach my $m (sort { scalar(()=$a->[1]=~/\:/g) <=> scalar(()=$b->[1]=~/\:/g) } @match) {
404+
$str .= "\n" . format_ref( $m );
405+
my ($name,$module,$hash) = @$m;
406+
die "No usage info found for $func\n" if !grep defined, @$hash{qw(Example Sig Usage)};
407+
for (grep $hash->{$_->[0]},
408+
['Sig','Signature'],['Usage','Usage'],['Opt','Options'],['Example','Example']
409+
) {
410+
$str .= " $_->[1]:\n".allindent($hash->{$_->[0]},4)."\n";
416411
}
417-
return $str;
412+
$str .= '='x20 unless 1==$count--;
413+
}
414+
$str;
418415
}
419416

420417
=head2 sig
@@ -454,17 +451,17 @@ sub sig {
454451
}
455452

456453
sub allindent {
457-
my ($txt,$n) = @_;
458-
my ($ntxt,$tspc) = ($txt,' 'x8);
459-
$ntxt =~ s/^\s*$//mg;
460-
$ntxt =~ s/\t/$tspc/g;
461-
my $minspc = length $txt;
462-
for (split '\n', $txt) { if (/^(\s*)/)
463-
{ $minspc = length $1 if length $1 < $minspc } }
464-
$n -= $minspc;
465-
$tspc = ' 'x abs($n);
466-
$ntxt =~ s/^/$tspc/mg if $n > 0;
467-
return $ntxt;
454+
my ($txt,$n) = @_;
455+
my ($ntxt,$tspc) = ($txt,' 'x8);
456+
$ntxt =~ s/^\s*$//mg;
457+
$ntxt =~ s/\t/$tspc/g;
458+
my $minspc = length $txt;
459+
for (split '\n', $txt) { if (/^(\s*)/)
460+
{ $minspc = length $1 if length $1 < $minspc } }
461+
$n -= $minspc;
462+
$tspc = ' 'x abs($n);
463+
$ntxt =~ s/^/$tspc/mg if $n > 0;
464+
$ntxt;
468465
}
469466

470467

@@ -661,7 +658,7 @@ And has a horrible name.
661658
=cut
662659

663660
# need to get this to format the output - want a format_bad()
664-
# subroutine that's like - but much simpler - than format_ref()
661+
# subroutine that's like - but much simpler than - format_ref()
665662
#
666663
sub badinfo {
667664
my $func = shift;

0 commit comments

Comments
 (0)