This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add a few helpers to B for INVLIST
[perl5.git] / ext / B / B.pm
CommitLineData
a798dbf2
MB
1# B.pm
2#
1a52ab62 3# Copyright (c) 1996, 1997, 1998 Malcolm Beattie
a798dbf2
MB
4#
5# You may distribute under the terms of either the GNU General Public
6# License or the Artistic License, as specified in the README file.
7#
8package B;
28b605d8 9
744aaba0 10@B::ISA = qw(Exporter);
b2590c4e 11
95d444af
TR
12# If B is loaded without imports, we do not want to unnecessarily pollute the stash with Exporter.
13sub import {
14 return unless scalar @_ > 1; # Called as a method call.
15 require Exporter;
16 B->export_to_level(1, @_);
17}
18
f72d64f0
DC
19# walkoptree_slow comes from B.pm (you are there),
20# walkoptree comes from B.xs
744aaba0
NC
21
22BEGIN {
47e6c6d9 23 $B::VERSION = '1.82';
4aa23ba6 24 @B::EXPORT_OK = ();
744aaba0 25
4aa23ba6
NC
26 # Our BOOT code needs $VERSION set, and will append to @EXPORT_OK.
27 # Want our constants loaded before the compiler meets OPf_KIDS below, as
28 # the combination of having the constant stay a Proxy Constant Subroutine
29 # and its value being inlined saves a little over .5K
744aaba0 30
744aaba0
NC
31 require XSLoader;
32 XSLoader::load();
33}
34
4aa23ba6
NC
35push @B::EXPORT_OK, (qw(minus_c ppname save_BEGINs
36 class peekop cast_I32 cstring cchar hash threadsv_names
37 main_root main_start main_cv svref_2object opnumber
38 sub_generation amagic_generation perlstring
39 walkoptree_slow walkoptree walkoptree_exec walksymtable
40 parents comppadlist sv_undef compile_stats timing_info
41 begin_av init_av check_av end_av regex_padav dowarn
42 defstash curstash warnhook diehook inc_gv @optype
d9cd2aeb 43 @specialsv_name unitcheck_av safename));
4aa23ba6 44
a798dbf2
MB
45@B::SV::ISA = 'B::OBJECT';
46@B::NULL::ISA = 'B::SV';
47@B::PV::ISA = 'B::SV';
48@B::IV::ISA = 'B::SV';
4edc9001 49@B::NV::ISA = 'B::SV';
4df7f6af 50# RV is eliminated with 5.11.0, but effectively is a specialisation of IV now.
eb32218e 51@B::RV::ISA = 'B::IV';
a798dbf2 52@B::PVIV::ISA = qw(B::PV B::IV);
4edc9001 53@B::PVNV::ISA = qw(B::PVIV B::NV);
a798dbf2 54@B::PVMG::ISA = 'B::PVNV';
eb32218e
Z
55@B::REGEXP::ISA = 'B::PVMG';
56@B::INVLIST::ISA = 'B::PV';
35633035
DM
57@B::PVLV::ISA = 'B::GV';
58@B::BM::ISA = 'B::GV';
a798dbf2
MB
59@B::AV::ISA = 'B::PVMG';
60@B::GV::ISA = 'B::PVMG';
61@B::HV::ISA = 'B::PVMG';
62@B::CV::ISA = 'B::PVMG';
276493cb
SM
63@B::IO::ISA = 'B::PVMG';
64@B::FM::ISA = 'B::CV';
a798dbf2
MB
65
66@B::OP::ISA = 'B::OBJECT';
67@B::UNOP::ISA = 'B::OP';
2f7c6295 68@B::UNOP_AUX::ISA = 'B::UNOP';
a798dbf2
MB
69@B::BINOP::ISA = 'B::UNOP';
70@B::LOGOP::ISA = 'B::UNOP';
a798dbf2
MB
71@B::LISTOP::ISA = 'B::BINOP';
72@B::SVOP::ISA = 'B::OP';
7934575e 73@B::PADOP::ISA = 'B::OP';
a798dbf2 74@B::PVOP::ISA = 'B::OP';
a798dbf2
MB
75@B::LOOP::ISA = 'B::LISTOP';
76@B::PMOP::ISA = 'B::LISTOP';
77@B::COP::ISA = 'B::OP';
b46e009d 78@B::METHOP::ISA = 'B::OP';
a798dbf2
MB
79
80@B::SPECIAL::ISA = 'B::OBJECT';
81
602bd5cc 82our @optype = qw(OP UNOP BINOP LOGOP LISTOP PMOP SVOP PADOP PVOP LOOP COP
2f7c6295 83 METHOP UNOP_AUX);
baccf54f
NC
84# bytecode.pl contained the following comment:
85# Nullsv *must* come first in the following so that the condition
86# ($$sv == 0) can continue to be used to test (sv == Nullsv).
602bd5cc 87our @specialsv_name = qw(Nullsv &PL_sv_undef &PL_sv_yes &PL_sv_no
b043c4bf
DM
88 (SV*)pWARN_ALL (SV*)pWARN_NONE (SV*)pWARN_STD
89 &PL_sv_zero);
baccf54f 90
a798dbf2
MB
91{
92 # Stop "-w" from complaining about the lack of a real B::OBJECT class
93 package B::OBJECT;
94}
95
002b978b 96sub B::GV::SAFENAME {
d9cd2aeb
FC
97 safename(shift()->NAME);
98}
99
100sub safename {
101 my $name = shift;
d9963e60
RH
102
103 # The regex below corresponds to the isCONTROLVAR macro
104 # from toke.c
105
2bd1cbf6
KW
106 $name =~ s/^\c?/^?/
107 or $name =~ s/^([\cA-\cZ\c\\c[\c]\c_\c^])/
108 "^" . chr( utf8::unicode_to_native( 64 ^ ord($1) ))/e;
7a9b44b9
RH
109
110 # When we say unicode_to_native we really mean ascii_to_native,
2bd1cbf6
KW
111 # which matters iff this is a non-ASCII platform (EBCDIC). '\c?' would
112 # not have to be special cased, except for non-ASCII.
7a9b44b9 113
002b978b
RH
114 return $name;
115}
116
d9963e60
RH
117sub B::IV::int_value {
118 my ($self) = @_;
119 return (($self->FLAGS() & SVf_IVisUV()) ? $self->UVX : $self->IV);
120}
121
f3402b25 122sub B::NULL::as_string() {""}
602bd5cc
TR
123*B::IV::as_string = *B::IV::as_string = \*B::IV::int_value;
124*B::PV::as_string = *B::PV::as_string = \*B::PV::PV;
f3402b25 125
ff1a9fc0
NC
126# The input typemap checking makes no distinction between different SV types,
127# so the XS body will generate the same C code, despite the different XS
128# "types". So there is no change in behaviour from doing "newXS" like this,
129# compared with the old approach of having a (near) duplicate XS body.
130# We should fix the typemap checking.
602bd5cc
TR
131
132# Since perl 5.12.0
133*B::IV::RV = *B::IV::RV = \*B::PV::RV;
ff1a9fc0 134
a798dbf2
MB
135my $debug;
136my $op_count = 0;
137my @parents = ();
138
139sub debug {
140 my ($class, $value) = @_;
141 $debug = $value;
142 walkoptree_debug($value);
143}
144
a798dbf2
MB
145sub class {
146 my $obj = shift;
147 my $name = ref $obj;
148 $name =~ s/^.*:://;
149 return $name;
150}
151
152sub parents { \@parents }
153
154# For debugging
155sub peekop {
156 my $op = shift;
3f872cb9 157 return sprintf("%s (0x%x) %s", class($op), $$op, $op->name);
a798dbf2
MB
158}
159
b2590c4e 160sub walkoptree_slow {
a798dbf2
MB
161 my($op, $method, $level) = @_;
162 $op_count++; # just for statistics
163 $level ||= 0;
164 warn(sprintf("walkoptree: %d. %s\n", $level, peekop($op))) if $debug;
156f89f0 165 $op->$method($level) if $op->can($method);
a798dbf2
MB
166 if ($$op && ($op->flags & OPf_KIDS)) {
167 my $kid;
168 unshift(@parents, $op);
169 for ($kid = $op->first; $$kid; $kid = $kid->sibling) {
b2590c4e 170 walkoptree_slow($kid, $method, $level + 1);
a798dbf2
MB
171 }
172 shift @parents;
173 }
156f89f0
JJ
174 if (class($op) eq 'PMOP'
175 && ref($op->pmreplroot)
176 && ${$op->pmreplroot}
177 && $op->pmreplroot->isa( 'B::OP' ))
178 {
0091380b
RGS
179 unshift(@parents, $op);
180 walkoptree_slow($op->pmreplroot, $method, $level + 1);
181 shift @parents;
182 }
a798dbf2
MB
183}
184
185sub compile_stats {
186 return "Total number of OPs processed: $op_count\n";
187}
188
189sub timing_info {
190 my ($sec, $min, $hr) = localtime;
191 my ($user, $sys) = times;
192 sprintf("%02d:%02d:%02d user=$user sys=$sys",
193 $hr, $min, $sec, $user, $sys);
194}
195
196my %symtable;
2b8dc4d2
DM
197
198sub clearsym {
199 %symtable = ();
200}
201
a798dbf2
MB
202sub savesym {
203 my ($obj, $value) = @_;
204# warn(sprintf("savesym: sym_%x => %s\n", $$obj, $value)); # debug
205 $symtable{sprintf("sym_%x", $$obj)} = $value;
206}
207
208sub objsym {
209 my $obj = shift;
210 return $symtable{sprintf("sym_%x", $$obj)};
211}
212
213sub walkoptree_exec {
214 my ($op, $method, $level) = @_;
244826eb 215 $level ||= 0;
a798dbf2
MB
216 my ($sym, $ppname);
217 my $prefix = " " x $level;
218 for (; $$op; $op = $op->next) {
219 $sym = objsym($op);
220 if (defined($sym)) {
221 print $prefix, "goto $sym\n";
222 return;
223 }
224 savesym($op, sprintf("%s (0x%lx)", class($op), $$op));
225 $op->$method($level);
3f872cb9 226 $ppname = $op->name;
1a67a97c 227 if ($ppname =~
62e36f8a 228 /^(d?or(assign)?|and(assign)?|mapwhile|grepwhile|entertry|range|cond_expr)$/)
1a67a97c 229 {
a798dbf2
MB
230 print $prefix, uc($1), " => {\n";
231 walkoptree_exec($op->other, $method, $level + 1);
232 print $prefix, "}\n";
3f872cb9 233 } elsif ($ppname eq "match" || $ppname eq "subst") {
a798dbf2
MB
234 my $pmreplstart = $op->pmreplstart;
235 if ($$pmreplstart) {
236 print $prefix, "PMREPLSTART => {\n";
237 walkoptree_exec($pmreplstart, $method, $level + 1);
238 print $prefix, "}\n";
239 }
3f872cb9 240 } elsif ($ppname eq "substcont") {
a798dbf2
MB
241 print $prefix, "SUBSTCONT => {\n";
242 walkoptree_exec($op->other->pmreplstart, $method, $level + 1);
243 print $prefix, "}\n";
244 $op = $op->other;
3f872cb9 245 } elsif ($ppname eq "enterloop") {
a798dbf2
MB
246 print $prefix, "REDO => {\n";
247 walkoptree_exec($op->redoop, $method, $level + 1);
248 print $prefix, "}\n", $prefix, "NEXT => {\n";
249 walkoptree_exec($op->nextop, $method, $level + 1);
250 print $prefix, "}\n", $prefix, "LAST => {\n";
251 walkoptree_exec($op->lastop, $method, $level + 1);
252 print $prefix, "}\n";
3f872cb9 253 } elsif ($ppname eq "subst") {
a798dbf2
MB
254 my $replstart = $op->pmreplstart;
255 if ($$replstart) {
256 print $prefix, "SUBST => {\n";
257 walkoptree_exec($replstart, $method, $level + 1);
258 print $prefix, "}\n";
259 }
260 }
261 }
262}
263
264sub walksymtable {
265 my ($symref, $method, $recurse, $prefix) = @_;
266 my $sym;
b6b0fb7b
MB
267 my $fullname;
268 no strict 'refs';
0cc1d052 269 $prefix = '' unless defined $prefix;
5cc8528c 270 foreach my $sym ( sort keys %$symref ) {
f4a37198
FC
271 my $dummy = $symref->{$sym}; # Copying the glob and incrementing
272 # the GPs refcnt clears cached methods
b6b0fb7b 273 $fullname = "*main::".$prefix.$sym;
a798dbf2
MB
274 if ($sym =~ /::$/) {
275 $sym = $prefix . $sym;
7834d9fb 276 if (svref_2object(\*$sym)->NAME ne "main::" && $sym ne "<none>::" && &$recurse($sym)) {
b6b0fb7b 277 walksymtable(\%$fullname, $method, $recurse, $sym);
a798dbf2
MB
278 }
279 } else {
b6b0fb7b 280 svref_2object(\*$fullname)->$method();
a798dbf2
MB
281 }
282 }
283}
284
a798dbf2 2851;
7f20e9dd
GS
286
287__END__
288
289=head1 NAME
290
4b661dd3 291B - The Perl Compiler Backend
7f20e9dd
GS
292
293=head1 SYNOPSIS
294
295 use B;
296
297=head1 DESCRIPTION
298
1a52ab62 299The C<B> module supplies classes which allow a Perl program to delve
130592f5
FC
300into its own innards. It is the module used to implement the
301"backends" of the Perl compiler. Usage of the compiler does not
1a52ab62 302require knowledge of this module: see the F<O> module for the
130592f5
FC
303user-visible part. The C<B> module is of use to those who want to
304write new compiler backends. This documentation assumes that the
1a52ab62
MB
305reader knows a fair amount about perl's internals including such
306things as SVs, OPs and the internal symbol table and syntax tree
307of a program.
308
85cf7f2e
MJD
309=head1 OVERVIEW
310
311The C<B> module contains a set of utility functions for querying the
312current state of the Perl interpreter; typically these functions
313return objects from the B::SV and B::OP classes, or their derived
314classes. These classes in turn define methods for querying the
315resulting objects about their own internal state.
316
317=head1 Utility Functions
318
319The C<B> module exports a variety of functions: some are simple
320utility functions, others provide a Perl program with a way to
321get an initial "handle" on an internal object.
322
323=head2 Functions Returning C<B::SV>, C<B::AV>, C<B::HV>, and C<B::CV> objects
324
3d036c2b 325For descriptions of the class hierarchy of these objects and the
85cf7f2e
MJD
326methods that can be called on them, see below, L<"OVERVIEW OF
327CLASSES"> and L<"SV-RELATED CLASSES">.
328
329=over 4
330
331=item sv_undef
332
333Returns the SV object corresponding to the C variable C<sv_undef>.
334
335=item sv_yes
336
337Returns the SV object corresponding to the C variable C<sv_yes>.
338
339=item sv_no
340
341Returns the SV object corresponding to the C variable C<sv_no>.
342
343=item svref_2object(SVREF)
344
345Takes a reference to any Perl value, and turns the referred-to value
346into an object in the appropriate B::OP-derived or B::SV-derived
130592f5 347class. Apart from functions such as C<main_root>, this is the primary
85cf7f2e
MJD
348way to get an initial "handle" on an internal perl data structure
349which can then be followed with the other access methods.
350
f31c3107 351The returned object will only be valid as long as the underlying OPs
130592f5 352and SVs continue to exist. Do not attempt to use the object after the
f31c3107
SM
353underlying structures are freed.
354
85cf7f2e
MJD
355=item amagic_generation
356
357Returns the SV object corresponding to the C variable C<amagic_generation>.
66978156
FC
358As of Perl 5.18, this is just an alias to C<PL_na>, so its value is
359meaningless.
85cf7f2e 360
e13efe3c 361=item init_av
85cf7f2e
MJD
362
363Returns the AV object (i.e. in class B::AV) representing INIT blocks.
364
ece599bd
RGS
365=item check_av
366
367Returns the AV object (i.e. in class B::AV) representing CHECK blocks.
368
676456c2
AG
369=item unitcheck_av
370
371Returns the AV object (i.e. in class B::AV) representing UNITCHECK blocks.
372
85cf7f2e
MJD
373=item begin_av
374
375Returns the AV object (i.e. in class B::AV) representing BEGIN blocks.
376
377=item end_av
378
379Returns the AV object (i.e. in class B::AV) representing END blocks.
380
381=item comppadlist
382
3a910aa0
FC
383Returns the PADLIST object (i.e. in class B::PADLIST) of the global
384comppadlist. In Perl 5.16 and earlier it returns an AV object (class
385B::AV).
85cf7f2e
MJD
386
387=item regex_padav
388
389Only when perl was compiled with ithreads.
390
e13efe3c 391=item main_cv
85cf7f2e
MJD
392
393Return the (faked) CV corresponding to the main part of the Perl
394program.
395
396=back
397
398=head2 Functions for Examining the Symbol Table
399
400=over 4
401
402=item walksymtable(SYMREF, METHOD, RECURSE, PREFIX)
403
404Walk the symbol table starting at SYMREF and call METHOD on each
405symbol (a B::GV object) visited. When the walk reaches package
406symbols (such as "Foo::") it invokes RECURSE, passing in the symbol
407name, and only recurses into the package if that sub returns true.
408
409PREFIX is the name of the SYMREF you're walking.
410
411For example:
412
413 # Walk CGI's symbol table calling print_subs on each symbol.
414 # Recurse only into CGI::Util::
26d2adad
FC
415 walksymtable(\%CGI::, 'print_subs',
416 sub { $_[0] eq 'CGI::Util::' }, 'CGI::');
85cf7f2e 417
130592f5 418print_subs() is a B::GV method you have declared. Also see L<"B::GV
85cf7f2e
MJD
419Methods">, below.
420
421=back
422
423=head2 Functions Returning C<B::OP> objects or for walking op trees
424
3d036c2b 425For descriptions of the class hierarchy of these objects and the
85cf7f2e
MJD
426methods that can be called on them, see below, L<"OVERVIEW OF
427CLASSES"> and L<"OP-RELATED CLASSES">.
428
429=over 4
430
431=item main_root
432
433Returns the root op (i.e. an object in the appropriate B::OP-derived
434class) of the main part of the Perl program.
435
436=item main_start
437
438Returns the starting op of the main part of the Perl program.
439
440=item walkoptree(OP, METHOD)
441
442Does a tree-walk of the syntax tree based at OP and calls METHOD on
130592f5 443each op it visits. Each node is visited before its children. If
85cf7f2e
MJD
444C<walkoptree_debug> (see below) has been called to turn debugging on then
445the method C<walkoptree_debug> is called on each op before METHOD is
446called.
447
448=item walkoptree_debug(DEBUG)
449
130592f5
FC
450Returns the current debugging flag for C<walkoptree>. If the optional
451DEBUG argument is non-zero, it sets the debugging flag to that. See
85cf7f2e
MJD
452the description of C<walkoptree> above for what the debugging flag
453does.
454
455=back
456
457=head2 Miscellaneous Utility Functions
458
459=over 4
460
461=item ppname(OPNUM)
462
463Return the PP function name (e.g. "pp_add") of op number OPNUM.
464
465=item hash(STR)
466
467Returns a string in the form "0x..." representing the value of the
468internal hash function used by perl on string STR.
469
470=item cast_I32(I)
471
472Casts I to the internal I32 type used by that perl.
473
474=item minus_c
475
130592f5 476Does the equivalent of the C<-c> command-line option. Obviously, this
85cf7f2e
MJD
477is only useful in a BEGIN block or else the flag is set too late.
478
479=item cstring(STR)
480
481Returns a double-quote-surrounded escaped version of STR which can
482be used as a string in C source code.
483
484=item perlstring(STR)
485
486Returns a double-quote-surrounded escaped version of STR which can
487be used as a string in Perl source code.
488
d9cd2aeb
FC
489=item safename(STR)
490
491This function returns the string with the first character modified if it
492is a control character. It converts it to ^X format first, so that "\cG"
493becomes "^G". This is used internally by L<B::GV::SAFENAME|/SAFENAME>, but
494you can call it directly.
495
85cf7f2e
MJD
496=item class(OBJ)
497
498Returns the class of an object without the part of the classname
130592f5 499preceding the first C<"::">. This is used to turn C<"B::UNOP"> into
85cf7f2e
MJD
500C<"UNOP"> for example.
501
502=item threadsv_names
503
dacd2ca7
DM
504This used to provide support for the old 5.005 threading module. It now
505does nothing.
85cf7f2e
MJD
506
507=back
508
4082acab 509=head2 Exported utility variables
baccf54f
NC
510
511=over 4
512
513=item @optype
514
515 my $op_type = $optype[$op_type_num];
85cf7f2e 516
baccf54f
NC
517A simple mapping of the op type number to its type (like 'COP' or 'BINOP').
518
519=item @specialsv_name
520
521 my $sv_name = $specialsv_name[$sv_index];
522
523Certain SV types are considered 'special'. They're represented by
524B::SPECIAL and are referred to by a number from the specialsv_list.
525This array maps that number back to the name of the SV (like 'Nullsv'
526or '&PL_sv_undef').
527
528=back
85cf7f2e
MJD
529
530
1a52ab62
MB
531=head1 OVERVIEW OF CLASSES
532
533The C structures used by Perl's internals to hold SV and OP
534information (PVIV, AV, HV, ..., OP, SVOP, UNOP, ...) are modelled on a
535class hierarchy and the C<B> module gives access to them via a true
130592f5 536object hierarchy. Structure fields which point to other objects
1a52ab62 537(whether types of SV or types of OP) are represented by the C<B>
85cf7f2e
MJD
538module as Perl objects of the appropriate class.
539
540The bulk of the C<B> module is the methods for accessing fields of
541these structures.
542
543Note that all access is read-only. You cannot modify the internals by
130592f5 544using this module. Also, note that the B::OP and B::SV objects created
f31c3107
SM
545by this module are only valid for as long as the underlying objects
546exist; their creation doesn't increase the reference counts of the
130592f5 547underlying objects. Trying to access the fields of a freed object will
f31c3107 548give incomprehensible results, or worse.
1a52ab62
MB
549
550=head2 SV-RELATED CLASSES
551
eb32218e
Z
552B::IV, B::NV, B::PV, B::PVIV, B::PVNV, B::PVMG,
553B::PVLV, B::AV, B::HV, B::CV, B::GV, B::FM, B::IO. These classes
6822775c 554correspond in the obvious way to the underlying C structures of similar names.
eb32218e 555The inheritance hierarchy mimics the underlying C "inheritance":
dda36756
NC
556
557 B::SV
558 |
559 +------------+------------+
560 | | |
561 B::PV B::IV B::NV
47e6c6d9
N
562 / \ / /
563 / \ / /
564 B::INVLIST B::PVIV /
dda36756
NC
565 \ /
566 \ /
567 \ /
568 B::PVNV
569 |
570 |
571 B::PVMG
572 |
573 +-------+-------+---+---+-------+-------+
574 | | | | | |
575 B::AV B::GV B::HV B::CV B::IO B::REGEXP
576 | |
577 | |
578 B::PVLV B::FM
579
f5ba1307 580
85cf7f2e 581Access methods correspond to the underlying C macros for field access,
1a52ab62 582usually with the leading "class indication" prefix removed (Sv, Av,
130592f5
FC
583Hv, ...). The leading prefix is only left in cases where its removal
584would cause a clash in method name. For example, C<GvREFCNT> stays
1a52ab62
MB
585as-is since its abbreviation would clash with the "superclass" method
586C<REFCNT> (corresponding to the C function C<SvREFCNT>).
587
85cf7f2e 588=head2 B::SV Methods
1a52ab62
MB
589
590=over 4
591
592=item REFCNT
593
594=item FLAGS
595
429a5ce7
SM
596=item object_2svref
597
598Returns a reference to the regular scalar corresponding to this
130592f5
FC
599B::SV object. In other words, this method is the inverse operation
600to the svref_2object() subroutine. This scalar and other data it points
429a5ce7
SM
601at should be considered read-only: modifying them is neither safe nor
602guaranteed to have a sensible effect.
603
1a52ab62
MB
604=back
605
85cf7f2e 606=head2 B::IV Methods
1a52ab62
MB
607
608=over 4
609
610=item IV
611
d9963e60 612Returns the value of the IV, I<interpreted as
130592f5
FC
613a signed integer>. This will be misleading
614if C<FLAGS & SVf_IVisUV>. Perhaps you want the
d9963e60
RH
615C<int_value> method instead?
616
1a52ab62
MB
617=item IVX
618
d9963e60
RH
619=item UVX
620
621=item int_value
622
623This method returns the value of the IV as an integer.
624It differs from C<IV> in that it returns the correct
625value regardless of whether it's stored signed or
626unsigned.
627
1a52ab62
MB
628=item needs64bits
629
630=item packiv
631
632=back
633
85cf7f2e 634=head2 B::NV Methods
1a52ab62
MB
635
636=over 4
637
638=item NV
639
640=item NVX
641
43e81a9a
FC
642=item COP_SEQ_RANGE_LOW
643
644=item COP_SEQ_RANGE_HIGH
645
dbac5ffe
FC
646These last two are only valid for pad name SVs. They only existed in the
647B::NV class before Perl 5.22. In 5.22 they were moved to the B::PADNAME
648class.
43e81a9a 649
1a52ab62
MB
650=back
651
85cf7f2e 652=head2 B::RV Methods
1a52ab62
MB
653
654=over 4
655
656=item RV
657
658=back
659
85cf7f2e 660=head2 B::PV Methods
1a52ab62
MB
661
662=over 4
663
664=item PV
665
130592f5 666This method is the one you usually want. It constructs a
76ef7183
JH
667string using the length and offset information in the struct:
668for ordinary scalars it will return the string that you'd see
669from Perl, even if it contains null characters.
670
9d2bbe64
MB
671=item RV
672
673Same as B::RV::RV, except that it will die() if the PV isn't
674a reference.
675
0b40bd6d
RH
676=item PVX
677
130592f5 678This method is less often useful. It assumes that the string
76ef7183
JH
679stored in the struct is null-terminated, and disregards the
680length information.
681
682It is the appropriate method to use if you need to get the name
130592f5 683of a lexical variable from a padname array. Lexical variable names
76ef7183 684are always stored with a null terminator, and the length field
5c140421
FC
685(CUR) is overloaded for other purposes and can't be relied on here.
686
687=item CUR
688
689This method returns the internal length field, which consists of the number
690of internal bytes, not necessarily the number of logical characters.
691
692=item LEN
693
694This method returns the number of bytes allocated (via malloc) for storing
695the string. This is 0 if the scalar does not "own" the string.
76ef7183 696
1a52ab62
MB
697=back
698
85cf7f2e 699=head2 B::PVMG Methods
1a52ab62
MB
700
701=over 4
702
703=item MAGIC
704
705=item SvSTASH
706
707=back
708
85cf7f2e 709=head2 B::MAGIC Methods
1a52ab62
MB
710
711=over 4
712
713=item MOREMAGIC
714
9d2bbe64
MB
715=item precomp
716
717Only valid on r-magic, returns the string that generated the regexp.
718
1a52ab62
MB
719=item PRIVATE
720
721=item TYPE
722
723=item FLAGS
724
725=item OBJ
726
9d2bbe64
MB
727Will die() if called on r-magic.
728
1a52ab62
MB
729=item PTR
730
9d2bbe64
MB
731=item REGEX
732
733Only valid on r-magic, returns the integer value of the REGEX stored
734in the MAGIC.
735
1a52ab62
MB
736=back
737
47e6c6d9
N
738=head2 B::INVLIST Methods
739
740=over 4
741
742=item prev_index
743
744Returns the cache result of previous invlist_search() (internal usage)
745
746=item is_offset
747
748Returns a boolean value (0 or 1) to know if the invlist is using an offset.
749When false the list begins with the code point U+0000.
750When true the list begins with the following elements.
751
752=item array_len
753
754Returns an integer with the size of the array used to define the invlist.
755
756=item get_invlist_array
757
758This method returns a list of integers representing the array used by the
759invlist.
760Note: this cannot be used while in middle of iterating on an invlist and croaks.
761
762=back
763
85cf7f2e 764=head2 B::PVLV Methods
1a52ab62
MB
765
766=over 4
767
768=item TARGOFF
769
770=item TARGLEN
771
772=item TYPE
773
774=item TARG
775
776=back
777
85cf7f2e 778=head2 B::BM Methods
1a52ab62
MB
779
780=over 4
781
782=item USEFUL
783
784=item PREVIOUS
785
786=item RARE
787
788=item TABLE
789
790=back
791
3f03baed
FC
792=head2 B::REGEXP Methods
793
794=over 4
795
796=item REGEX
797
798=item precomp
799
6190dd99
FC
800=item qr_anoncv
801
1f306347
FC
802=item compflags
803
804The last two were added in Perl 5.22.
805
3f03baed
FC
806=back
807
85cf7f2e 808=head2 B::GV Methods
1a52ab62
MB
809
810=over 4
811
87d7fd28
GS
812=item is_empty
813
814This method returns TRUE if the GP field of the GV is NULL.
815
1a52ab62
MB
816=item NAME
817
002b978b
RH
818=item SAFENAME
819
820This method returns the name of the glob, but if the first
821character of the name is a control character, then it converts
822it to ^X first, so that *^G would return "^G" rather than "\cG".
823
824It's useful if you want to print out the name of a variable.
825If you restrict yourself to globs which exist at compile-time
826then the result ought to be unambiguous, because code like
827C<${"^G"} = 1> is compiled as two ops - a constant string and
828a dereference (rv2gv) - so that the glob is created at runtime.
829
830If you're working with globs at runtime, and need to disambiguate
831*^G from *{"^G"}, then you should use the raw NAME method.
832
1a52ab62
MB
833=item STASH
834
835=item SV
836
837=item IO
838
839=item FORM
840
841=item AV
842
843=item HV
844
845=item EGV
846
847=item CV
848
849=item CVGEN
850
851=item LINE
852
b195d487
GS
853=item FILE
854
1a52ab62
MB
855=item FILEGV
856
857=item GvREFCNT
858
859=item FLAGS
860
8f4983af
FC
861=item GPFLAGS
862
863This last one is present only in perl 5.22.0 and higher.
864
1a52ab62
MB
865=back
866
85cf7f2e 867=head2 B::IO Methods
1a52ab62 868
8b858c71
FC
869B::IO objects derive from IO objects and you will get more information from
870the IO object itself.
44f7f2d5
RU
871
872For example:
873
874 $gvio = B::svref_2object(\*main::stdin)->IO;
875 $IO = $gvio->object_2svref();
876 $fd = $IO->fileno();
877
1a52ab62
MB
878=over 4
879
880=item LINES
881
882=item PAGE
883
884=item PAGE_LEN
885
886=item LINES_LEFT
887
888=item TOP_NAME
889
890=item TOP_GV
891
892=item FMT_NAME
893
894=item FMT_GV
895
896=item BOTTOM_NAME
897
898=item BOTTOM_GV
899
900=item SUBPROCESS
901
902=item IoTYPE
903
44f7f2d5
RU
904A character symbolizing the type of IO Handle.
905
906 - STDIN/OUT
907 I STDIN/OUT/ERR
908 < read-only
909 > write-only
910 a append
911 + read and write
912 s socket
913 | pipe
914 I IMPLICIT
915 # NUMERIC
916 space closed handle
917 \0 closed internal handle
918
1a52ab62
MB
919=item IoFLAGS
920
9d2bbe64
MB
921=item IsSTD
922
44f7f2d5 923Takes one argument ( 'stdin' | 'stdout' | 'stderr' ) and returns true
9d2bbe64 924if the IoIFP of the object is equal to the handle whose name was
8b858c71 925passed as argument; i.e., $io->IsSTD('stderr') is true if
44f7f2d5 926IoIFP($io) == PerlIO_stderr().
9d2bbe64 927
1a52ab62
MB
928=back
929
85cf7f2e 930=head2 B::AV Methods
1a52ab62
MB
931
932=over 4
933
934=item FILL
935
936=item MAX
937
1a52ab62
MB
938=item ARRAY
939
429a5ce7
SM
940=item ARRAYelt
941
942Like C<ARRAY>, but takes an index as an argument to get only one element,
943rather than a list of all of them.
944
1a52ab62
MB
945=back
946
85cf7f2e 947=head2 B::CV Methods
1a52ab62
MB
948
949=over 4
950
951=item STASH
952
953=item START
954
955=item ROOT
956
957=item GV
958
57843af0
GS
959=item FILE
960
1a52ab62
MB
961=item DEPTH
962
963=item PADLIST
964
eb32218e 965Returns a B::PADLIST object.
3a910aa0 966
1a52ab62
MB
967=item OUTSIDE
968
a3985cdc
DM
969=item OUTSIDE_SEQ
970
1a52ab62
MB
971=item XSUB
972
973=item XSUBANY
974
9d2bbe64
MB
975For constant subroutines, returns the constant SV returned by the subroutine.
976
5cfd8ad4
VB
977=item CvFLAGS
978
de3f1649
JT
979=item const_sv
980
486b1e7f
TC
981=item NAME_HEK
982
983Returns the name of a lexical sub, otherwise C<undef>.
984
1a52ab62
MB
985=back
986
85cf7f2e 987=head2 B::HV Methods
1a52ab62
MB
988
989=over 4
990
991=item FILL
992
993=item MAX
994
995=item KEYS
996
997=item RITER
998
999=item NAME
1000
1a52ab62
MB
1001=item ARRAY
1002
1003=back
1004
1005=head2 OP-RELATED CLASSES
1006
2f7c6295
DM
1007C<B::OP>, C<B::UNOP>, C<B::UNOP_AUX>, C<B::BINOP>, C<B::LOGOP>,
1008C<B::LISTOP>, C<B::PMOP>, C<B::SVOP>, C<B::PADOP>, C<B::PVOP>, C<B::LOOP>,
1009C<B::COP>, C<B::METHOP>.
85cf7f2e
MJD
1010
1011These classes correspond in the obvious way to the underlying C
130592f5 1012structures of similar names. The inheritance hierarchy mimics the
85cf7f2e
MJD
1013underlying C "inheritance":
1014
1015 B::OP
1016 |
b46e009d 1017 +----------+---------+--------+-------+---------+
1018 | | | | | |
1019 B::UNOP B::SVOP B::PADOP B::COP B::PVOP B::METHOP
2f7c6295
DM
1020 |
1021 +---+---+---------+
1022 | | |
1023 B::BINOP B::LOGOP B::UNOP_AUX
85cf7f2e
MJD
1024 |
1025 |
1026 B::LISTOP
2f7c6295
DM
1027 |
1028 +---+---+
1029 | |
1030 B::LOOP B::PMOP
85cf7f2e 1031
b84c7839 1032Access methods correspond to the underlying C structure field names,
85cf7f2e
MJD
1033with the leading "class indication" prefix (C<"op_">) removed.
1034
1035=head2 B::OP Methods
1a52ab62 1036
a60ba18b
JC
1037These methods get the values of similarly named fields within the OP
1038data structure. See top of C<op.h> for more info.
1039
1a52ab62
MB
1040=over 4
1041
1042=item next
1043
1044=item sibling
1045
29e61fd9
DM
1046=item parent
1047
1048Returns the OP's parent. If it has no parent, or if your perl wasn't built
1049with C<-DPERL_OP_PARENT>, returns NULL.
1050
c3890f9c
DM
1051Note that the global variable C<$B::OP::does_parent> is undefined on older
1052perls that don't support the C<parent> method, is defined but false on
1053perls that support the method but were built without C<-DPERL_OP_PARENT>,
1054and is true otherwise.
1055
3f872cb9
GS
1056=item name
1057
1058This returns the op name as a string (e.g. "add", "rv2av").
1059
1a52ab62
MB
1060=item ppaddr
1061
dc333d64
GS
1062This returns the function name as a string (e.g. "PL_ppaddr[OP_ADD]",
1063"PL_ppaddr[OP_RV2AV]").
1a52ab62
MB
1064
1065=item desc
1066
4369b173 1067This returns the op description from the global C PL_op_desc array
1a52ab62
MB
1068(e.g. "addition" "array deref").
1069
1070=item targ
1071
1072=item type
1073
a60ba18b
JC
1074=item opt
1075
1a52ab62
MB
1076=item flags
1077
1078=item private
1079
a60ba18b
JC
1080=item spare
1081
1a52ab62
MB
1082=back
1083
18aceeb0 1084=head2 B::UNOP Method
1a52ab62
MB
1085
1086=over 4
1087
1088=item first
1089
1090=back
1091
18aceeb0 1092=head2 B::UNOP_AUX Methods (since 5.22)
2f7c6295
DM
1093
1094=over 4
1095
1096=item aux_list(cv)
1097
1098This returns a list of the elements of the op's aux data structure,
1099or a null list if there is no aux. What will be returned depends on the
1100object's type, but will typically be a collection of C<B::IV>, C<B::GV>,
1101etc. objects. C<cv> is the C<B::CV> object representing the sub that the
1102op is contained within.
1103
1104=item string(cv)
1105
1106This returns a textual representation of the object (likely to b useful
1107for deparsing and debugging), or an empty string if the op type doesn't
1108support this. C<cv> is the C<B::CV> object representing the sub that the
1109op is contained within.
1110
1111=back
1112
18aceeb0 1113=head2 B::BINOP Method
1a52ab62
MB
1114
1115=over 4
1116
1117=item last
1118
1119=back
1120
18aceeb0 1121=head2 B::LOGOP Method
1a52ab62
MB
1122
1123=over 4
1124
1125=item other
1126
1127=back
1128
18aceeb0 1129=head2 B::LISTOP Method
1a52ab62
MB
1130
1131=over 4
1132
1133=item children
1134
1135=back
1136
85cf7f2e 1137=head2 B::PMOP Methods
1a52ab62
MB
1138
1139=over 4
1140
1141=item pmreplroot
1142
1143=item pmreplstart
1144
1a52ab62
MB
1145=item pmflags
1146
1a52ab62
MB
1147=item precomp
1148
651aa52e 1149=item pmoffset
9d2bbe64
MB
1150
1151Only when perl was compiled with ithreads.
1152
e07bb516
DM
1153=item code_list
1154
1155Since perl 5.17.1
1156
429ba3b2
FC
1157=item pmregexp
1158
1159Added in perl 5.22, this method returns the B::REGEXP associated with the
1160op. While PMOPs do not actually have C<pmregexp> fields under threaded
1161builds, this method returns the regexp under threads nonetheless, for
1162convenience.
1163
1a52ab62
MB
1164=back
1165
18aceeb0 1166=head2 B::SVOP Methods
1a52ab62
MB
1167
1168=over 4
1169
1170=item sv
1171
065a1863
GS
1172=item gv
1173
1a52ab62
MB
1174=back
1175
18aceeb0 1176=head2 B::PADOP Method
1a52ab62
MB
1177
1178=over 4
1179
7934575e 1180=item padix
1a52ab62
MB
1181
1182=back
1183
18aceeb0 1184=head2 B::PVOP Method
1a52ab62
MB
1185
1186=over 4
1187
1188=item pv
1189
1190=back
1191
85cf7f2e 1192=head2 B::LOOP Methods
1a52ab62
MB
1193
1194=over 4
1195
1196=item redoop
1197
1198=item nextop
1199
1200=item lastop
1201
1202=back
1203
85cf7f2e 1204=head2 B::COP Methods
1a52ab62 1205
a379c8cb
FC
1206The C<B::COP> class is used for "nextstate" and "dbstate" ops. As of Perl
12075.22, it is also used for "null" ops that started out as COPs.
1208
1a52ab62
MB
1209=over 4
1210
1211=item label
1212
1213=item stash
1214
6e6a1aef
RGS
1215=item stashpv
1216
a60c099b 1217=item stashoff (threaded only)
8df2993f 1218
57843af0 1219=item file
1a52ab62
MB
1220
1221=item cop_seq
1222
1a52ab62
MB
1223=item line
1224
6e6a1aef
RGS
1225=item warnings
1226
1227=item io
1228
d5ec2987
NC
1229=item hints
1230
b47e7f93
RGS
1231=item hints_hash
1232
1a52ab62
MB
1233=back
1234
b46e009d 1235=head2 B::METHOP Methods (Since Perl 5.22)
1236
1237=over 4
1238
1239=item first
1240
1241=item meth_sv
1242
1243=back
1244
9b7476d7 1245=head2 PAD-RELATED CLASSES
3a910aa0 1246
9b7476d7 1247Perl 5.18 introduced a new class, B::PADLIST, returned by B::CV's
3a910aa0
FC
1248C<PADLIST> method.
1249
9b7476d7
FC
1250Perl 5.22 introduced the B::PADNAMELIST and B::PADNAME classes.
1251
3a910aa0
FC
1252=head2 B::PADLIST Methods
1253
1254=over 4
1255
1256=item MAX
1257
1258=item ARRAY
1259
eb32218e
Z
1260A list of pads. The first one is a B::PADNAMELIST containing the names.
1261The rest are currently B::AV objects, but that could
9b7476d7 1262change in future versions.
3a910aa0
FC
1263
1264=item ARRAYelt
1265
1266Like C<ARRAY>, but takes an index as an argument to get only one element,
1267rather than a list of all of them.
1268
9b7476d7
FC
1269=item NAMES
1270
1271This method, introduced in 5.22, returns the B::PADNAMELIST. It is
1272equivalent to C<ARRAYelt> with a 0 argument.
1273
1274=item REFCNT
1275
58480c3b
FC
1276=item id
1277
1278This method, introduced in 5.22, returns an ID shared by clones of the same
1279padlist.
1280
1281=item outid
1282
1283This method, also added in 5.22, returns the ID of the outer padlist.
1284
9b7476d7
FC
1285=back
1286
1287=head2 B::PADNAMELIST Methods
1288
1289=over 4
1290
1291=item MAX
1292
1293=item ARRAY
1294
1295=item ARRAYelt
1296
aa572f37
FC
1297These two methods return the pad names, using B::SPECIAL objects for null
1298pointers and B::PADNAME objects otherwise.
1299
3a910aa0
FC
1300=item REFCNT
1301
1302=back
7f20e9dd 1303
dbac5ffe
FC
1304=head2 B::PADNAME Methods
1305
1306=over 4
1307
1308=item PV
1309
1310=item PVX
1311
1312=item LEN
1313
1314=item REFCNT
1315
1316=item FLAGS
1317
1318For backward-compatibility, if the PADNAMEt_OUTER flag is set, the FLAGS
1319method adds the SVf_FAKE flag, too.
1320
1321=item TYPE
1322
1323A B::HV object representing the stash for a typed lexical.
1324
1325=item SvSTASH
1326
1327A backward-compatibility alias for TYPE.
1328
1329=item OURSTASH
1330
1331A B::HV object representing the stash for 'our' variables.
1332
1333=item PROTOCV
1334
1335The prototype CV for a 'my' sub.
1336
1337=item COP_SEQ_RANGE_LOW
1338
1339=item COP_SEQ_RANGE_HIGH
1340
1341Sequence numbers representing the scope within which a lexical is visible.
1342Meaningless if PADNAMEt_OUTER is set.
1343
1344=item PARENT_PAD_INDEX
1345
1346Only meaningful if PADNAMEt_OUTER is set.
1347
1348=item PARENT_FAKELEX_FLAGS
1349
1350Only meaningful if PADNAMEt_OUTER is set.
1351
1352=back
1353
71324a3b
DM
1354=head2 $B::overlay
1355
1356Although the optree is read-only, there is an overlay facility that allows
1357you to override what values the various B::*OP methods return for a
1358particular op. C<$B::overlay> should be set to reference a two-deep hash:
1359indexed by OP address, then method name. Whenever a an op method is
1360called, the value in the hash is returned if it exists. This facility is
1361used by B::Deparse to "undo" some optimisations. For example:
1362
1363
1364 local $B::overlay = {};
1365 ...
1366 if ($op->name eq "foo") {
1367 $B::overlay->{$$op} = {
1368 name => 'bar',
1369 next => $op->next->next,
1370 };
1371 }
1372 ...
1373 $op->name # returns "bar"
1374 $op->next # returns the next op but one
1375
1376
7f20e9dd
GS
1377=head1 AUTHOR
1378
1379Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
1380
1381=cut