⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch25.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<HTML>



<HEAD>

   <TITLE>Chapter 25 -- Perl Internal Files and Structures</TITLE>

   <META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]">

</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">

<H1><FONT COLOR=#FF0000>Chapter 25</FONT></H1>

<H1><B><FONT SIZE=5 COLOR=#FF0000>Perl Internal Files and Structures</FONT></B>

</H1>

<P>

<HR WIDTH="100%"></P>

<P>

<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>

</FONT></FONT></H3>



<UL>

<LI><A HREF="#Introduction" >Introduction</A>

<UL>

<LI><A HREF="#ExploringPerlSourceCode" >Exploring Perl Source Code</A>

</UL>

<LI><A HREF="#PerlVariableTypes" >Perl Variable Types</A>

<UL>

<LI><A HREF="#NamingConventions" >Naming Conventions</A>

</UL>

<LI><A HREF="#ScalarsandScalarFunctions" >Scalars and Scalar Functions</A>

<LI><A HREF="#ArrayFunctions" >Array Functions</A>

<LI><A HREF="#HashFunctions" >Hash Functions</A>

<LI><A HREF="#MortalityofVariables" >Mortality of Variables</A>

<LI><A HREF="#SubroutinesandStacks" >Subroutines and Stacks</A>

<LI><A HREF="#WhatIsMagic" >What Is Magic?</A>

<LI><A HREF="#TheGlobalVariableGVType" >The Global Variable (GV) Type</A>

<LI><A HREF="#WheretoLookforMoreInformation" >Where to Look for More Information</A>

<LI><A HREF="#Summary" >Summary</A>

</UL>

<HR>

<P>

This chapter introduces some of Perl's internal data structures,

and the information presented here serves as a reference for the

rest of the book. This chapter will be useful not only for programmers

who want to add their own extensions to Perl but also for those

who simply want to look at the Perl source code to see what's

&quot;under the hood.&quot;

<H2><A NAME="Introduction"><B><FONT SIZE=5 COLOR=#FF0000>Introduction</FONT></B></A>

</H2>

<P>

Perl is written primarily in C and has libraries to which you

can link in your own C/C++ code. To perform this linking, however,

your programs have to know how Perl stores its own data structures

as well as how to interpret Perl's data types.

<P>

The information presented in this chapter also will show you where

to look for files, structures, and so on. There will be times

when you are writing extensions or modules that you'll need to

look up specific data structure definitions. The functions defined

here are called from your extension's C sources.

<P>

Version 5.002b was the latest Perl release at the time this book

was written. The <I>b</I> stands for beta; therefore, some changes

to the source tree are quite possible. What you see here will

not only be a snapshot in time of the source tree for 5.002b,

but will also serve as a basis for you to do your own research.

<P>

The information in this chapter is about the functions you can

call from C functions that interact with Perl variables. Your

C code could be calling the Perl functions, or your Perl function

could be calling your C code as part of an extension. The C functions

have to be linked with the Perl libraries and also require the

header files in your Perl distribution.

<P>

The compiler is guaranteed to work with Perl on almost all platforms

in the GNU C compiler. If you have problems compiling with other

commercial compilers, then get the GNU compiler from the Net.

A good place to try is the <TT><FONT FACE="Courier">oak.oakland.edu

ftp</FONT></TT> site.

<H3><A NAME="ExploringPerlSourceCode"><B>Exploring Perl Source

Code</B></A></H3>

<P>

This section covers some of the header files in your Perl distribution.

Table 25.1 provides a brief description of what the ones covered

here contain. You can track the values or specific definitions

by starting from these header files.<BR>

<P>

<CENTER><B>Table 25.1. The Perl header files.</B></CENTER>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD WIDTH=138><I>File</I></TD><TD WIDTH=453><I>Purpose</I>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">XSUB.h</FONT></TT>

</TD><TD WIDTH=453>Defines the <TT><FONT FACE="Courier">XSUB</FONT></TT> interface (<A HREF="ch27.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch27.htm" >see Chapter 27</A>, &quot;Writing Extensions in C,&quot; for more information)

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">av.h</FONT></TT></TD>

<TD WIDTH=453>Array variable information</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">config.h</FONT></TT>

</TD><TD WIDTH=453>Generated when Perl is installed</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">cop.h</FONT></TT></TD>

<TD WIDTH=453>Glob pointers</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">cv.h</FONT></TT></TD>

<TD WIDTH=453>Conversion structure</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">dosish.h</FONT></TT>

