PERL   22
Terse
Guest on 14th September 2023 01:20:57 PM


  1. package B::Terse;
  2.  
  3. our $VERSION = '1.06';
  4.  
  5. use strict;
  6. use B qw(class @specialsv_name);
  7. use B::Concise qw(concise_subref set_style_standard);
  8. use Carp;
  9.  
  10. sub terse {
  11.     my ($order, $subref) = @_;
  12.     set_style_standard("terse");
  13.     if ($order eq "exec") {
  14.         concise_subref('exec', $subref);
  15.     } else {
  16.         concise_subref('basic', $subref);
  17.     }
  18. }
  19.  
  20. sub compile {
  21.     my @args = @_;
  22.     my $order = @args ? shift(@args) : "";
  23.     $order = "-exec" if $order eq "exec";
  24.     unshift @args, $order if $order ne "";
  25.     B::Concise::compile("-terse", @args);
  26. }
  27.  
  28. sub indent {
  29.     my ($level) = @_ ? shift : 0;
  30.     return "    " x $level;
  31. }
  32.  
  33. # Don't use this, at least on OPs in subroutines: it has no way of
  34. # getting to the pad, and will give wrong answers or crash.
  35. sub B::OP::terse {
  36.     carp "B::OP::terse is deprecated; use B::Concise instead";
  37.     B::Concise::b_terse(@_);
  38. }
  39.  
  40. sub B::SV::terse {
  41.     my($sv, $level) = (@_, 0);
  42.     my %info;
  43.     B::Concise::concise_sv($sv, \%info);
  44.     my $s = indent($level)
  45.         . B::Concise::fmt_line(\%info, $sv,
  46.                                  "#svclass~(?((#svaddr))?)~#svval", 0);
  47.     chomp $s;
  48.     print "$s\n" unless defined wantarray;
  49.     $s;
  50. }
  51.  
  52. sub B::NULL::terse {
  53.     my ($sv, $level) = (@_, 0);
  54.     my $s = indent($level) . sprintf "%s (0x%lx)", class($sv), $$sv;
  55.     print "$s\n" unless defined wantarray;
  56.     $s;
  57. }
  58.  
  59. sub B::SPECIAL::terse {
  60.     my ($sv, $level) = (@_, 0);
  61.     my $s = indent($level)
  62.         . sprintf( "%s #%d %s", class($sv), $$sv, $specialsv_name[$$sv]);
  63.     print "$s\n" unless defined wantarray;
  64.     $s;
  65. }
  66.  
  67. 1;
  68.  
  69. __END__
  70.  
  71. =head1 NAME
  72.  
  73. B::Terse - Walk Perl syntax tree, printing terse info about ops
  74.  
  75. =head1 SYNOPSIS
  76.  
  77.         perl -MO=Terse[,OPTIONS] foo.pl
  78.  
  79. =head1 DESCRIPTION
  80.  
  81. This module prints the contents of the parse tree, but without as much
  82. information as L<B::Debug>.  For comparison, C<print "Hello, world.">
  83. produced 96 lines of output from B::Debug, but only 6 from B::Terse.
  84.  
  85. This module is useful for people who are writing their own back end,
  86. or who are learning about the Perl internals.  It's not useful to the
  87. average programmer.
  88.  
  89. This version of B::Terse is really just a wrapper that calls L<B::Concise>
  90. with the B<-terse> option. It is provided for compatibility with old scripts
  91. (and habits) but using B::Concise directly is now recommended instead.
  92.  
  93. For compatibility with the old B::Terse, this module also adds a
  94. method named C<terse> to B::OP and B::SV objects. The B::SV method is
  95. largely compatible with the old one, though authors of new software
  96. might be advised to choose a more user-friendly output format. The
  97. B::OP C<terse> method, however, doesn't work well. Since B::Terse was
  98. first written, much more information in OPs has migrated to the
  99. scratchpad datastructure, but the C<terse> interface doesn't have any
  100. way of getting to the correct pad. As a kludge, the new version will
  101. always use the pad for the main program, but for OPs in subroutines
  102. this will give the wrong answer or crash.
  103.  
  104. =head1 AUTHOR
  105.  
  106. The original version of B::Terse was written by Malcolm Beattie,
  107. E<lt>mbeattie@sable.ox.ac.ukE<gt>. This wrapper was written by Stephen
  108. McCamant, E<lt>smcc@MIT.EDUE<gt>.
  109.  
  110. =cut

Raw Paste

Login or Register to edit or fork this paste. It's free.