Server IP : 103.119.228.120 / Your IP : 3.15.239.50 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/ssl/local/ssl/local/ssl/local/ssl/local/share/man/man3/ |
Upload File : |
.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .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" '' . ds C` . ds C' '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 (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Class::Accessor 3" .TH Class::Accessor 3 "2009-09-15" "perl v5.16.3" "User Contributed Perl Documentation" .\" 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" .Vb 1 \& Class::Accessor \- Automated accessor generation .Ve .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 4 \& package Foo; \& use base qw(Class::Accessor); \& Foo\->follow_best_practice; \& Foo\->mk_accessors(qw(name role salary)); \& \& # or if you prefer a Moose\-like interface... \& \& package Foo; \& use Class::Accessor "antlers"; \& has name => ( is => "rw", isa => "Str" ); \& has role => ( is => "rw", isa => "Str" ); \& has salary => ( is => "rw", isa => "Num" ); \& \& # Meanwhile, in a nearby piece of code! \& # Class::Accessor provides new(). \& my $mp = Foo\->new({ name => "Marty", role => "JAPH" }); \& \& my $job = $mp\->role; # gets $mp\->{role} \& $mp\->salary(400000); # sets $mp\->{salary} = 400000 # I wish \& \& # like my @info = @{$mp}{qw(name role)} \& my @info = $mp\->get(qw(name role)); \& \& # $mp\->{salary} = 400000 \& $mp\->set(\*(Aqsalary\*(Aq, 400000); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module automagically generates accessors/mutators for your class. .PP Most of the time, writing accessors is an exercise in cutting and pasting. You usually wind up with a series of methods like this: .PP .Vb 7 \& sub name { \& my $self = shift; \& if(@_) { \& $self\->{name} = $_[0]; \& } \& return $self\->{name}; \& } \& \& sub salary { \& my $self = shift; \& if(@_) { \& $self\->{salary} = $_[0]; \& } \& return $self\->{salary}; \& } \& \& # etc... .Ve .PP One for each piece of data in your object. While some will be unique, doing value checks and special storage tricks, most will simply be exercises in repetition. Not only is it Bad Style to have a bunch of repetitious code, but it's also simply not lazy, which is the real tragedy. .PP If you make your module a subclass of Class::Accessor and declare your accessor fields with \fImk_accessors()\fR then you'll find yourself with a set of automatically generated accessors which can even be customized! .PP The basic set up is very simple: .PP .Vb 3 \& package Foo; \& use base qw(Class::Accessor); \& Foo\->mk_accessors( qw(far bar car) ); .Ve .PP Done. Foo now has simple \fIfar()\fR, \fIbar()\fR and \fIcar()\fR accessors defined. .PP Alternatively, if you want to follow Damian's \fIbest practice\fR guidelines you can use: .PP .Vb 4 \& package Foo; \& use base qw(Class::Accessor); \& Foo\->follow_best_practice; \& Foo\->mk_accessors( qw(far bar car) ); .Ve .PP \&\fBNote:\fR you must call \f(CW\*(C`follow_best_practice\*(C'\fR before calling \f(CW\*(C`mk_accessors\*(C'\fR. .SS "Moose-like" .IX Subsection "Moose-like" By popular demand we now have a simple Moose-like interface. You can now do: .PP .Vb 5 \& package Foo; \& use Class::Accessor "antlers"; \& has far => ( is => "rw" ); \& has bar => ( is => "rw" ); \& has car => ( is => "rw" ); .Ve .PP Currently only the \f(CW\*(C`is\*(C'\fR attribute is supported. .SH "CONSTRUCTOR" .IX Header "CONSTRUCTOR" Class::Accessor provides a basic constructor, \f(CW\*(C`new\*(C'\fR. It generates a hash-based object and can be called as either a class method or an object method. .SS "new" .IX Subsection "new" .Vb 2 \& my $obj = Foo\->new; \& my $obj = $other_obj\->new; \& \& my $obj = Foo\->new(\e%fields); \& my $obj = $other_obj\->new(\e%fields); .Ve .PP It takes an optional \f(CW%fields\fR hash which is used to initialize the object (handy if you use read-only accessors). The fields of the hash correspond to the names of your accessors, so... .PP .Vb 3 \& package Foo; \& use base qw(Class::Accessor); \& Foo\->mk_accessors(\*(Aqfoo\*(Aq); \& \& my $obj = Foo\->new({ foo => 42 }); \& print $obj\->foo; # 42 .Ve .PP however \f(CW%fields\fR can contain anything, \fInew()\fR will shove them all into your object. .SH "MAKING ACCESSORS" .IX Header "MAKING ACCESSORS" .SS "follow_best_practice" .IX Subsection "follow_best_practice" In Damian's Perl Best Practices book he recommends separate get and set methods with the prefix set_ and get_ to make it explicit what you intend to do. If you want to create those accessor methods instead of the default ones, call: .PP .Vb 1 \& _\|_PACKAGE_\|_\->follow_best_practice .Ve .PP \&\fBbefore\fR you call any of the accessor-making methods. .SS "accessor_name_for / mutator_name_for" .IX Subsection "accessor_name_for / mutator_name_for" You may have your own crazy ideas for the names of the accessors, so you can make those happen by overriding \f(CW\*(C`accessor_name_for\*(C'\fR and \f(CW\*(C`mutator_name_for\*(C'\fR in your subclass. (I copied that idea from Class::DBI.) .SS "mk_accessors" .IX Subsection "mk_accessors" .Vb 1 \& _\|_PACKAGE_\|_\->mk_accessors(@fields); .Ve .PP This creates accessor/mutator methods for each named field given in \&\f(CW@fields\fR. Foreach field in \f(CW@fields\fR it will generate two accessors. One called \*(L"\fIfield()\fR\*(R" and the other called \*(L"\fI_field_accessor()\fR\*(R". For example: .PP .Vb 2 \& # Generates foo(), _foo_accessor(), bar() and _bar_accessor(). \& _\|_PACKAGE_\|_\->mk_accessors(qw(foo bar)); .Ve .PP See \*(L"Overriding autogenerated accessors\*(R" in \s-1CAVEATS AND TRICKS\s0 for details. .SS "mk_ro_accessors" .IX Subsection "mk_ro_accessors" .Vb 1 \& _\|_PACKAGE_\|_\->mk_ro_accessors(@read_only_fields); .Ve .PP Same as \fImk_accessors()\fR except it will generate read-only accessors (ie. true accessors). If you attempt to set a value with these accessors it will throw an exception. It only uses \fIget()\fR and not \&\fIset()\fR. .PP .Vb 3 \& package Foo; \& use base qw(Class::Accessor); \& Foo\->mk_ro_accessors(qw(foo bar)); \& \& # Let\*(Aqs assume we have an object $foo of class Foo... \& print $foo\->foo; # ok, prints whatever the value of $foo\->{foo} is \& $foo\->foo(42); # BOOM! Naughty you. .Ve .SS "mk_wo_accessors" .IX Subsection "mk_wo_accessors" .Vb 1 \& _\|_PACKAGE_\|_\->mk_wo_accessors(@write_only_fields); .Ve .PP Same as \fImk_accessors()\fR except it will generate write-only accessors (ie. mutators). If you attempt to read a value with these accessors it will throw an exception. It only uses \fIset()\fR and not \fIget()\fR. .PP \&\fB\s-1NOTE\s0\fR I'm not entirely sure why this is useful, but I'm sure someone will need it. If you've found a use, let me know. Right now it's here for orthoginality and because it's easy to implement. .PP .Vb 3 \& package Foo; \& use base qw(Class::Accessor); \& Foo\->mk_wo_accessors(qw(foo bar)); \& \& # Let\*(Aqs assume we have an object $foo of class Foo... \& $foo\->foo(42); # OK. Sets $self\->{foo} = 42 \& print $foo\->foo; # BOOM! Can\*(Aqt read from this accessor. .Ve .SH "Moose!" .IX Header "Moose!" If you prefer a Moose-like interface to create accessors, you can use \f(CW\*(C`has\*(C'\fR by importing this module like this: .PP .Vb 1 \& use Class::Accessor "antlers"; .Ve .PP or .PP .Vb 1 \& use Class::Accessor "moose\-like"; .Ve .PP Then you can declare accessors like this: .PP .Vb 3 \& has alpha => ( is => "rw", isa => "Str" ); \& has beta => ( is => "ro", isa => "Str" ); \& has gamma => ( is => "wo", isa => "Str" ); .Ve .PP Currently only the \f(CW\*(C`is\*(C'\fR attribute is supported. And our \f(CW\*(C`is\*(C'\fR also supports the \*(L"wo\*(R" value to make a write-only accessor. .PP If you are using the Moose-like interface then you should use the \f(CW\*(C`extends\*(C'\fR rather than tweaking your \f(CW@ISA\fR directly. Basically, replace .PP .Vb 1 \& @ISA = qw/Foo Bar/; .Ve .PP with .PP .Vb 1 \& extends(qw/Foo Bar/); .Ve .SH "DETAILS" .IX Header "DETAILS" An accessor generated by Class::Accessor looks something like this: .PP .Vb 10 \& # Your foo may vary. \& sub foo { \& my($self) = shift; \& if(@_) { # set \& return $self\->set(\*(Aqfoo\*(Aq, @_); \& } \& else { \& return $self\->get(\*(Aqfoo\*(Aq); \& } \& } .Ve .PP Very simple. All it does is determine if you're wanting to set a value or get a value and calls the appropriate method. Class::Accessor provides default \fIget()\fR and \fIset()\fR methods which your class can override. They're detailed later. .SS "Modifying the behavior of the accessor" .IX Subsection "Modifying the behavior of the accessor" Rather than actually modifying the accessor itself, it is much more sensible to simply override the two key methods which the accessor calls. Namely \fIset()\fR and \fIget()\fR. .PP If you \-really\- want to, you can override \fImake_accessor()\fR. .SS "set" .IX Subsection "set" .Vb 2 \& $obj\->set($key, $value); \& $obj\->set($key, @values); .Ve .PP \&\fIset()\fR defines how generally one stores data in the object. .PP override this method to change how data is stored by your accessors. .SS "get" .IX Subsection "get" .Vb 2 \& $value = $obj\->get($key); \& @values = $obj\->get(@keys); .Ve .PP \&\fIget()\fR defines how data is retreived from your objects. .PP override this method to change how it is retreived. .SS "make_accessor" .IX Subsection "make_accessor" .Vb 1 \& $accessor = _\|_PACKAGE_\|_\->make_accessor($field); .Ve .PP Generates a subroutine reference which acts as an accessor for the given \&\f(CW$field\fR. It calls \fIget()\fR and \fIset()\fR. .PP If you wish to change the behavior of your accessors, try overriding \&\fIget()\fR and \fIset()\fR before you start mucking with \fImake_accessor()\fR. .SS "make_ro_accessor" .IX Subsection "make_ro_accessor" .Vb 1 \& $read_only_accessor = _\|_PACKAGE_\|_\->make_ro_accessor($field); .Ve .PP Generates a subroutine refrence which acts as a read-only accessor for the given \f(CW$field\fR. It only calls \fIget()\fR. .PP Override \fIget()\fR to change the behavior of your accessors. .SS "make_wo_accessor" .IX Subsection "make_wo_accessor" .Vb 1 \& $read_only_accessor = _\|_PACKAGE_\|_\->make_wo_accessor($field); .Ve .PP Generates a subroutine refrence which acts as a write-only accessor (mutator) for the given \f(CW$field\fR. It only calls \fIset()\fR. .PP Override \fIset()\fR to change the behavior of your accessors. .SH "EXCEPTIONS" .IX Header "EXCEPTIONS" If something goes wrong Class::Accessor will warn or die by calling Carp::carp or Carp::croak. If you don't like this you can override \fI_carp()\fR and \fI_croak()\fR in your subclass and do whatever else you want. .SH "EFFICIENCY" .IX Header "EFFICIENCY" Class::Accessor does not employ an autoloader, thus it is much faster than you'd think. Its generated methods incur no special penalty over ones you'd write yourself. .PP .Vb 6 \& accessors: \& Rate Basic Fast Faster Direct \& Basic 367589/s \-\- \-51% \-55% \-89% \& Fast 747964/s 103% \-\- \-9% \-77% \& Faster 819199/s 123% 10% \-\- \-75% \& Direct 3245887/s 783% 334% 296% \-\- \& \& mutators: \& Rate Acc Fast Faster Direct \& Acc 265564/s \-\- \-54% \-63% \-91% \& Fast 573439/s 116% \-\- \-21% \-80% \& Faster 724710/s 173% 26% \-\- \-75% \& Direct 2860979/s 977% 399% 295% \-\- .Ve .PP Class::Accessor::Fast is faster than methods written by an average programmer (where \*(L"average\*(R" is based on Schwern's example code). .PP Class::Accessor is slower than average, but more flexible. .PP Class::Accessor::Faster is even faster than Class::Accessor::Fast. It uses an array internally, not a hash. This could be a good or bad feature depending on your point of view. .PP Direct hash access is, of course, much faster than all of these, but it provides no encapsulation. .PP Of course, it's not as simple as saying \*(L"Class::Accessor is slower than average\*(R". These are benchmarks for a simple accessor. If your accessors do any sort of complicated work (such as talking to a database or writing to a file) the time spent doing that work will quickly swamp the time spend just calling the accessor. In that case, Class::Accessor and the ones you write will be roughly the same speed. .SH "EXAMPLES" .IX Header "EXAMPLES" Here's an example of generating an accessor for every public field of your class. .PP .Vb 1 \& package Altoids; \& \& use base qw(Class::Accessor Class::Fields); \& use fields qw(curiously strong mints); \& Altoids\->mk_accessors( Altoids\->show_fields(\*(AqPublic\*(Aq) ); \& \& sub new { \& my $proto = shift; \& my $class = ref $proto || $proto; \& return fields::new($class); \& } \& \& my Altoids $tin = Altoids\->new; \& \& $tin\->curiously(\*(AqCuriouser and curiouser\*(Aq); \& print $tin\->{curiously}; # prints \*(AqCuriouser and curiouser\*(Aq \& \& \& # Subclassing works, too. \& package Mint::Snuff; \& use base qw(Altoids); \& \& my Mint::Snuff $pouch = Mint::Snuff\->new; \& $pouch\->strong(\*(AqBlow your head off!\*(Aq); \& print $pouch\->{strong}; # prints \*(AqBlow your head off!\*(Aq .Ve .PP Here's a simple example of altering the behavior of your accessors. .PP .Vb 3 \& package Foo; \& use base qw(Class::Accessor); \& Foo\->mk_accessors(qw(this that up down)); \& \& sub get { \& my $self = shift; \& \& # Note every time someone gets some data. \& print STDERR "Getting @_\en"; \& \& $self\->SUPER::get(@_); \& } \& \& sub set { \& my ($self, $key) = splice(@_, 0, 2); \& \& # Note every time someone sets some data. \& print STDERR "Setting $key to @_\en"; \& \& $self\->SUPER::set($key, @_); \& } .Ve .SH "CAVEATS AND TRICKS" .IX Header "CAVEATS AND TRICKS" Class::Accessor has to do some internal wackiness to get its job done quickly and efficiently. Because of this, there's a few tricks and traps one must know about. .PP Hey, nothing's perfect. .SS "Don't make a field called \s-1DESTROY\s0" .IX Subsection "Don't make a field called DESTROY" This is bad. Since \s-1DESTROY\s0 is a magical method it would be bad for us to define an accessor using that name. Class::Accessor will carp if you try to use it with a field named \*(L"\s-1DESTROY\*(R".\s0 .SS "Overriding autogenerated accessors" .IX Subsection "Overriding autogenerated accessors" You may want to override the autogenerated accessor with your own, yet have your custom accessor call the default one. For instance, maybe you want to have an accessor which checks its input. Normally, one would expect this to work: .PP .Vb 3 \& package Foo; \& use base qw(Class::Accessor); \& Foo\->mk_accessors(qw(email this that whatever)); \& \& # Only accept addresses which look valid. \& sub email { \& my($self) = shift; \& my($email) = @_; \& \& if( @_ ) { # Setting \& require Email::Valid; \& unless( Email::Valid\->address($email) ) { \& carp("$email doesn\*(Aqt look like a valid address."); \& return; \& } \& } \& \& return $self\->SUPER::email(@_); \& } .Ve .PP There's a subtle problem in the last example, and it's in this line: .PP .Vb 1 \& return $self\->SUPER::email(@_); .Ve .PP If we look at how Foo was defined, it called \fImk_accessors()\fR which stuck \fIemail()\fR right into Foo's namespace. There *is* no \&\fISUPER::email()\fR to delegate to! Two ways around this... first is to make a \*(L"pure\*(R" base class for Foo. This pure class will generate the accessors and provide the necessary super class for Foo to use: .PP .Vb 3 \& package Pure::Organic::Foo; \& use base qw(Class::Accessor); \& Pure::Organic::Foo\->mk_accessors(qw(email this that whatever)); \& \& package Foo; \& use base qw(Pure::Organic::Foo); .Ve .PP And now \fIFoo::email()\fR can override the generated \&\fIPure::Organic::Foo::email()\fR and use it as \fISUPER::email()\fR. .PP This is probably the most obvious solution to everyone but me. Instead, what first made sense to me was for \fImk_accessors()\fR to define an alias of \fIemail()\fR, \fI_email_accessor()\fR. Using this solution, \&\fIFoo::email()\fR would be written with: .PP .Vb 1 \& return $self\->_email_accessor(@_); .Ve .PP instead of the expected \fISUPER::email()\fR. .SH "AUTHORS" .IX Header "AUTHORS" Copyright 2009 Marty Pauley <marty+perl@kasei.com> .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. That means either (a) the \s-1GNU\s0 General Public License or (b) the Artistic License. .SS "\s-1ORIGINAL AUTHOR\s0" .IX Subsection "ORIGINAL AUTHOR" Michael G Schwern <schwern@pobox.com> .SS "\s-1THANKS\s0" .IX Subsection "THANKS" Liz and \s-1RUZ\s0 for performance tweaks. .PP Tels, for his big feature request/bug report. .PP Various presenters at YAPC::Asia 2009 for criticising the non-Moose interface. .SH "SEE ALSO" .IX Header "SEE ALSO" See Class::Accessor::Fast and Class::Accessor::Faster if speed is more important than flexibility. .PP These are some modules which do similar things in different ways Class::Struct, Class::Methodmaker, Class::Generate, Class::Class, Class::Contract, Moose, Mouse .PP See Class::DBI for an example of this module in use.