</TD><TD WIDTH=453>Redefining <TT><FONT FACE="Courier">stat</FONT></TT>, <TT><FONT FACE="Courier">fstat</FONT></TT>, and <TT><FONT FACE="Courier">fflush</FONT></TT> for DOS

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">embed.h</FONT></TT>

</TD><TD WIDTH=453>For embedding Perl in C</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">EXTERN.h</FONT></TT> 

</TD><TD WIDTH=453>Global and external variables</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">form.h</FONT></TT>

</TD><TD WIDTH=453>For form feed and line feed definitions</TD>

</TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">gv.h</FONT></TT></TD>

<TD WIDTH=453>Glob pointer definitions</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">handy.h</FONT></TT>

</TD><TD WIDTH=453>Used for embedding Perl in C</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">hv.h</FONT></TT></TD>

<TD WIDTH=453>Hash definitions</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">INTERN.h</FONT></TT>

</TD><TD WIDTH=453>Perl internal variables</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">keywords.h</FONT></TT>

</TD><TD WIDTH=453>For Perl keywords</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">mg.h</FONT></TT></TD>

<TD WIDTH=453>Definitions for using <TT><FONT FACE="Courier">MAGIC</FONT></TT> structures

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">op.h</FONT></TT></TD>

<TD WIDTH=453>Perl operators</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">patchlevel.h</FONT></TT>

</TD><TD WIDTH=453>For current <TT><FONT FACE="Courier">patchlevel</FONT></TT> information

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">pp.h</FONT></TT> </TD>

<TD WIDTH=453>For preprocessor directives</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">perl.h</FONT></TT>

</TD><TD WIDTH=453>Main header for Perl</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">perly.h</FONT></TT>

</TD><TD WIDTH=453>For the <TT><FONT FACE="Courier">yylex</FONT></TT> parser

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">proto.h</FONT></TT>

</TD><TD WIDTH=453>Function prototypes</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">regexp.h</FONT></TT>

</TD><TD WIDTH=453>Regular expressions</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">scope.h</FONT></TT>

</TD><TD WIDTH=453>Scoping rule definitions</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">sv.h</FONT></TT></TD>

<TD WIDTH=453>Scalar values</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">util.h</FONT></TT>

</TD><TD WIDTH=453>Blank header file</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">unixish.h</FONT></TT>

</TD><TD WIDTH=453>For UNIX-specific definitions</TD></TR>

</TABLE></CENTER>

<P>

<P>

The source files in the Perl distribution are as follows. They

come with very sparse comments.<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD WIDTH=121><TT><FONT FACE="Courier">av.c</FONT></TT></TD>

<TD WIDTH=168><FONT FACE="Courier">mg.c</FONT></TD><TD WIDTH=144><FONT FACE="Courier">pp_sys.c</FONT>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=121><TT><FONT FACE="Courier">deb.c</FONT></TT></TD>

<TD WIDTH=168><FONT FACE="Courier">miniperlmain.c</FONT></TD>

<TD WIDTH=144><FONT FACE="Courier">regcomp.c</FONT></TD></TR>

<TR VALIGN=TOP><TD WIDTH=121><TT><FONT FACE="Courier">doio.c</FONT></TT>

</TD><TD WIDTH=168><FONT FACE="Courier">op.c</FONT></TD><TD WIDTH=144><FONT FACE="Courier">regexec.c</FONT>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=121><TT><FONT FACE="Courier">doop.c</FONT></TT>

</TD><TD WIDTH=168><FONT FACE="Courier">perl.c</FONT></TD><TD WIDTH=144><FONT FACE="Courier">run.c</FONT>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=121><TT><FONT FACE="Courier">dump.c</FONT></TT>

</TD><TD WIDTH=168><FONT FACE="Courier">perlmain.c</FONT></TD>

<TD WIDTH=144><FONT FACE="Courier">scope.c</FONT></TD></TR>

<TR VALIGN=TOP><TD WIDTH=121><TT><FONT FACE="Courier">globals.c</FONT></TT>

</TD><TD WIDTH=168><FONT FACE="Courier">perly.c</FONT></TD><TD WIDTH=144><FONT FACE="Courier">sv.c</FONT>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=121><TT><FONT FACE="Courier">gv.c</FONT></TT></TD>

<TD WIDTH=168><FONT FACE="Courier">pp.c</FONT></TD><TD WIDTH=144><FONT FACE="Courier">taint.c</FONT>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=121><TT><FONT FACE="Courier">hv.c</FONT></TT></TD>

<TD WIDTH=168><FONT FACE="Courier">pp_ctl.c</FONT></TD><TD WIDTH=144><FONT FACE="Courier">toke.c</FONT>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=121><TT><FONT FACE="Courier">malloc.c</FONT></TT>

</TD><TD WIDTH=168><FONT FACE="Courier">pp_hot.c</FONT></TD><TD WIDTH=144><FONT FACE="Courier">util.c</FONT>

</TD></TR>

</TABLE></CENTER>

<P>

<P>

The name of each file gives a hint as to what the code in the

file does. Run <TT><FONT FACE="Courier">head *.c &gt; text</FONT></TT>

to get a list of the headers for the files.

<P>

Now that you know a little about what source files to consult,

you're ready to learn about the building blocks of Perl programs:

the variables.

<H2><A NAME="PerlVariableTypes"><B><FONT SIZE=5 COLOR=#FF0000>Perl

Variable Types</FONT></B></A></H2>

<P>

Perl has three basic data types: scalars, arrays, and hashes.

Perl enables you to have references to these data types as well

as references to subroutines. Most references use scalar values

to store their values, but you can have arrays of arrays, arrays

of references, and so on. It's quite possible to build complicated

data structures using the three basic types in Perl.

<P>

Variables in Perl programs can even have two types of values,

depending on how they are interpreted. For instance, <TT><FONT FACE="Courier">$i</FONT></TT>

can be an integer when used in a numeric operation, and <TT><FONT FACE="Courier">$i</FONT></TT>

is a string when used in a string operation. Another example is

the <TT><FONT FACE="Courier">$!</FONT></TT>, which is the <TT><FONT FACE="Courier">errno</FONT></TT>

code when used as a number but a string when used within a <TT><FONT FACE="Courier">print</FONT></TT>

statement.

<H3><A NAME="NamingConventions"><B>Naming Conventions</B></A>

</H3>

<P>

Because variables internal to Perl source code can have many types

of values and definitions, the name must be descriptive enough

to indicate what type it is. By convention, there are three tokens

in Perl source code variable names: arrays, hashes, and scalar

variables. A scalar variable can be further qualified to define

the type of value it holds. The list of token prefixes for these

Perl types are shown in the following list:<P>

<CENTER>

<TABLE BORDER=1>

<TR VALIGN=TOP><TD WIDTH=100><BLOCKQUOTE>

<TT><FONT FACE="Courier">AV</FONT></TT>

</BLOCKQUOTE>



</TD><TD WIDTH=246><BLOCKQUOTE>

Array variables</BLOCKQUOTE>



</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><BLOCKQUOTE>

<TT><FONT FACE="Courier">HV</FONT></TT>

</BLOCKQUOTE>



</TD><TD WIDTH=246><BLOCKQUOTE>

Hash variables</BLOCKQUOTE>



</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><BLOCKQUOTE>

<TT><FONT FACE="Courier">SV</FONT></TT>

</BLOCKQUOTE>



</TD><TD WIDTH=246><BLOCKQUOTE>

Generic scalar variables</BLOCKQUOTE>



</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><BLOCKQUOTE>

<TT><FONT FACE="Courier">I32</FONT></TT>

</BLOCKQUOTE>



</TD><TD WIDTH=246><BLOCKQUOTE>

32-bit integer (scalar)</BLOCKQUOTE>



</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><BLOCKQUOTE>

<TT><FONT FACE="Courier">I16</FONT></TT>

</BLOCKQUOTE>



</TD><TD WIDTH=246><BLOCKQUOTE>

16-bit integer (scalar)</BLOCKQUOTE>



</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><BLOCKQUOTE>

<TT><FONT FACE="Courier">IV</FONT></TT>

</BLOCKQUOTE>



</TD><TD WIDTH=246><BLOCKQUOTE>

Integer or pointer only (scalar)</BLOCKQUOTE>



</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><BLOCKQUOTE>

<TT><FONT FACE="Courier">NV</FONT></TT>

</BLOCKQUOTE>



</TD><TD WIDTH=246><BLOCKQUOTE>

Double only (scalar)</BLOCKQUOTE>



</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><BLOCKQUOTE>

<TT><FONT FACE="Courier">PV</FONT></TT>

</BLOCKQUOTE>



</TD><TD WIDTH=246><BLOCKQUOTE>

String pointer only (scalar)</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<P>

If you see <TT><FONT FACE="Courier">SV</FONT></TT> in a function

or variable name, the function is probably working on a scalar

item. The convention is followed closely in the Perl source code,

and you should be able to glean the type of most variable names

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -