-
-
02 Feb 2008 10:21:05 UTC
- Distribution: Tie-OneOff
- Module version: 1.03
- Source (raw)
- Browse (raw)
- Changes
- How to Contribute
- Issues
- Testers (615 / 0 / 2)
- Kwalitee
Bus factor: 0- 75.61% Coverage
- License: unknown
- Activity
24 month- Tools
- Download (3.96KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
- Dependencies
- Scalar::Util
- Test::Simple
- and possibly others
- Reverse dependencies
- CPAN Testers List
- Dependency graph
Why not adopt me?
This distribution is up for adoption! If you're interested then please contact the PAUSE module admins via email.NAME
Tie::OneOff - create tied variables without defining a separate package
SYNOPSIS
require Tie::OneOff; tie my %REV, 'Tie::OneOff' => sub { reverse shift; }; print "$REV{olleH}\n"; # Hello sub make_counter { my $step = shift; my $i = 0; Tie::OneOff->scalar({ BASE => \$i, # Implies: STORE => sub { $i = shift } FETCH => sub { $i += $step }, }); } my $c1 = make_counter(1); my $c2 = make_counter(2); $$c2 = 10; print "$$c1 $$c2 $$c2 $$c2 $$c1 $$c1\n"; # 1 12 14 16 2 3 sub foo : lvalue { +Tie::OneOff->lvalue({ STORE => sub { print "foo()=$_[0]\n" }, FETCH => sub { "wibble" }, }); } foo='wobble'; # foo()=wobble print "foo()=", foo, "\n"; # foo()=wibble
DESCRIPTION
The Perl tie mechanism ties a Perl variable to a Perl object. This means that, conventionally, for each distinct set of tied variable semantics one needs to create a new package. The package symbol table then acts as a dispatch table for the intrinsic actions (such as
FETCH
,STORE
,FETCHSIZE
) that can be performed on Perl variables.Sometimes it would seem more natural to associate a dispatch table hash directly with the variable and pretend as if the intermediate object did not exist. This is what
Tie::OneOff
does.It is important to note that in this model there is no object to hold the instance data for the tied variable. The callbacks in the dispatch table are called not as object methods but as simple subroutines. If there is to be any instance information for a variable tied using
Tie::OneOff
it must be in lexical variables that are referenced by the callback closures.Tie::OneOff
does not itself provide any default callbacks. This can make defining a full featured hash interface rather tedious. To simplify matters the elementBASE
in the dispatch table can be used to specify a "base object" whose methods provide the default callbacks. If a reference to an unblessed Perl variable is specified as theBASE
then the variable is blessed into the appropriateTie::StdXXXX
package. In this case the unblessed variable used as the base must, of course, be of the same type as the variable that is being tied.In
make_counter()
in the synopsis above, the variable$i
gets blessed intoTie::StdScalar
. Since there is no explict STORE in the dispatch table, an attempt to store into a counter is implemented by calling(\$i)->STORE(@_)
which in turn is resolved asTie::StdScalar::STORE(\$i,@_)
which in turn is equivalent to$i=shift
.Since many tied variables need only a
FETCH
methodTie::OneOff
ties can also be specified by giving a simple code reference that is taken to be the variable'sFETCH
callback.For convience the class methods
scalar
,hash
andarray
take the same arguments as the tie inferface and return a reference to an anonymous tied variable. The class methodlvalue
is likescalar
but returns an lvalue rather than a reference.Relationship to other modules
This module's original working title was Tie::Simple however it was eventually released as Tie::OneOff. Some time later another, substancially identical, module was developed independantly and released as Tie::Simple.
This module can be used as a trick to make functions that interpolate into strings but if that's all you want you may want to use Interpolation instead.
XXX Want XXX
SEE ALSO
perltie, Tie::Scalar, Tie::Hash, Tie::Array, Interpolation, Tie::Simple.
Module Install Instructions
To install Tie::OneOff, copy and paste the appropriate command in to your terminal.
cpanm Tie::OneOff
perl -MCPAN -e shell install Tie::OneOff
For more information on module installation, please visit the detailed CPAN module installation guide.