📄 class::struct.3
字号:
.IX Item "Scalar ($ or *$)"The element is a scalar, and by default is initialized to \f(CW\*(C`undef\*(C'\fR(but see \*(L"Initializing with new\*(R")..SpThe accessor's argument, if any, is assigned to the element..SpIf the element type is \f(CW\*(Aq$\*(Aq\fR, the value of the element (afterassignment) is returned. If the element type is \f(CW\*(Aq*$\*(Aq\fR, a referenceto the element is returned..ie n .IP "Array (\*(Aq@\*(Aq\fR or \f(CW\*(Aq*@\*(Aq)" 4.el .IP "Array (\f(CW\*(Aq@\*(Aq\fR or \f(CW\*(Aq*@\*(Aq\fR)" 4.IX Item "Array (@ or *@)"The element is an array, initialized by default to \f(CW\*(C`()\*(C'\fR..SpWith no argument, the accessor returns a reference to theelement's whole array (whether or not the element wasspecified as \f(CW\*(Aq@\*(Aq\fR or \f(CW\*(Aq*@\*(Aq\fR)..SpWith one or two arguments, the first argument is an indexspecifying one element of the array; the second argument, ifpresent, is assigned to the array element. If the element typeis \f(CW\*(Aq@\*(Aq\fR, the accessor returns the array element value. If theelement type is \f(CW\*(Aq*@\*(Aq\fR, a reference to the array element isreturned..SpAs a special case, when the accessor is called with an array referenceas the sole argument, this causes an assignment of the whole array element.The object reference is returned..ie n .IP "Hash (\*(Aq%\*(Aq\fR or \f(CW\*(Aq*%\*(Aq)" 4.el .IP "Hash (\f(CW\*(Aq%\*(Aq\fR or \f(CW\*(Aq*%\*(Aq\fR)" 4.IX Item "Hash (% or *%)"The element is a hash, initialized by default to \f(CW\*(C`()\*(C'\fR..SpWith no argument, the accessor returns a reference to theelement's whole hash (whether or not the element wasspecified as \f(CW\*(Aq%\*(Aq\fR or \f(CW\*(Aq*%\*(Aq\fR)..SpWith one or two arguments, the first argument is a key specifyingone element of the hash; the second argument, if present, isassigned to the hash element. If the element type is \f(CW\*(Aq%\*(Aq\fR, theaccessor returns the hash element value. If the element type is\&\f(CW\*(Aq*%\*(Aq\fR, a reference to the hash element is returned..SpAs a special case, when the accessor is called with a hash referenceas the sole argument, this causes an assignment of the whole hash element.The object reference is returned..ie n .IP "Class (\*(AqClass_Name\*(Aq\fR or \f(CW\*(Aq*Class_Name\*(Aq)" 4.el .IP "Class (\f(CW\*(AqClass_Name\*(Aq\fR or \f(CW\*(Aq*Class_Name\*(Aq\fR)" 4.IX Item "Class (Class_Name or *Class_Name)"The element's value must be a reference blessed to the namedclass or to one of its subclasses. The element is not initializedby default..SpThe accessor's argument, if any, is assigned to the element. Theaccessor will \f(CW\*(C`croak\*(C'\fR if this is not an appropriate objectreference..SpIf the element type does not start with a \f(CW\*(Aq*\*(Aq\fR, the accessorreturns the element value (after assignment). If the element typestarts with a \f(CW\*(Aq*\*(Aq\fR, a reference to the element itself is returned..ie n .Sh "Initializing with ""new""".el .Sh "Initializing with \f(CWnew\fP".IX Subsection "Initializing with new"\&\f(CW\*(C`struct\*(C'\fR always creates a constructor called \f(CW\*(C`new\*(C'\fR. That constructormay take a list of initializers for the various elements of the newstruct..PPEach initializer is a pair of values: \fIelement name\fR\f(CW\*(C` => \*(C'\fR\fIvalue\fR.The initializer value for a scalar element is just a scalar value. The initializer for an array element is an array reference. The initializerfor a hash is a hash reference..PPThe initializer for a class element is an object of the corresponding class,or of one of it's subclasses, or a reference to a hash containing named arguments to be passed to the element's constructor..PPSee Example 3 below for an example of initialization..SH "EXAMPLES".IX Header "EXAMPLES".IP "Example 1" 4.IX Item "Example 1"Giving a struct element a class type that is also a struct is howstructs are nested. Here, \f(CW\*(C`Timeval\*(C'\fR represents a time (seconds andmicroseconds), and \f(CW\*(C`Rusage\*(C'\fR has two elements, each of which is oftype \f(CW\*(C`Timeval\*(C'\fR..Sp.Vb 1\& use Class::Struct;\&\& struct( Rusage => {\& ru_utime => \*(AqTimeval\*(Aq, # user time used\& ru_stime => \*(AqTimeval\*(Aq, # system time used\& });\&\& struct( Timeval => [\& tv_secs => \*(Aq$\*(Aq, # seconds\& tv_usecs => \*(Aq$\*(Aq, # microseconds\& ]);\&\& # create an object:\& my $t = Rusage\->new(ru_utime=>Timeval\->new(), ru_stime=>Timeval\->new());\&\& # $t\->ru_utime and $t\->ru_stime are objects of type Timeval.\& # set $t\->ru_utime to 100.0 sec and $t\->ru_stime to 5.0 sec.\& $t\->ru_utime\->tv_secs(100);\& $t\->ru_utime\->tv_usecs(0);\& $t\->ru_stime\->tv_secs(5);\& $t\->ru_stime\->tv_usecs(0);.Ve.IP "Example 2" 4.IX Item "Example 2"An accessor function can be redefined in order to provideadditional checking of values, etc. Here, we want the \f(CW\*(C`count\*(C'\fRelement always to be nonnegative, so we redefine the \f(CW\*(C`count\*(C'\fRaccessor accordingly..Sp.Vb 2\& package MyObj;\& use Class::Struct;\&\& # declare the struct\& struct ( \*(AqMyObj\*(Aq, { count => \*(Aq$\*(Aq, stuff => \*(Aq%\*(Aq } );\&\& # override the default accessor method for \*(Aqcount\*(Aq\& sub count {\& my $self = shift;\& if ( @_ ) {\& die \*(Aqcount must be nonnegative\*(Aq if $_[0] < 0;\& $self\->{\*(AqMyObj::count\*(Aq} = shift;\& warn "Too many args to count" if @_;\& }\& return $self\->{\*(AqMyObj::count\*(Aq};\& }\&\& package main;\& $x = new MyObj;\& print "\e$x\->count(5) = ", $x\->count(5), "\en";\& # prints \*(Aq$x\->count(5) = 5\*(Aq\&\& print "\e$x\->count = ", $x\->count, "\en";\& # prints \*(Aq$x\->count = 5\*(Aq\&\& print "\e$x\->count(\-5) = ", $x\->count(\-5), "\en";\& # dies due to negative argument!.Ve.IP "Example 3" 4.IX Item "Example 3"The constructor of a generated class can be passed a listof \fIelement\fR=>\fIvalue\fR pairs, with which to initialize the struct.If no initializer is specified for a particular element, its defaultinitialization is performed instead. Initializers for non-existentelements are silently ignored..SpNote that the initializer for a nested class may be specified asan object of that class, or as a reference to a hash of initializersthat are passed on to the nested struct's constructor..Sp.Vb 1\& use Class::Struct;\&\& struct Breed =>\& {\& name => \*(Aq$\*(Aq,\& cross => \*(Aq$\*(Aq,\& };\&\& struct Cat =>\& [\& name => \*(Aq$\*(Aq,\& kittens => \*(Aq@\*(Aq,\& markings => \*(Aq%\*(Aq,\& breed => \*(AqBreed\*(Aq,\& ];\&\&\& my $cat = Cat\->new( name => \*(AqSocks\*(Aq,\& kittens => [\*(AqMonica\*(Aq, \*(AqKenneth\*(Aq],\& markings => { socks=>1, blaze=>"white" },\& breed => Breed\->new(name=>\*(Aqshort\-hair\*(Aq, cross=>1),\& or: breed => {name=>\*(Aqshort\-hair\*(Aq, cross=>1},\& );\&\& print "Once a cat called ", $cat\->name, "\en";\& print "(which was a ", $cat\->breed\->name, ")\en";\& print "had two kittens: ", join(\*(Aq and \*(Aq, @{$cat\->kittens}), "\en";.Ve.SH "Author and Modification History".IX Header "Author and Modification History"Modified by Damian Conway, 2001\-09\-10, v0.62..PP.Vb 11\& Modified implicit construction of nested objects.\& Now will also take an object ref instead of requiring a hash ref.\& Also default initializes nested object attributes to undef, rather\& than calling object constructor without args\& Original over\-helpfulness was fraught with problems:\& * the class\*(Aqs constructor might not be called \*(Aqnew\*(Aq\& * the class might not have a hash\-like\-arguments constructor\& * the class might not have a no\-argument constructor\& * "recursive" data structures didn\*(Aqt work well:\& package Person;\& struct { mother => \*(AqPerson\*(Aq, father => \*(AqPerson\*(Aq};.Ve.PPModified by Casey West, 2000\-11\-08, v0.59..PP.Vb 1\& Added the ability for compile time class creation..Ve.PPModified by Damian Conway, 1999\-03\-05, v0.58..PP.Vb 1\& Added handling of hash\-like arg list to class ctor.\&\& Changed to two\-argument blessing in ctor to support\& derivation from created classes.\&\& Added classname prefixes to keys in hash\-based classes\& (refer to "Perl Cookbook", Recipe 13.12 for rationale).\&\& Corrected behaviour of accessors for \*(Aq*@\*(Aq and \*(Aq*%\*(Aq struct\& elements. Package now implements documented behaviour when\& returning a reference to an entire hash or array element.\& Previously these were returned as a reference to a reference\& to the element..Ve.PPRenamed to \f(CW\*(C`Class::Struct\*(C'\fR and modified by Jim Miner, 1997\-04\-02..PP.Vb 8\& members() function removed.\& Documentation corrected and extended.\& Use of struct() in a subclass prohibited.\& User definition of accessor allowed.\& Treatment of \*(Aq*\*(Aq in element types corrected.\& Treatment of classes as element types corrected.\& Class name to struct() made optional.\& Diagnostic checks added..Ve.PPOriginally \f(CW\*(C`Class::Template\*(C'\fR by Dean Roehrich..PP.Vb 10\& # Template.pm \-\-\- struct/member template builder\& # 12mar95\& # Dean Roehrich\& #\& # changes/bugs fixed since 28nov94 version:\& # \- podified\& # changes/bugs fixed since 21nov94 version:\& # \- Fixed examples.\& # changes/bugs fixed since 02sep94 version:\& # \- Moved to Class::Template.\& # changes/bugs fixed since 20feb94 version:\& # \- Updated to be a more proper module.\& # \- Added "use strict".\& # \- Bug in build_methods, was using @var when @$var needed.\& # \- Now using my() rather than local().\& #\& # Uses perl5 classes to create nested data types.\& # This is offered as one implementation of Tom Christiansen\*(Aqs "structs.pl"\& # idea..Ve
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -