NAME

self - provides '$self' in OO code.

VERSION

This document describes self version 0.30.

SYNOPSIS

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;
}

DESCRIPTION

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.

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%    --

INTERFACE

$self, or self

Return the current object.

@args, or args

Return the argument list.

CONFIGURATION AND ENVIRONMENT

self.pm requires no configuration files or environment variables.

DEPENDENCIES

B::OPCheck, B::Hooks::Parser, Sub::Exporter

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

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 not 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.

AUTHOR

Kang-min Liu <gugod@gugod.org>

LICENCE AND COPYRIGHT

Copyright (c) 2021 Kang-min Liu <gugod@gugod.org>.

This is free software, licensed under:

The MIT (X11) License

DISCLAIMER OF WARRANTY

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.