Stacks and Messaging Protocol
Whew! We have now finished a reasonably in-depth look at all the value types offered by Perl. The next half of this chapter is devoted to understanding the data structures, API, and protocol used between caller and called subroutines.
We mentioned earlier that the argument stack is the data structure
used for passing parameters and results between functions. Figure 20.11 shows the stack after calling
foo(10,20)
, which in turn has called
bar("hello",
30.2,
100)
.
Figure 20-11. Argument and mark stack after foo has been called and foo has just called bar
How does bar
know how many parameters it should
pick up from the top of stack
? Well, Perl keeps
track of the stretches of the argument stack using another stack
called a
markstack
(a stack of bookmarks, in a sense). bar
knows the
parameters meant for it by simply computing the difference between
the current top of stack
and the bookmark stored
at the top of markstack
. This stretch of the stack
corresponds to bar
’s @_
array. Conversely, when bar
is ready to return, it
dumps one or more results in its stretch of stack, and
foo
knows how many scalars have been returned by
looking at the markstack.
All these manipulations happen transparently when you are in script space. But if you write C routines that are called by Perl (extending Perl) or call Perl functions from C (embedding Perl), there are some details ...
Get Advanced Perl Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.