GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
self(3) User Contributed Perl Documentation self(3)
 

self - provides '$self' in OO code.

This document describes self version 0.30.

    package MyModule;
    use self;
    # Write constructor as usual
    sub new {
        return bless({}, shift);
    }
    # '$self' is special now.
    sub foo {
        $self->{foo}
    }
    # '@args' too
    sub set {
        my ($foo, $bar) = @args;
        $self->{foo} = $foo;
        $self->{bar} = $bar;
    }

This module adds $self and @args variables in your code. So you don't need to say:
    my $self = shift;
The provided $self and @args are lexicals in your sub, and it's always the same as saying:
    my ($self, @args) = @_;
... in the first line of sub.
However it is not source filtering, but compile-time code injection. For more info about code injection, see B::Hooks::Parser or Devel::Declare.
It also exports a "self" and a "args" functions. Basically "self" is just equal to $_[0], and "args" is just $_[1..$#_].
For convienence (and backward compatibility), these two functions are exported by default. If you don't want them to be exported, you need to say:
    use self ();
Since self.pm uses Sub::Exporter, the exported <self> funciton can be renamed:
    use self self => { -as => 'this' };
For more information, see Sub::Exporter.
It is recommended to use variables instead, because it's much much faster. There's a benchmark program under "example" directory compare them: Here's one example run:
    > perl -Ilib examples/benchmark.pl
              Rate  self $self
    self   46598/s    --  -92%
    $self 568182/s 1119%    --

$self, or self
Return the current object.
@args, or args
Return the argument list.

self.pm requires no configuration files or environment variables.

"B::OPCheck", "Devel::Declare", "Sub::Exporter"

None reported.

In some cases, $self and @args may failed to be injected.
If you're using 0.30, please ensure that your sub declaration has its '{' at the same line like this:
    sub foo {
    }
Also it's ok to have the entire sub in one line:
    sub foo { }
Please upgrade to 0.31 if you prefer this style of code:
   sub foo
   {
       $self;
   }
Extra spaces around sub declarations are handled as much as possible, if you found any cases that it failed to work, please send me bug reports with your test cases.
It does it work on methods generated in runtime. Remember, it's a compile-time code injection. For those cases, use "self" function instead.
Please report any bugs or feature requests to "bug-self@rt.cpan.org", or through the web interface at <http://rt.cpan.org>.

Kang-min Liu "<gugod@gugod.org>"

Copyright (c) 2007, 2008, 2009, Kang-min Liu "<gugod@gugod.org>".
This is free software, licensed under:
    The MIT (X11) License

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
2016-02-01 perl v5.28.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.