File: perlinterp.html

package info (click to toggle)
perl-doc-html 5.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,756 kB
  • ctags: 26,472
  • sloc: xml: 36; makefile: 2
file content (1103 lines) | stat: -rw-r--r-- 67,940 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlinterp - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>perlinterp</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 22.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-internals.html">Internals and C language interface</a> &gt;
      
    
    perlinterp
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>perlinterp</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#ELEMENTS-OF-THE-INTERPRETER">ELEMENTS OF THE INTERPRETER</a><ul><li><a href="#Startup">Startup</a><li><a href="#Parsing">Parsing</a><li><a href="#Optimization">Optimization</a><li><a href="#Running">Running</a><li><a href="#Exception-handing">Exception handing</a><li><a href="#INTERNAL-VARIABLE-TYPES">INTERNAL VARIABLE TYPES</a></ul><li><a href="#OP-TREES">OP TREES</a><li><a href="#STACKS">STACKS</a><ul><li><a href="#Argument-stack">Argument stack</a><li><a href="#Mark-stack">Mark stack</a><li><a href="#Save-stack">Save stack</a></ul><li><a href="#MILLIONS-OF-MACROS">MILLIONS OF MACROS</a><li><a href="#FURTHER-READING">FURTHER READING</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlinterp - An overview of the Perl interpreter</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This document provides an overview of how the Perl interpreter works at
the level of C code, along with pointers to the relevant C source code
files.</p>
<a name="ELEMENTS-OF-THE-INTERPRETER"></a><h1>ELEMENTS OF THE INTERPRETER</h1>
<p>The work of the interpreter has two main stages: compiling the code
into the internal representation, or bytecode, and then executing it.
<a href="perlguts.html#Compiled-code">Compiled code in perlguts</a> explains exactly how the compilation stage
happens.</p>
<p>Here is a short breakdown of perl's operation:</p>
<a name="Startup"></a><h2>Startup</h2>
<p>The action begins in <i>perlmain.c</i>. (or <i>miniperlmain.c</i> for miniperl)
This is very high-level code, enough to fit on a single screen, and it
resembles the code found in <a href="perlembed.html">perlembed</a>; most of the real action takes
place in <i>perl.c</i></p>
<p><i>perlmain.c</i> is generated by <code class="inline"><span class="w">ExtUtils::Miniperl</span></code>
 from
<i>miniperlmain.c</i> at make time, so you should make perl to follow this
along.</p>
<p>First, <i>perlmain.c</i> allocates some memory and constructs a Perl
interpreter, along these lines:</p>
<pre class="verbatim"><ol><li>    1 PERL_SYS_INIT3(&amp;argc,&amp;argv,&amp;env);</li><li>    2</li><li>    3 if (!PL_do_undump) {</li><li>    4     my_perl = perl_alloc();</li><li>    5     if (!my_perl)</li><li>    6         exit(1);</li><li>    7     perl_construct(my_perl);</li><li>    8     PL_perl_destruct_level = 0;</li><li>    9 }</li></ol></pre><p>Line 1 is a macro, and its definition is dependent on your operating
system. Line 3 references <code class="inline"><span class="w">PL_do_undump</span></code>
, a global variable - all
global variables in Perl start with <code class="inline"><span class="w">PL_</span></code>
. This tells you whether the
current running program was created with the <code class="inline">-u</code>
 flag to perl and
then <i>undump</i>, which means it's going to be false in any sane context.</p>
<p>Line 4 calls a function in <i>perl.c</i> to allocate memory for a Perl
interpreter. It's quite a simple function, and the guts of it looks
like this:</p>
<pre class="verbatim"><ol><li> <span class="w">my_perl</span> = <span class="s">(</span><span class="w">PerlInterpreter</span>*<span class="s">)</span><span class="i">PerlMem_malloc</span><span class="s">(</span><span class="i">sizeof</span><span class="s">(</span><span class="w">PerlInterpreter</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Here you see an example of Perl's system abstraction, which we'll see
later: <code class="inline"><span class="w">PerlMem_malloc</span></code>
 is either your system's <code class="inline"><span class="w">malloc</span></code>
, or Perl's
own <code class="inline"><span class="w">malloc</span></code>
 as defined in <i>malloc.c</i> if you selected that option at
configure time.</p>
<p>Next, in line 7, we construct the interpreter using perl_construct,
also in <i>perl.c</i>; this sets up all the special variables that Perl
needs, the stacks, and so on.</p>
<p>Now we pass Perl the command line options, and tell it to go:</p>
<pre class="verbatim"><ol><li> <span class="w">exitstatus</span> = <span class="i">perl_parse</span><span class="s">(</span><span class="w">my_perl</span><span class="cm">,</span> <span class="w">xs_init</span><span class="cm">,</span> <span class="w">argc</span><span class="cm">,</span> <span class="w">argv</span><span class="cm">,</span> <span class="s">(</span><span class="w">char</span> **<span class="s">)</span><span class="w">NULL</span><span class="s">)</span><span class="sc">;</span></li><li> if <span class="s">(</span>!<span class="w">exitstatus</span><span class="s">)</span></li><li>     <span class="i">perl_run</span><span class="s">(</span><span class="w">my_perl</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="w">exitstatus</span> = <span class="i">perl_destruct</span><span class="s">(</span><span class="w">my_perl</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="i">perl_free</span><span class="s">(</span><span class="w">my_perl</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p><code class="inline"><span class="w">perl_parse</span></code>
 is actually a wrapper around <code class="inline"><span class="w">S_parse_body</span></code>
, as defined
in <i>perl.c</i>, which processes the command line options, sets up any
statically linked XS modules, opens the program and calls <code class="inline"><span class="w">yyparse</span></code>
 to
parse it.</p>
<a name="Parsing"></a><h2>Parsing</h2>
<p>The aim of this stage is to take the Perl source, and turn it into an
op tree. We'll see what one of those looks like later. Strictly
speaking, there's three things going on here.</p>
<p><code class="inline"><span class="w">yyparse</span></code>
, the parser, lives in <i>perly.c</i>, although you're better off
reading the original YACC input in <i>perly.y</i>. (Yes, Virginia, there
<b>is</b> a YACC grammar for Perl!) The job of the parser is to take your
code and "understand" it, splitting it into sentences, deciding which
operands go with which operators and so on.</p>
<p>The parser is nobly assisted by the lexer, which chunks up your input
into tokens, and decides what type of thing each token is: a variable
name, an operator, a bareword, a subroutine, a core function, and so
on. The main point of entry to the lexer is <code class="inline"><span class="w">yylex</span></code>
, and that and its
associated routines can be found in <i>toke.c</i>. Perl isn't much like
other computer languages; it's highly context sensitive at times, it
can be tricky to work out what sort of token something is, or where a
token ends. As such, there's a lot of interplay between the tokeniser
and the parser, which can get pretty frightening if you're not used to
it.</p>
<p>As the parser understands a Perl program, it builds up a tree of
operations for the interpreter to perform during execution. The
routines which construct and link together the various operations are
to be found in <i>op.c</i>, and will be examined later.</p>
<a name="Optimization"></a><h2>Optimization</h2>
<p>Now the parsing stage is complete, and the finished tree represents the
operations that the Perl interpreter needs to perform to execute our
program. Next, Perl does a dry run over the tree looking for
optimisations: constant expressions such as <code class="inline"><span class="n">3</span> + <span class="n">4</span></code>
 will be computed
now, and the optimizer will also see if any multiple operations can be
replaced with a single one. For instance, to fetch the variable
<code class="inline"><span class="i">$foo</span></code>
, instead of grabbing the glob <code class="inline"><span class="i">*foo</span></code>
 and looking at the scalar
component, the optimizer fiddles the op tree to use a function which
directly looks up the scalar in question. The main optimizer is <code class="inline"><span class="w">peep</span></code>

in <i>op.c</i>, and many ops have their own optimizing functions.</p>
<a name="Running"></a><h2>Running</h2>
<p>Now we're finally ready to go: we have compiled Perl byte code, and all
that's left to do is run it. The actual execution is done by the
<code class="inline"><span class="w">runops_standard</span></code>
 function in <i>run.c</i>; more specifically, it's done
by these three innocent looking lines:</p>
<pre class="verbatim"><ol><li>    while <span class="s">(</span><span class="s">(</span><span class="w">PL_op</span> = <span class="w">PL_op</span><span class="w">-&gt;op_ppaddr</span><span class="s">(</span><span class="w">aTHX</span><span class="s">)</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">PERL_ASYNC_CHECK</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>You may be more comfortable with the Perl version of that:</p>
<pre class="verbatim"><ol><li>    <span class="i">PERL_ASYNC_CHECK</span><span class="s">(</span><span class="s">)</span> while <span class="i">$Perl::op</span> = <span class="i">&amp;</span>{<span class="i">$Perl::op</span>-&gt;{<span class="w">function</span>}}<span class="sc">;</span></li></ol></pre><p>Well, maybe not. Anyway, each op contains a function pointer, which
stipulates the function which will actually carry out the operation.
This function will return the next op in the sequence - this allows for
things like <code class="inline">if</code>
 which choose the next op dynamically at run time. The
<code class="inline"><span class="w">PERL_ASYNC_CHECK</span></code>
 makes sure that things like signals interrupt
execution if required.</p>
<p>The actual functions called are known as PP code, and they're spread
between four files: <i>pp_hot.c</i> contains the "hot" code, which is most
often used and highly optimized, <i>pp_sys.c</i> contains all the
system-specific functions, <i>pp_ctl.c</i> contains the functions which
implement control structures (<code class="inline">if</code>
, <code class="inline">while</code>
 and the like) and <i>pp.c</i>
contains everything else. These are, if you like, the C code for Perl's
built-in functions and operators.</p>
<p>Note that each <code class="inline"><span class="w">pp_</span></code>
 function is expected to return a pointer to the
next op. Calls to perl subs (and eval blocks) are handled within the
same runops loop, and do not consume extra space on the C stack. For
example, <code class="inline"><span class="w">pp_entersub</span></code>
 and <code class="inline"><span class="w">pp_entertry</span></code>
 just push a <code class="inline"><span class="w">CxSUB</span></code>
 or
<code class="inline"><span class="w">CxEVAL</span></code>
 block struct onto the context stack which contain the address
of the op following the sub call or eval. They then return the first op
of that sub or eval block, and so execution continues of that sub or
block. Later, a <code class="inline"><span class="w">pp_leavesub</span></code>
 or <code class="inline"><span class="w">pp_leavetry</span></code>
 op pops the <code class="inline"><span class="w">CxSUB</span></code>

or <code class="inline"><span class="w">CxEVAL</span></code>
, retrieves the return op from it, and returns it.</p>
<a name="Exception-handing"></a><h2>Exception handing</h2>
<p>Perl's exception handing (i.e. <code class="inline"><a class="l_k" href="functions/die.html">die</a></code> etc.) is built on top of the
low-level <code class="inline"><span class="i">setjmp</span><span class="s">(</span><span class="s">)</span></code>
/<code class="inline"><span class="i">longjmp</span><span class="s">(</span><span class="s">)</span></code>
 C-library functions. These basically
provide a way to capture the current PC and SP registers and later
restore them; i.e. a <code class="inline"><span class="i">longjmp</span><span class="s">(</span><span class="s">)</span></code>
 continues at the point in code where
a previous <code class="inline"><span class="i">setjmp</span><span class="s">(</span><span class="s">)</span></code>
 was done, with anything further up on the C
stack being lost. This is why code should always save values using
<code class="inline"><span class="w">SAVE_FOO</span></code>
 rather than in auto variables.</p>
<p>The perl core wraps <code class="inline"><span class="i">setjmp</span><span class="s">(</span><span class="s">)</span></code>
 etc in the macros <code class="inline"><span class="w">JMPENV_PUSH</span></code>
 and
<code class="inline"><span class="w">JMPENV_JUMP</span></code>
. The basic rule of perl exceptions is that <code class="inline"><a class="l_k" href="functions/exit.html">exit</a></code>, and
<code class="inline"><a class="l_k" href="functions/die.html">die</a></code> (in the absence of <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code>) perform a <code class="inline"><span class="i">JMPENV_JUMP</span><span class="s">(</span><span class="n">2</span><span class="s">)</span></code>
, while
<code class="inline"><a class="l_k" href="functions/die.html">die</a></code> within <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code> does a <code class="inline"><span class="i">JMPENV_JUMP</span><span class="s">(</span><span class="n">3</span><span class="s">)</span></code>
.</p>
<p>At entry points to perl, such as <code class="inline"><span class="i">perl_parse</span><span class="s">(</span><span class="s">)</span></code>
, <code class="inline"><span class="i">perl_run</span><span class="s">(</span><span class="s">)</span></code>
 and
<code class="inline"><span class="i">call_sv</span><span class="s">(</span><span class="w">cv</span><span class="cm">,</span> <span class="w">G_EVAL</span><span class="s">)</span></code>
 each does a <code class="inline"><span class="w">JMPENV_PUSH</span></code>
, then enter a runops
loop or whatever, and handle possible exception returns. For a 2
return, final cleanup is performed, such as popping stacks and calling
<code class="inline">CHECK</code>
 or <code class="inline">END</code>
 blocks. Amongst other things, this is how scope
cleanup still occurs during an <code class="inline"><a class="l_k" href="functions/exit.html">exit</a></code>.</p>
<p>If a <code class="inline"><a class="l_k" href="functions/die.html">die</a></code> can find a <code class="inline"><span class="w">CxEVAL</span></code>
 block on the context stack, then the
stack is popped to that level and the return op in that block is
assigned to <code class="inline"><span class="w">PL_restartop</span></code>
; then a <code class="inline"><span class="i">JMPENV_JUMP</span><span class="s">(</span><span class="n">3</span><span class="s">)</span></code>
 is performed.
This normally passes control back to the guard. In the case of
<code class="inline"><span class="w">perl_run</span></code>
 and <code class="inline"><span class="w">call_sv</span></code>
, a non-null <code class="inline"><span class="w">PL_restartop</span></code>
 triggers
re-entry to the runops loop. The is the normal way that <code class="inline"><a class="l_k" href="functions/die.html">die</a></code> or
<code class="inline"><span class="w">croak</span></code>
 is handled within an <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code>.</p>
<p>Sometimes ops are executed within an inner runops loop, such as tie,
sort or overload code. In this case, something like</p>
<pre class="verbatim"><ol><li><a name="FETCH"></a>    sub <span class="m">FETCH</span> <span class="s">{</span> <a class="l_k" href="functions/eval.html">eval</a> <span class="s">{</span> <a class="l_k" href="functions/die.html">die</a> <span class="s">}</span> <span class="s">}</span></li></ol></pre><p>would cause a longjmp right back to the guard in <code class="inline"><span class="w">perl_run</span></code>
, popping
both runops loops, which is clearly incorrect. One way to avoid this is
for the tie code to do a <code class="inline"><span class="w">JMPENV_PUSH</span></code>
 before executing <code class="inline"><span class="w">FETCH</span></code>
 in
the inner runops loop, but for efficiency reasons, perl in fact just
sets a flag, using <code class="inline"><span class="i">CATCH_SET</span><span class="s">(</span><span class="w">TRUE</span><span class="s">)</span></code>
. The <code class="inline"><span class="w">pp_require</span></code>
,
<code class="inline"><span class="w">pp_entereval</span></code>
 and <code class="inline"><span class="w">pp_entertry</span></code>
 ops check this flag, and if true,
they call <code class="inline"><span class="w">docatch</span></code>
, which does a <code class="inline"><span class="w">JMPENV_PUSH</span></code>
 and starts a new
runops level to execute the code, rather than doing it on the current
loop.</p>
<p>As a further optimisation, on exit from the eval block in the <code class="inline"><span class="w">FETCH</span></code>
,
execution of the code following the block is still carried on in the
inner loop. When an exception is raised, <code class="inline"><span class="w">docatch</span></code>
 compares the
<code class="inline"><span class="w">JMPENV</span></code>
 level of the <code class="inline"><span class="w">CxEVAL</span></code>
 with <code class="inline"><span class="w">PL_top_env</span></code>
 and if they differ,
just re-throws the exception. In this way any inner loops get popped.</p>
<p>Here's an example.</p>
<pre class="verbatim"><ol><li>    1: eval { tie @a, 'A' };</li><li>    2: sub A::TIEARRAY {</li><li>    3:     eval { die };</li><li>    4:     die;</li><li>    5: }</li></ol></pre><p>To run this code, <code class="inline"><span class="w">perl_run</span></code>
 is called, which does a <code class="inline"><span class="w">JMPENV_PUSH</span></code>

then enters a runops loop. This loop executes the eval and tie ops on
line 1, with the eval pushing a <code class="inline"><span class="w">CxEVAL</span></code>
 onto the context stack.</p>
<p>The <code class="inline"><span class="w">pp_tie</span></code>
 does a <code class="inline"><span class="i">CATCH_SET</span><span class="s">(</span><span class="w">TRUE</span><span class="s">)</span></code>
, then starts a second runops
loop to execute the body of <code class="inline"><span class="w">TIEARRAY</span></code>
. When it executes the entertry
op on line 3, <code class="inline"><span class="w">CATCH_GET</span></code>
 is true, so <code class="inline"><span class="w">pp_entertry</span></code>
 calls <code class="inline"><span class="w">docatch</span></code>

which does a <code class="inline"><span class="w">JMPENV_PUSH</span></code>
 and starts a third runops loop, which then
executes the die op. At this point the C call stack looks like this:</p>
<pre class="verbatim"><ol><li>    <span class="w">Perl_pp_die</span></li><li>    <span class="w">Perl_runops</span>      <span class="c"># third loop</span></li><li>    <span class="w">S_docatch_body</span></li><li>    <span class="w">S_docatch</span></li><li>    <span class="w">Perl_pp_entertry</span></li><li>    <span class="w">Perl_runops</span>      <span class="c"># second loop</span></li><li>    <span class="w">S_call_body</span></li><li>    <span class="w">Perl_call_sv</span></li><li>    <span class="w">Perl_pp_tie</span></li><li>    <span class="w">Perl_runops</span>      <span class="c"># first loop</span></li><li>    <span class="w">S_run_body</span></li><li>    <span class="w">perl_run</span></li><li>    <span class="w">main</span></li></ol></pre><p>and the context and data stacks, as shown by <code class="inline">-<span class="w">Dstv</span></code>
, look like:</p>
<pre class="verbatim"><ol><li>    STACK 0: MAIN</li><li>      CX 0: BLOCK  =&gt;</li><li>      CX 1: EVAL   =&gt; AV()  PV("A"\0)</li><li>      retop=leave</li><li>    STACK 1: MAGIC</li><li>      CX 0: SUB    =&gt;</li><li>      retop=(null)</li><li>      CX 1: EVAL   =&gt; *</li><li>    retop=nextstate</li></ol></pre><p>The die pops the first <code class="inline"><span class="w">CxEVAL</span></code>
 off the context stack, sets
<code class="inline"><span class="w">PL_restartop</span></code>
 from it, does a <code class="inline"><span class="i">JMPENV_JUMP</span><span class="s">(</span><span class="n">3</span><span class="s">)</span></code>
, and control returns
to the top <code class="inline"><span class="w">docatch</span></code>
. This then starts another third-level runops
level, which executes the nextstate, pushmark and die ops on line 4. At
the point that the second <code class="inline"><span class="w">pp_die</span></code>
 is called, the C call stack looks
exactly like that above, even though we are no longer within an inner
eval; this is because of the optimization mentioned earlier. However,
the context stack now looks like this, ie with the top CxEVAL popped:</p>
<pre class="verbatim"><ol><li>    STACK 0: MAIN</li><li>      CX 0: BLOCK  =&gt;</li><li>      CX 1: EVAL   =&gt; AV()  PV("A"\0)</li><li>      retop=leave</li><li>    STACK 1: MAGIC</li><li>      CX 0: SUB    =&gt;</li><li>      retop=(null)</li></ol></pre><p>The die on line 4 pops the context stack back down to the CxEVAL,
leaving it as:</p>
<pre class="verbatim"><ol><li>    STACK 0: MAIN</li><li>      CX 0: BLOCK  =&gt;</li></ol></pre><p>As usual, <code class="inline"><span class="w">PL_restartop</span></code>
 is extracted from the <code class="inline"><span class="w">CxEVAL</span></code>
, and a
<code class="inline"><span class="i">JMPENV_JUMP</span><span class="s">(</span><span class="n">3</span><span class="s">)</span></code>
 done, which pops the C stack back to the docatch:</p>
<pre class="verbatim"><ol><li>    <span class="w">S_docatch</span></li><li>    <span class="w">Perl_pp_entertry</span></li><li>    <span class="w">Perl_runops</span>      <span class="c"># second loop</span></li><li>    <span class="w">S_call_body</span></li><li>    <span class="w">Perl_call_sv</span></li><li>    <span class="w">Perl_pp_tie</span></li><li>    <span class="w">Perl_runops</span>      <span class="c"># first loop</span></li><li>    <span class="w">S_run_body</span></li><li>    <span class="w">perl_run</span></li><li>    <span class="w">main</span></li></ol></pre><p>In  this case, because the <code class="inline"><span class="w">JMPENV</span></code>
 level recorded in the <code class="inline"><span class="w">CxEVAL</span></code>

differs from the current one, <code class="inline"><span class="w">docatch</span></code>
 just does a <code class="inline"><span class="i">JMPENV_JUMP</span><span class="s">(</span><span class="n">3</span><span class="s">)</span></code>

and the C stack unwinds to:</p>
<pre class="verbatim"><ol><li>    <span class="w">perl_run</span></li><li>    <span class="w">main</span></li></ol></pre><p>Because <code class="inline"><span class="w">PL_restartop</span></code>
 is non-null, <code class="inline"><span class="w">run_body</span></code>
 starts a new runops
loop and execution continues.</p>
<a name="INTERNAL-VARIABLE-TYPES"></a><h2>INTERNAL VARIABLE TYPES</h2>
<p>You should by now have had a look at <a href="perlguts.html">perlguts</a>, which tells you about
Perl's internal variable types: SVs, HVs, AVs and the rest. If not, do
that now.</p>
<p>These variables are used not only to represent Perl-space variables,
but also any constants in the code, as well as some structures
completely internal to Perl. The symbol table, for instance, is an
ordinary Perl hash. Your code is represented by an SV as it's read into
the parser; any program files you call are opened via ordinary Perl
filehandles, and so on.</p>
<p>The core <a href="Devel/Peek.html">Devel::Peek</a> module lets us examine SVs from a
Perl program. Let's see, for instance, how Perl treats the constant
<code class="inline"><span class="q">&quot;hello&quot;</span></code>
.</p>
<pre class="verbatim"><ol><li>      % perl -MDevel::Peek -e 'Dump("hello")'</li><li>    1 SV = PV(0xa041450) at 0xa04ecbc</li><li>    2   REFCNT = 1</li><li>    3   FLAGS = (POK,READONLY,pPOK)</li><li>    4   PV = 0xa0484e0 "hello"\0</li><li>    5   CUR = 5</li><li>    6   LEN = 6</li></ol></pre><p>Reading <code class="inline"><span class="w">Devel::Peek</span></code>
 output takes a bit of practise, so let's go
through it line by line.</p>
<p>Line 1 tells us we're looking at an SV which lives at <code class="inline"><span class="n">0xa04ecbc</span></code>
 in
memory. SVs themselves are very simple structures, but they contain a
pointer to a more complex structure. In this case, it's a PV, a
structure which holds a string value, at location <code class="inline"><span class="n">0xa041450</span></code>
. Line 2
is the reference count; there are no other references to this data, so
it's 1.</p>
<p>Line 3 are the flags for this SV - it's OK to use it as a PV, it's a
read-only SV (because it's a constant) and the data is a PV internally.
Next we've got the contents of the string, starting at location
<code class="inline"><span class="n">0xa0484e0</span></code>
.</p>
<p>Line 5 gives us the current length of the string - note that this does
<b>not</b> include the null terminator. Line 6 is not the length of the
string, but the length of the currently allocated buffer; as the string
grows, Perl automatically extends the available storage via a routine
called <code class="inline"><span class="w">SvGROW</span></code>
.</p>
<p>You can get at any of these quantities from C very easily; just add
<code class="inline"><span class="w">Sv</span></code>
 to the name of the field shown in the snippet, and you've got a
macro which will return the value: <code class="inline"><span class="i">SvCUR</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span></code>
 returns the current
length of the string, <code class="inline"><span class="i">SvREFCOUNT</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span></code>
 returns the reference count,
<code class="inline"><span class="i">SvPV</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">len</span><span class="s">)</span></code>
 returns the string itself with its length, and so on.
More macros to manipulate these properties can be found in <a href="perlguts.html">perlguts</a>.</p>
<p>Let's take an example of manipulating a PV, from <code class="inline"><span class="w">sv_catpvn</span></code>
, in
<i>sv.c</i></p>
<pre class="verbatim"><ol><li>     <span class="n">1</span>  <span class="w">void</span></li><li>     <span class="n">2</span>  <span class="i">Perl_sv_catpvn</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">SV</span> *<span class="w">sv</span><span class="cm">,</span> <span class="w">const</span> <span class="w">char</span> *<span class="w">ptr</span><span class="cm">,</span> <span class="w">STRLEN</span> <span class="w">len</span><span class="s">)</span></li><li>     <span class="n">3</span>  <span class="s">{</span></li><li>     <span class="n">4</span>      <span class="w">STRLEN</span> <span class="w">tlen</span><span class="sc">;</span></li><li>     <span class="n">5</span>      <span class="w">char</span> *<span class="w">junk</span><span class="sc">;</span></li><li></li><li>     <span class="n">6</span>      <span class="w">junk</span> = <span class="i">SvPV_force</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">tlen</span><span class="s">)</span><span class="sc">;</span></li><li>     <span class="n">7</span>      <span class="i">SvGROW</span><span class="s">(</span><span class="w">sv</span><span class="cm">,</span> <span class="w">tlen</span> + <span class="w">len</span> + <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>     <span class="n">8</span>      <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="w">ptr</span> == <span class="w">junk</span><span class="s">)</span></li><li>     <span class="n">9</span>          <span class="w">ptr</span> = <span class="i">SvPVX</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="n">10</span>      <span class="i">Move</span><span class="s">(</span><span class="w">ptr</span><span class="cm">,</span><span class="i">SvPVX</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span>+<span class="w">tlen</span><span class="cm">,</span><span class="w">len</span><span class="cm">,</span><span class="w">char</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="n">11</span>      <span class="i">SvCUR</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span> += <span class="w">len</span><span class="sc">;</span></li><li>    <span class="n">12</span>      *<span class="i">SvEND</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span> = <span class="q">&#39;\0&#39;</span><span class="sc">;</span></li><li>    <span class="n">13</span>      <span class="s">(</span><span class="w">void</span><span class="s">)</span><span class="i">SvPOK_only_UTF8</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span><span class="sc">;</span>          <span class="q">/* validate pointer */</span></li><li>    <span class="n">14</span>      <span class="i">SvTAINT</span><span class="s">(</span><span class="w">sv</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="n">15</span>  <span class="s">}</span></li></ol></pre><p>This is a function which adds a string, <code class="inline"><span class="w">ptr</span></code>
, of length <code class="inline"><span class="w">len</span></code>
 onto
the end of the PV stored in <code class="inline"><span class="w">sv</span></code>
. The first thing we do in line 6 is
make sure that the SV <b>has</b> a valid PV, by calling the <code class="inline"><span class="w">SvPV_force</span></code>

macro to force a PV. As a side effect, <code class="inline"><span class="w">tlen</span></code>
 gets set to the current
value of the PV, and the PV itself is returned to <code class="inline"><span class="w">junk</span></code>
.</p>
<p>In line 7, we make sure that the SV will have enough room to
accommodate the old string, the new string and the null terminator. If
<code class="inline"><span class="w">LEN</span></code>
 isn't big enough, <code class="inline"><span class="w">SvGROW</span></code>
 will reallocate space for us.</p>
<p>Now, if <code class="inline"><span class="w">junk</span></code>
 is the same as the string we're trying to add, we can
grab the string directly from the SV; <code class="inline"><span class="w">SvPVX</span></code>
 is the address of the PV
in the SV.</p>
<p>Line 10 does the actual catenation: the <code class="inline"><span class="w">Move</span></code>
 macro moves a chunk of
memory around: we move the string <code class="inline"><span class="w">ptr</span></code>
 to the end of the PV - that's
the start of the PV plus its current length. We're moving <code class="inline"><span class="w">len</span></code>
 bytes
of type <code class="inline"><span class="w">char</span></code>
. After doing so, we need to tell Perl we've extended
the string, by altering <code class="inline"><span class="w">CUR</span></code>
 to reflect the new length. <code class="inline"><span class="w">SvEND</span></code>
 is a
macro which gives us the end of the string, so that needs to be a
<code class="inline"><span class="q">&quot;\0&quot;</span></code>
.</p>
<p>Line 13 manipulates the flags; since we've changed the PV, any IV or NV
values will no longer be valid: if we have <code class="inline"><span class="i">$a</span>=<span class="n">10</span><span class="sc">;</span> <span class="i">$a</span>.=<span class="q">&quot;6&quot;</span><span class="sc">;</span></code>
 we don't
want to use the old IV of 10. <code class="inline"><span class="w">SvPOK_only_utf8</span></code>
 is a special
UTF-8-aware version of <code class="inline"><span class="w">SvPOK_only</span></code>
, a macro which turns off the IOK
and NOK flags and turns on POK. The final <code class="inline"><span class="w">SvTAINT</span></code>
 is a macro which
launders tainted data if taint mode is turned on.</p>
<p>AVs and HVs are more complicated, but SVs are by far the most common
variable type being thrown around. Having seen something of how we
manipulate these, let's go on and look at how the op tree is
constructed.</p>
<a name="OP-TREES"></a><h1>OP TREES</h1>
<p>First, what is the op tree, anyway? The op tree is the parsed
representation of your program, as we saw in our section on parsing,
and it's the sequence of operations that Perl goes through to execute
your program, as we saw in <a href="#Running">Running</a>.</p>
<p>An op is a fundamental operation that Perl can perform: all the
built-in functions and operators are ops, and there are a series of ops
which deal with concepts the interpreter needs internally - entering
and leaving a block, ending a statement, fetching a variable, and so
on.</p>
<p>The op tree is connected in two ways: you can imagine that there are
two "routes" through it, two orders in which you can traverse the tree.
First, parse order reflects how the parser understood the code, and
secondly, execution order tells perl what order to perform the
operations in.</p>
<p>The easiest way to examine the op tree is to stop Perl after it has
finished parsing, and get it to dump out the tree. This is exactly what
the compiler backends <a href="B/Terse.html">B::Terse</a>, <a href="B/Concise.html">B::Concise</a>
and <a href="B/Debug.html">B::Debug</a> do.</p>
<p>Let's have a look at how Perl sees <code class="inline"><span class="i">$a</span> = <span class="i">$b</span> + <span class="i">$c</span></code>
:</p>
<pre class="verbatim"><ol><li>     % perl -MO=Terse -e '$a=$b+$c'</li><li>     1  LISTOP (0x8179888) leave</li><li>     2      OP (0x81798b0) enter</li><li>     3      COP (0x8179850) nextstate</li><li>     4      BINOP (0x8179828) sassign</li><li>     5          BINOP (0x8179800) add [1]</li><li>     6              UNOP (0x81796e0) null [15]</li><li>     7                  SVOP (0x80fafe0) gvsv  GV (0x80fa4cc) *b</li><li>     8              UNOP (0x81797e0) null [15]</li><li>     9                  SVOP (0x8179700) gvsv  GV (0x80efeb0) *c</li><li>    10          UNOP (0x816b4f0) null [15]</li><li>    11              SVOP (0x816dcf0) gvsv  GV (0x80fa460) *a</li></ol></pre><p>Let's start in the middle, at line 4. This is a BINOP, a binary
operator, which is at location <code class="inline"><span class="n">0x8179828</span></code>
. The specific operator in
question is <code class="inline"><span class="w">sassign</span></code>
 - scalar assignment - and you can find the code
which implements it in the function <code class="inline"><span class="w">pp_sassign</span></code>
 in <i>pp_hot.c</i>. As a
binary operator, it has two children: the add operator, providing the
result of <code class="inline"><span class="i">$b</span>+<span class="i">$c</span></code>
, is uppermost on line 5, and the left hand side is
on line 10.</p>
<p>Line 10 is the null op: this does exactly nothing. What is that doing
there? If you see the null op, it's a sign that something has been
optimized away after parsing. As we mentioned in <a href="#Optimization">Optimization</a>, the
optimization stage sometimes converts two operations into one, for
example when fetching a scalar variable. When this happens, instead of
rewriting the op tree and cleaning up the dangling pointers, it's
easier just to replace the redundant operation with the null op.
Originally, the tree would have looked like this:</p>
<pre class="verbatim"><ol><li>    10          SVOP (0x816b4f0) rv2sv [15]</li><li>    11              SVOP (0x816dcf0) gv  GV (0x80fa460) *a</li></ol></pre><p>That is, fetch the <code class="inline"><span class="w">a</span></code>
 entry from the main symbol table, and then look
at the scalar component of it: <code class="inline"><span class="w">gvsv</span></code>
 (<code class="inline"><span class="w">pp_gvsv</span></code>
 into <i>pp_hot.c</i>)
happens to do both these things.</p>
<p>The right hand side, starting at line 5 is similar to what we've just
seen: we have the <code class="inline"><span class="w">add</span></code>
 op (<code class="inline"><span class="w">pp_add</span></code>
 also in <i>pp_hot.c</i>) add
together two <code class="inline"><span class="w">gvsv</span></code>
s.</p>
<p>Now, what's this about?</p>
<pre class="verbatim"><ol><li>     1  LISTOP (0x8179888) leave</li><li>     2      OP (0x81798b0) enter</li><li>     3      COP (0x8179850) nextstate</li></ol></pre><p><code class="inline"><span class="w">enter</span></code>
 and <code class="inline"><span class="w">leave</span></code>
 are scoping ops, and their job is to perform any
housekeeping every time you enter and leave a block: lexical variables
are tidied up, unreferenced variables are destroyed, and so on. Every
program will have those first three lines: <code class="inline"><span class="w">leave</span></code>
 is a list, and its
children are all the statements in the block. Statements are delimited
by <code class="inline"><span class="w">nextstate</span></code>
, so a block is a collection of <code class="inline"><span class="w">nextstate</span></code>
 ops, with
the ops to be performed for each statement being the children of
<code class="inline"><span class="w">nextstate</span></code>
. <code class="inline"><span class="w">enter</span></code>
 is a single op which functions as a marker.</p>
<p>That's how Perl parsed the program, from top to bottom:</p>
<pre class="verbatim"><ol><li>                        Program</li><li>                           |</li><li>                       Statement</li><li>                           |</li><li>                           =</li><li>                          / \</li><li>                         /   \</li><li>                        $a   +</li><li>                            / \</li><li>                          $b   $c</li></ol></pre><p>However, it's impossible to <b>perform</b> the operations in this order:
you have to find the values of <code class="inline"><span class="i">$b</span></code>
 and <code class="inline"><span class="i">$c</span></code>
 before you add them
together, for instance. So, the other thread that runs through the op
tree is the execution order: each op has a field <code class="inline"><span class="w">op_next</span></code>
 which
points to the next op to be run, so following these pointers tells us
how perl executes the code. We can traverse the tree in this order
using the <code class="inline"><a class="l_k" href="functions/exec.html">exec</a></code> option to <code class="inline"><span class="w">B::Terse</span></code>
:</p>
<pre class="verbatim"><ol><li>     % perl -MO=Terse,exec -e '$a=$b+$c'</li><li>     1  OP (0x8179928) enter</li><li>     2  COP (0x81798c8) nextstate</li><li>     3  SVOP (0x81796c8) gvsv  GV (0x80fa4d4) *b</li><li>     4  SVOP (0x8179798) gvsv  GV (0x80efeb0) *c</li><li>     5  BINOP (0x8179878) add [1]</li><li>     6  SVOP (0x816dd38) gvsv  GV (0x80fa468) *a</li><li>     7  BINOP (0x81798a0) sassign</li><li>     8  LISTOP (0x8179900) leave</li></ol></pre><p>This probably makes more sense for a human: enter a block, start a
statement. Get the values of <code class="inline"><span class="i">$b</span></code>
 and <code class="inline"><span class="i">$c</span></code>
, and add them together.
Find <code class="inline"><span class="i">$a</span></code>
, and assign one to the other. Then leave.</p>
<p>The way Perl builds up these op trees in the parsing process can be
unravelled by examining <i>perly.y</i>, the YACC grammar. Let's take the
piece we need to construct the tree for <code class="inline"><span class="i">$a</span> = <span class="i">$b</span> + <span class="i">$c</span></code>
</p>
<pre class="verbatim"><ol><li>    1 term    :   term ASSIGNOP term</li><li>    2                { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }</li><li>    3         |   term ADDOP term</li><li>    4                { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }</li></ol></pre><p>If you're not used to reading BNF grammars, this is how it works:
You're fed certain things by the tokeniser, which generally end up in
upper case. Here, <code class="inline"><span class="w">ADDOP</span></code>
, is provided when the tokeniser sees <code class="inline">+</code>
 in
your code. <code class="inline"><span class="w">ASSIGNOP</span></code>
 is provided when <code class="inline">=</code>
 is used for assigning.
These are "terminal symbols", because you can't get any simpler than
them.</p>
<p>The grammar, lines one and three of the snippet above, tells you how to
build up more complex forms. These complex forms, "non-terminal
symbols" are generally placed in lower case. <code class="inline"><span class="w">term</span></code>
 here is a
non-terminal symbol, representing a single expression.</p>
<p>The grammar gives you the following rule: you can make the thing on the
left of the colon if you see all the things on the right in sequence.
This is called a "reduction", and the aim of parsing is to completely
reduce the input. There are several different ways you can perform a
reduction, separated by vertical bars: so, <code class="inline"><span class="w">term</span></code>
 followed by <code class="inline">=</code>

followed by <code class="inline"><span class="w">term</span></code>
 makes a <code class="inline"><span class="w">term</span></code>
, and <code class="inline"><span class="w">term</span></code>
 followed by <code class="inline">+</code>

followed by <code class="inline"><span class="w">term</span></code>
 can also make a <code class="inline"><span class="w">term</span></code>
.</p>
<p>So, if you see two terms with an <code class="inline">=</code>
 or <code class="inline">+</code>
, between them, you can
turn them into a single expression. When you do this, you execute the
code in the block on the next line: if you see <code class="inline">=</code>
, you'll do the code
in line 2. If you see <code class="inline">+</code>
, you'll do the code in line 4. It's this
code which contributes to the op tree.</p>
<pre class="verbatim"><ol><li>            |   term ADDOP term</li><li>            { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }</li></ol></pre><p>What this does is creates a new binary op, and feeds it a number of
variables. The variables refer to the tokens: <code class="inline"><span class="i">$1</span></code>
 is the first token
in the input, <code class="inline"><span class="i">$2</span></code>
 the second, and so on - think regular expression
backreferences. <code class="inline"><span class="i">$$</span></code>
 is the op returned from this reduction. So, we
call <code class="inline"><span class="w">newBINOP</span></code>
 to create a new binary operator. The first parameter
to <code class="inline"><span class="w">newBINOP</span></code>
, a function in <i>op.c</i>, is the op type. It's an addition
operator, so we want the type to be <code class="inline"><span class="w">ADDOP</span></code>
. We could specify this
directly, but it's right there as the second token in the input, so we
use <code class="inline"><span class="i">$2</span></code>
. The second parameter is the op's flags: 0 means "nothing
special". Then the things to add: the left and right hand side of our
expression, in scalar context.</p>
<a name="STACKS"></a><h1>STACKS</h1>
<p>When perl executes something like <code class="inline"><span class="w">addop</span></code>
, how does it pass on its
results to the next op? The answer is, through the use of stacks. Perl
has a number of stacks to store things it's currently working on, and
we'll look at the three most important ones here.</p>
<a name="Argument-stack"></a><h2>Argument stack</h2>
<p>Arguments are passed to PP code and returned from PP code using the
argument stack, <code class="inline"><span class="w">ST</span></code>
. The typical way to handle arguments is to pop
them off the stack, deal with them how you wish, and then push the
result back onto the stack. This is how, for instance, the cosine
operator works:</p>
<pre class="verbatim"><ol><li>      <span class="w">NV</span> <span class="w">value</span><span class="sc">;</span></li><li>      <span class="w">value</span> = <span class="w">POPn</span><span class="sc">;</span></li><li>      <span class="w">value</span> = <span class="i">Perl_cos</span><span class="s">(</span><span class="w">value</span><span class="s">)</span><span class="sc">;</span></li><li>      <span class="i">XPUSHn</span><span class="s">(</span><span class="w">value</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>We'll see a more tricky example of this when we consider Perl's macros
below. <code class="inline"><span class="w">POPn</span></code>
 gives you the NV (floating point value) of the top SV on
the stack: the <code class="inline"><span class="i">$x</span></code>
 in <code class="inline"><a class="l_k" href="functions/cos.html">cos($x)</a></code>. Then we compute the cosine, and
push the result back as an NV. The <code class="inline"><span class="w">X</span></code>
 in <code class="inline"><span class="w">XPUSHn</span></code>
 means that the
stack should be extended if necessary - it can't be necessary here,
because we know there's room for one more item on the stack, since
we've just removed one! The <code class="inline"><span class="w">XPUSH</span>*</code>
 macros at least guarantee safety.</p>
<p>Alternatively, you can fiddle with the stack directly: <code class="inline"><span class="w">SP</span></code>
 gives you
the first element in your portion of the stack, and <code class="inline"><span class="w">TOP</span>*</code>
 gives you
the top SV/IV/NV/etc. on the stack. So, for instance, to do unary
negation of an integer:</p>
<pre class="verbatim"><ol><li>     <span class="i">SETi</span><span class="s">(</span>-<span class="w">TOPi</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Just set the integer value of the top stack entry to its negation.</p>
<p>Argument stack manipulation in the core is exactly the same as it is in
XSUBs - see <a href="perlxstut.html">perlxstut</a>, <a href="perlxs.html">perlxs</a> and <a href="perlguts.html">perlguts</a> for a longer
description of the macros used in stack manipulation.</p>
<a name="Mark-stack"></a><h2>Mark stack</h2>
<p>I say "your portion of the stack" above because PP code doesn't
necessarily get the whole stack to itself: if your function calls
another function, you'll only want to expose the arguments aimed for
the called function, and not (necessarily) let it get at your own data.
The way we do this is to have a "virtual" bottom-of-stack, exposed to
each function. The mark stack keeps bookmarks to locations in the
argument stack usable by each function. For instance, when dealing with
a tied variable, (internally, something with "P" magic) Perl has to
call methods for accesses to the tied variables. However, we need to
separate the arguments exposed to the method to the argument exposed to
the original function - the store or fetch or whatever it may be.
Here's roughly how the tied <code class="inline"><a class="l_k" href="functions/push.html">push</a></code> is implemented; see <code class="inline"><span class="w">av_push</span></code>
 in
<i>av.c</i>:</p>
<pre class="verbatim"><ol><li>     1	PUSHMARK(SP);</li><li>     2	EXTEND(SP,2);</li><li>     3	PUSHs(SvTIED_obj((SV*)av, mg));</li><li>     4	PUSHs(val);</li><li>     5	PUTBACK;</li><li>     6	ENTER;</li><li>     7	call_method("PUSH", G_SCALAR|G_DISCARD);</li><li>     8	LEAVE;</li></ol></pre><p>Let's examine the whole implementation, for practice:</p>
<pre class="verbatim"><ol><li>     1	PUSHMARK(SP);</li></ol></pre><p>Push the current state of the stack pointer onto the mark stack. This
is so that when we've finished adding items to the argument stack, Perl
knows how many things we've added recently.</p>
<pre class="verbatim"><ol><li>     2	EXTEND(SP,2);</li><li>     3	PUSHs(SvTIED_obj((SV*)av, mg));</li><li>     4	PUSHs(val);</li></ol></pre><p>We're going to add two more items onto the argument stack: when you
have a tied array, the <code class="inline"><span class="w">PUSH</span></code>
 subroutine receives the object and the
value to be pushed, and that's exactly what we have here - the tied
object, retrieved with <code class="inline"><span class="w">SvTIED_obj</span></code>
, and the value, the SV <code class="inline"><span class="w">val</span></code>
.</p>
<pre class="verbatim"><ol><li>     5	PUTBACK;</li></ol></pre><p>Next we tell Perl to update the global stack pointer from our internal
variable: <code class="inline"><span class="w">dSP</span></code>
 only gave us a local copy, not a reference to the
global.</p>
<pre class="verbatim"><ol><li>     6	ENTER;</li><li>     7	call_method("PUSH", G_SCALAR|G_DISCARD);</li><li>     8	LEAVE;</li></ol></pre><p><code class="inline"><span class="w">ENTER</span></code>
 and <code class="inline"><span class="w">LEAVE</span></code>
 localise a block of code - they make sure that
all variables are tidied up, everything that has been localised gets
its previous value returned, and so on. Think of them as the <code class="inline">{</code> and
<code class="inline">}</code> of a Perl block.</p>
<p>To actually do the magic method call, we have to call a subroutine in
Perl space: <code class="inline"><span class="w">call_method</span></code>
 takes care of that, and it's described in
<a href="perlcall.html">perlcall</a>. We call the <code class="inline"><span class="w">PUSH</span></code>
 method in scalar context, and we're
going to discard its return value. The call_method() function removes
the top element of the mark stack, so there is nothing for the caller
to clean up.</p>
<a name="Save-stack"></a><h2>Save stack</h2>
<p>C doesn't have a concept of local scope, so perl provides one. We've
seen that <code class="inline"><span class="w">ENTER</span></code>
 and <code class="inline"><span class="w">LEAVE</span></code>
 are used as scoping braces; the save
stack implements the C equivalent of, for example:</p>
<pre class="verbatim"><ol><li>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/local.html">local</a> <span class="i">$foo</span> = <span class="n">42</span><span class="sc">;</span></li><li>        ...</li><li>    <span class="s">}</span></li></ol></pre><p>See <a href="perlguts.html#Localizing-changes">Localizing changes in perlguts</a> for how to use the save stack.</p>
<a name="MILLIONS-OF-MACROS"></a><h1>MILLIONS OF MACROS</h1>
<p>One thing you'll notice about the Perl source is that it's full of
macros. Some have called the pervasive use of macros the hardest thing
to understand, others find it adds to clarity. Let's take an example,
the code which implements the addition operator:</p>
<pre class="verbatim"><ol><li>   1  PP(pp_add)</li><li>   2  {</li><li>   3      dSP; dATARGET; tryAMAGICbin(add,opASSIGN);</li><li>   4      {</li><li>   5        dPOPTOPnnrl_ul;</li><li>   6        SETn( left + right );</li><li>   7        RETURN;</li><li>   8      }</li><li>   9  }</li></ol></pre><p>Every line here (apart from the braces, of course) contains a macro.
The first line sets up the function declaration as Perl expects for PP
code; line 3 sets up variable declarations for the argument stack and
the target, the return value of the operation. Finally, it tries to see
if the addition operation is overloaded; if so, the appropriate
subroutine is called.</p>
<p>Line 5 is another variable declaration - all variable declarations
start with <code class="inline"><span class="w">d</span></code>
 - which pops from the top of the argument stack two NVs
(hence <code class="inline"><span class="w">nn</span></code>
) and puts them into the variables <code class="inline"><span class="w">right</span></code>
 and <code class="inline"><span class="w">left</span></code>
,
hence the <code class="inline"><span class="w">rl</span></code>
. These are the two operands to the addition operator.
Next, we call <code class="inline"><span class="w">SETn</span></code>
 to set the NV of the return value to the result
of adding the two values. This done, we return - the <code class="inline"><span class="w">RETURN</span></code>
 macro
makes sure that our return value is properly handled, and we pass the
next operator to run back to the main run loop.</p>
<p>Most of these macros are explained in <a href="perlapi.html">perlapi</a>, and some of the more
important ones are explained in <a href="perlxs.html">perlxs</a> as well. Pay special
attention to <a href="perlguts.html#Background-and-PERL_IMPLICIT_CONTEXT">Background and PERL_IMPLICIT_CONTEXT in perlguts</a> for
information on the <code class="inline">[pad]THX_?</code> macros.</p>
<a name="FURTHER-READING"></a><h1>FURTHER READING</h1>
<p>For more information on the Perl internals, please see the documents
listed at <a href="perl.html#Internals-and-C-Language-Interface">Internals and C Language Interface in perl</a>.</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#ELEMENTS-OF-THE-INTERPRETER">ELEMENTS OF THE INTERPRETER</a><ul><li><a href="#Startup">Startup</a><li><a href="#Parsing">Parsing</a><li><a href="#Optimization">Optimization</a><li><a href="#Running">Running</a><li><a href="#Exception-handing">Exception handing</a><li><a href="#INTERNAL-VARIABLE-TYPES">INTERNAL VARIABLE TYPES</a></ul><li><a href="#OP-TREES">OP TREES</a><li><a href="#STACKS">STACKS</a><ul><li><a href="#Argument-stack">Argument stack</a><li><a href="#Mark-stack">Mark stack</a><li><a href="#Save-stack">Save stack</a></ul><li><a href="#MILLIONS-OF-MACROS">MILLIONS OF MACROS</a><li><a href="#FURTHER-READING">FURTHER READING</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'perlinterp';
  perldoc.pageAddress = 'perlinterp.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>