📄 perlboot.1
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings. \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote. \*(C+ will.\" give a nicer C++. Capital omega is used to do unbreakable dashes and.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\. ds -- \(*W-. ds PI pi. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch. ds L" "". ds R" "". ds C` "". ds C' ""'br\}.el\{\. ds -- \|\(em\|. ds PI \(*p. ds L" ``. ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "PERLBOOT 1".TH PERLBOOT 1 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" For nroff, turn off justification. Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"perlboot \- Beginner's Object\-Oriented Tutorial.SH "DESCRIPTION".IX Header "DESCRIPTION"If you're not familiar with objects from other languages, some of theother Perl object documentation may be a little daunting, such asperlobj, a basic reference in using objects, and perltoot, whichintroduces readers to the peculiarities of Perl's object system in atutorial way..PPSo, let's take a different approach, presuming no prior objectexperience. It helps if you know about subroutines (perlsub),references (perlref et. seq.), and packages (perlmod), so becomefamiliar with those first if you haven't already..Sh "If we could talk to the animals...".IX Subsection "If we could talk to the animals..."Let's let the animals talk for a moment:.PP.Vb 9\& sub Cow::speak {\& print "a Cow goes moooo!\en";\& }\& sub Horse::speak {\& print "a Horse goes neigh!\en";\& }\& sub Sheep::speak {\& print "a Sheep goes baaaah!\en";\& }\&\& Cow::speak;\& Horse::speak;\& Sheep::speak;.Ve.PPThis results in:.PP.Vb 3\& a Cow goes moooo!\& a Horse goes neigh!\& a Sheep goes baaaah!.Ve.PPNothing spectacular here. Simple subroutines, albeit from separatepackages, and called using the full package name. So let's createan entire pasture:.PP.Vb 5\& # Cow::speak, Horse::speak, Sheep::speak as before\& @pasture = qw(Cow Cow Horse Sheep Sheep);\& foreach $animal (@pasture) {\& &{$animal."::speak"};\& }.Ve.PPThis results in:.PP.Vb 5\& a Cow goes moooo!\& a Cow goes moooo!\& a Horse goes neigh!\& a Sheep goes baaaah!\& a Sheep goes baaaah!.Ve.PPWow. That symbolic coderef de-referencing there is pretty nasty.We're counting on \f(CW\*(C`no strict subs\*(C'\fR mode, certainly not recommendedfor larger programs. And why was that necessary? Because the name ofthe package seems to be inseparable from the name of the subroutine wewant to invoke within that package..PPOr is it?.Sh "Introducing the method invocation arrow".IX Subsection "Introducing the method invocation arrow"For now, let's say that \f(CW\*(C`Class\->method\*(C'\fR invokes subroutine\&\f(CW\*(C`method\*(C'\fR in package \f(CW\*(C`Class\*(C'\fR. (Here, \*(L"Class\*(R" is used in its\&\*(L"category\*(R" meaning, not its \*(L"scholastic\*(R" meaning.) That's notcompletely accurate, but we'll do this one step at a time. Now let'suse it like so:.PP.Vb 4\& # Cow::speak, Horse::speak, Sheep::speak as before\& Cow\->speak;\& Horse\->speak;\& Sheep\->speak;.Ve.PPAnd once again, this results in:.PP.Vb 3\& a Cow goes moooo!\& a Horse goes neigh!\& a Sheep goes baaaah!.Ve.PPThat's not fun yet. Same number of characters, all constant, novariables. But yet, the parts are separable now. Watch:.PP.Vb 2\& $a = "Cow";\& $a\->speak; # invokes Cow\->speak.Ve.PPAhh! Now that the package name has been parted from the subroutinename, we can use a variable package name. And this time, we've gotsomething that works even when \f(CW\*(C`use strict refs\*(C'\fR is enabled..Sh "Invoking a barnyard".IX Subsection "Invoking a barnyard"Let's take that new arrow invocation and put it back in the barnyardexample:.PP.Vb 9\& sub Cow::speak {\& print "a Cow goes moooo!\en";\& }\& sub Horse::speak {\& print "a Horse goes neigh!\en";\& }\& sub Sheep::speak {\& print "a Sheep goes baaaah!\en";\& }\&\& @pasture = qw(Cow Cow Horse Sheep Sheep);\& foreach $animal (@pasture) {\& $animal\->speak;\& }.Ve.PPThere! Now we have the animals all talking, and safely at that,without the use of symbolic coderefs..PPBut look at all that common code. Each of the \f(CW\*(C`speak\*(C'\fR routines has asimilar structure: a \f(CW\*(C`print\*(C'\fR operator and a string that containscommon text, except for two of the words. It'd be nice if we couldfactor out the commonality, in case we decide later to change it allto \f(CW\*(C`says\*(C'\fR instead of \f(CW\*(C`goes\*(C'\fR..PPAnd we actually have a way of doing that without much fuss, but wehave to hear a bit more about what the method invocation arrow isactually doing for us..Sh "The extra parameter of method invocation".IX Subsection "The extra parameter of method invocation"The invocation of:.PP.Vb 1\& Class\->method(@args).Ve.PPattempts to invoke subroutine \f(CW\*(C`Class::method\*(C'\fR as:.PP.Vb 1\& Class::method("Class", @args);.Ve.PP(If the subroutine can't be found, \*(L"inheritance\*(R" kicks in, but we'llget to that later.) This means that we get the class name as thefirst parameter (the only parameter, if no arguments are given). Sowe can rewrite the \f(CW\*(C`Sheep\*(C'\fR speaking subroutine as:.PP.Vb 4\& sub Sheep::speak {\& my $class = shift;\& print "a $class goes baaaah!\en";\& }.Ve.PPAnd the other two animals come out similarly:.PP.Vb 8\& sub Cow::speak {\& my $class = shift;\& print "a $class goes moooo!\en";\& }\& sub Horse::speak {\& my $class = shift;\& print "a $class goes neigh!\en";\& }.Ve.PPIn each case, \f(CW$class\fR will get the value appropriate for thatsubroutine. But once again, we have a lot of similar structure. Canwe factor that out even further? Yes, by calling another method inthe same class..Sh "Calling a second method to simplify things".IX Subsection "Calling a second method to simplify things"Let's call out from \f(CW\*(C`speak\*(C'\fR to a helper method called \f(CW\*(C`sound\*(C'\fR.This method provides the constant text for the sound itself..PP.Vb 7\& { package Cow;\& sub sound { "moooo" }\& sub speak {\& my $class = shift;\& print "a $class goes ", $class\->sound, "!\en";\& }\& }.Ve.PPNow, when we call \f(CW\*(C`Cow\->speak\*(C'\fR, we get a \f(CW$class\fR of \f(CW\*(C`Cow\*(C'\fR in\&\f(CW\*(C`speak\*(C'\fR. This in turn selects the \f(CW\*(C`Cow\->sound\*(C'\fR method, whichreturns \f(CW\*(C`moooo\*(C'\fR. But how different would this be for the \f(CW\*(C`Horse\*(C'\fR?.PP.Vb 7\& { package Horse;\& sub sound { "neigh" }\& sub speak {\& my $class = shift;\& print "a $class goes ", $class\->sound, "!\en";\& }\& }.Ve.PP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -