Object::Lexical

Syntactic Sugar for Easy Object Instance Data & More
Download

Object::Lexical Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Scott Walters
  • Publisher web site:
  • http://search.cpan.org/~swalters/Math-Preference-SVD-0.01/lib/Math/Preference/SVD.pm

Object::Lexical Tags


Object::Lexical Description

Syntactic Sugar for Easy Object Instance Data & More Object::Lexical is a Perl module that provides syntactic sugar to create objects.Normal my variables are used for instance data. $this is automatically read off of the argument stack. This follows "real" OO languages, where user code need not concern itself with helping the language implement objects.Normal OO Perl code is ugly, hard to read, tedious to type, and error prone. The $self-{field}> syntax is cumbersome, and using an object field with a built in, like push(), requires syntax beyond novice Perl programmers: push @{$self-{field}}, $value>. Spelling field names wrong results in hard to find bugs: the hash autovivicates, and no "variables must be declared" warning is issued.instance() returns a new object that subclasses the current object, and contains all of the just-defined methods. The object returned is a blessed symbol table (stash) reference, which functions like a blessed hash reference for most purposes. In other words, it is a normal object.instance() takes an optional argument: the name of the package the object being created is to belong to. If the new() method reads the class name off of the argument stack, this class name should be passed to instance(), to support the creation of subclasses of your class. This is similar to the operation of bless(), except instance() will read the class name off of the stack for you if you don't.The use Method::Lexical line takes optional arguments: "nononlex" specifies that non-lexically defined methods shouldn't be moved. Methods defined using *name = sub { } and sub name { } won't be moved. If subroutines are created out side of the sub new { } block, then this option should be specified, or else the subroutines will mysteriously disappear. "noexport" specifies that method() and instance() should not be exported into your namespace. To get at these functions, you will need to qualify their names: Object::Lexical::method() and Object::Lexical::instance(), respectively. "nowrap" specifies that methods should be wrapped in logic that reads $this automatically, as they are moved into their new symbol table. If you want to refer to $this as $_, or you want to process it yourself, or you want keep memory usage on par with normal objects, use this.instance() is the heart of this module: lexically scoped methods (coderefs held in my variables) and methods placed into the symbol table are moved into a new namespace created just for that object instance. A thin wrapper is placed around each symbol table entry in this namespace that reads the reference to the current object into an our variable named $this.Any number of independent objects can be returned by new(). By defining methods in side the block of the new() method, each returned object has its own private copies of each my variable. This uses the "lambda closure" feature of Perl. A closure is code that holds references to variables - in this example, $counter will go out of scope, but inc, dec, inc3x all keep a reference to it. The next time new() is run, a new $counter lexical will be created, and new methods will be created that reference that.This serves to avoid the messy $this-{counter}++> syntax, making it easier to refactor code, move code into methods from subroutines, and turn plain old modules into objects.SYNOPSIS use Object::Lexical; use Sub::Lexical; sub new { my $counter; our $this; my sub inc { $counter++; } my sub dec { $counter--; } my sub inc3x { $this->inc() for(1..3); } instance(); } Requirements: · Perl


Object::Lexical Related Software