Mailing List
Perl 101
Perl 201

One of ScottWalters packages. Download off of http://wiki.slowass.net/?ScottWalters . From the POD:

=head1 NAME

this.pm - Syntactical Sugar for OO Methods


  use this;

  sub new {
    my $type = shift;
    my %args = @_;
    # must be blessed hash object
    bless { 
      foo => $args{'foo'},
      bar => $args{'bar'},
    }, $type;

  sub my_accessor {
    # $this is read for us. $bar is aliased to $this->{'bar'}, similiar for $foo

  sub anotheraccessor {
    # this will die if called from something not derived from our package    $foo++;


Methods in OO Perl receive "$this" without having to read it. Instance field variables are accessable by name instead of having to dereference the hash ref. Privicy is enforced for methods starting with an understore.


This emulates other OO languages, such as Java and C++, where the compiler implicitly and invisibly passes "this" as the first argument of each method call. While Perl passes this argument invisibly, you must manually write code to read it. Java and C++ also discover, at compile time, rather a variable is an instance variable or a static variable, without you needing to distinguish them using special syntax. We remove the extra syntax, but this is learned at run time, not compile time. Unlike Alias.pm, this code is likely to have a noticable impact on performance of code that uses OO accessors heavily.

this.pm places a thin wrapper is placed around methods in your object. *this{SCALAR} is a reference to a lexical we've shifted off the argument list. Aliases are created for each key in %$this to itself value in the same way.

While this module works fine for me, your milage may very: it has not been extensively tested.

This module was originally named "ImplicitThis.pm".

Similar to Alias.pm. However, we're pure Perl, and no additional syntax is introduced.
Blah blah blah.

=head2 EXPORT


=head1 BUGS

Does anyone know how to hand the CPU back after we finish loading, but take it again when whomever used us finishes loading? Doesn't work with strict on without doing "use vars" on each field member. Bumme r.

Not sure which version of perl is the minimum required.

May confuse highly introspective perl, like anything Damian Conway might write.

Does not work correctly when instance variables are tied: the alias doesn't curr ently take on the tiedness. In some cases, tie $alias, ref $this->{$alias} would do th e trick. This would fail on limited resources and when tie requires arguments.

=head1 SEE ALSO

Just a cheep knock-off of Alias.pm by Gurusamy Sarathy.

For more examples and documentation, as well as forums, Perl Design Patterns, se e: http://www.slowass.net/wiki/Just a cheep knock-off of Alias.pm by Gurusamy Sarathy.

For more examples and documentation, as well as forums, Perl Design Patterns, se e: http://www.slowass.net/wiki/

=head1 AUTHOR

Scott Walters "Root of all Evil" Escott@slowass.netE SWALTERS on CPAN


Copyright 2002 by Scott Walters

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Should you, through neglect or fault, speak the same phrase concurrently with another party, you realize that use of this software requires compliances with the rules governing the game of " jinx".

· Home Page · Edit This Page · Reverse Links · Expand Links · Recent Changes