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