Cari di Perl 
    Perl Tutorial
Daftar Isi
(Sebelumnya) Object OOPPerl DBM Filters (Berikutnya)
Language Reference

Perl Tie

Daftar Isi

NAME

perltie - how to hide an object class in a simple variable

SYNOPSIS

  1. tie VARIABLE, CLASSNAME, LIST
  2. $object = tied VARIABLE
  3. untie VARIABLE

DESCRIPTION

Prior to release 5.0 of Perl, a programmer could use dbmopen()to connect an on-disk database in the standard Unix dbm(3x)format magically to a %HASH in their program. However, their Perl was eitherbuilt with one particular dbm library or another, but not both, andyou couldn't extend this mechanism to other packages or types of variables.

Now you can.

The tie() function binds a variable to a class (package) that will providethe implementation for access methods for that variable. Once this magichas been performed, accessing a tied variable automatically triggersmethod calls in the proper class. The complexity of the class ishidden behind magic methods calls. The method names are in ALL CAPS,which is a convention that Perl uses to indicate that they're calledimplicitly rather than explicitly--just like the BEGIN() and END()functions.

In the tie() call, VARIABLE is the name of the variable to beenchanted. CLASSNAME is the name of a class implementing objects ofthe correct type. Any additional arguments in the LIST are passed tothe appropriate constructor method for that class--meaning TIESCALAR(),TIEARRAY(), TIEHASH(), or TIEHANDLE(). (Typically these are argumentssuch as might be passed to the dbminit() function of C.) The objectreturned by the "new" method is also returned by the tie() function,which would be useful if you wanted to access other methods inCLASSNAME. (You don't actually have to return a reference to a right"type" (e.g., HASH or CLASSNAME) so long as it's a properly blessedobject.) You can also retrieve a reference to the underlying objectusing the tied() function.

Unlike dbmopen(), the tie() function will not use or require a modulefor you--you need to do that explicitly yourself.

Tying Scalars

A class implementing a tied scalar should define the following methods:TIESCALAR, FETCH, STORE, and possibly UNTIE and/or DESTROY.

Let's look at each in turn, using as an example a tie class forscalars that allows the user to do something like:

  1. tie $his_speed, 'Nice', getppid();
  2. tie $my_speed, 'Nice', $$;

And now whenever either of those variables is accessed, its currentsystem priority is retrieved and returned. If those variables are set,then the process's priority is changed!

We'll use Jarkko Hietaniemi <[email protected]>'s BSD::Resource class (notincluded) to access the PRIO_PROCESS, PRIO_MIN, and PRIO_MAX constantsfrom your system, as well as the getpriority() and setpriority() systemcalls. Here's the preamble of the class.

  1. package Nice;
  2. use Carp;
  3. use BSD::Resource;
  4. use strict;
  5. $Nice::DEBUG = 0 unless defined $Nice::DEBUG;
  • TIESCALAR classname, LIST

    This is the constructor for the class. That means it isexpected to return a blessed reference to a new scalar(probably anonymous) that it's creating. For example:

    1. sub TIESCALAR {
    2. my $class = shift;
    3. my $pid = shift || $$; # 0 means me
    4. if ($pid !~ /^\d+$/) {
    5. carp "Nice::Tie::Scalar got non-numeric pid $pid" if $^W;
    6. return undef;
    7. }
    8. unless (kill 0, $pid) { # EPERM or ERSCH, no doubt
    9. carp "Nice::Tie::Scalar got bad pid $pid: $!" if $^W;
    10. return undef;
    11. }
    12. return bless \$pid, $class;
    13. }

    This tie class has chosen to return an error rather than raising anexception if its constructor should fail. While this is how dbmopen() works,other classes may well not wish to be so forgiving. It checks the globalvariable $^W to see whether to emit a bit of noise anyway.

  • FETCH this

    This method will be triggered every time the tied variable is accessed(read). It takes no arguments beyond its self reference, which is theobject representing the scalar we're dealing with. Because in this casewe're using just a SCALAR ref for the tied scalar object, a simple $$selfallows the method to get at the real value stored there. In our examplebelow, that real value is the process ID to which we've tied our variable.

    1. sub FETCH {
    2. my $self = shift;
    3. confess "wrong type" unless ref $self;
    4. croak "usage error" if @_;
    5. my $nicety;
    6. local($!) = 0;
    7. $nicety = getpriority(PRIO_PROCESS, $$self);
    8. if ($!) { croak "getpriority failed: $!" }
    9. return $nicety;
    10. }

    This time we've decided to blow up (raise an exception) if the renicefails--there's no place for us to return an error otherwise, and it'sprobably the right thing to do.

  • STORE this, value

    This method will be triggered every time the tied variable is set(assigned). Beyond its self reference, it also expects one (and only one)argument: the new value the user is trying to assign. Don't worry aboutreturning a value from STORE; the semantic of assignment returning theassigned value is implemented with FETCH.

    1. sub STORE {
    2. my $self = shift;
    3. confess "wrong type" unless ref $self;
    4. my $new_nicety = shift;
    5. croak "usage error" if @_;
    6. if ($new_nicety < PRIO_MIN) {
    7. carp sprintf
    8. "WARNING: priority %d less than minimum system priority %d",
    9. $new_nicety, PRIO_MIN if $^W;
    10. $new_nicety = PRIO_MIN;
    11. }
    12. if ($new_nicety > PRIO_MAX) {
    13. carp sprintf
    14. "WARNING: priority %d greater than maximum system priority %d",
    15. $new_nicety, PRIO_MAX if $^W;
    16. $new_nicety = PRIO_MAX;
    17. }
    18. unless (defined setpriority(PRIO_PROCESS, $$self, $new_nicety)) {
    19. confess "setpriority failed: $!";
    20. }
    21. }
  • UNTIE this

    This method will be triggered when the untie occurs. This can be usefulif the class needs to know when no further calls will be made. (Except DESTROYof course.) See The untie Gotcha below for more details.

  • DESTROY this

    This method will be triggered when the tied variable needs to be destructed.As with other object classes, such a method is seldom necessary, because Perldeallocates its moribund object's memory for you automatically--this isn'tC++, you know. We'll use a DESTROY method here for debugging purposes only.

    1. sub DESTROY {
    2. my $self = shift;
    3. confess "wrong type" unless ref $self;
    4. carp "[ Nice::DESTROY pid $$self ]" if $Nice::DEBUG;
    5. }

That's about all there is to it. Actually, it's more than all thereis to it, because we've done a few nice things here for the sakeof completeness, robustness, and general aesthetics. SimplerTIESCALAR classes are certainly possible.

Tying Arrays

A class implementing a tied ordinary array should define the followingmethods: TIEARRAY, FETCH, STORE, FETCHSIZE, STORESIZE and perhaps UNTIE and/or DESTROY.

FETCHSIZE and STORESIZE are used to provide $#array andequivalent scalar(@array) access.

The methods POP, PUSH, SHIFT, UNSHIFT, SPLICE, DELETE, and EXISTS arerequired if the perl operator with the corresponding (but lowercase) nameis to operate on the tied array. The Tie::Array class can be used as abase class to implement the first five of these in terms of the basicmethods above. The default implementations of DELETE and EXISTS inTie::Array simply croak.

In addition EXTEND will be called when perl would have pre-extendedallocation in a real array.

For this discussion, we'll implement an array whose elements are a fixedsize at creation. If you try to create an element larger than the fixedsize, you'll take an exception. For example:

  1. use FixedElem_Array;
  2. tie @array, 'FixedElem_Array', 3;
  3. $array[0] = 'cat'; # ok.
  4. $array[1] = 'dogs'; # exception, length('dogs') > 3.

The preamble code for the class is as follows:

  1. package FixedElem_Array;
  2. use Carp;
  3. use strict;
  • TIEARRAY classname, LIST

    This is the constructor for the class. That means it is expected toreturn a blessed reference through which the new array (probably ananonymous ARRAY ref) will be accessed.

    In our example, just to show you that you don't really have to return anARRAY reference, we'll choose a HASH reference to represent our object.A HASH works out well as a generic record type: the {ELEMSIZE} field willstore the maximum element size allowed, and the {ARRAY} field will hold thetrue ARRAY ref. If someone outside the class tries to dereference theobject returned (doubtless thinking it an ARRAY ref), they'll blow up.This just goes to show you that you should respect an object's privacy.

    1. sub TIEARRAY {
    2. my $class = shift;
    3. my $elemsize = shift;
    4. if ( @_ || $elemsize =~ /\D/ ) {
    5. croak "usage: tie ARRAY, '" . __PACKAGE__ . "', elem_size";
    6. }
    7. return bless {
    8. ELEMSIZE => $elemsize,
    9. ARRAY => [],
    10. }, $class;
    11. }
  • FETCH this, index

    This method will be triggered every time an individual element the tied arrayis accessed (read). It takes one argument beyond its self reference: theindex whose value we're trying to fetch.

    1. sub FETCH {
    2. my $self = shift;
    3. my $index = shift;
    4. return $self->{ARRAY}->[$index];
    5. }

    If a negative array index is used to read from an array, the indexwill be translated to a positive one internally by calling FETCHSIZEbefore being passed to FETCH. You may disable this feature byassigning a true value to the variable $NEGATIVE_INDICES in thetied array class.

    As you may have noticed, the name of the FETCH method (et al.) is the samefor all accesses, even though the constructors differ in names (TIESCALARvs TIEARRAY). While in theory you could have the same class servicingseveral tied types, in practice this becomes cumbersome, and it's easiestto keep them at simply one tie type per class.

  • STORE this, index, value

    This method will be triggered every time an element in the tied array is set(written). It takes two arguments beyond its self reference: the index atwhich we're trying to store something and the value we're trying to putthere.

    In our example, undef is really $self->{ELEMSIZE} number ofspaces so we have a little more work to do here:

    1. sub STORE {
    2. my $self = shift;
    3. my( $index, $value ) = @_;
    4. if ( length $value > $self->{ELEMSIZE} ) {
    5. croak "length of $value is greater than $self->{ELEMSIZE}";
    6. }
    7. # fill in the blanks
    8. $self->EXTEND( $index ) if $index > $self->FETCHSIZE();
    9. # right justify to keep element size for smaller elements
    10. $self->{ARRAY}->[$index] = sprintf "%$self->{ELEMSIZE}s", $value;
    11. }

    Negative indexes are treated the same as with FETCH.

  • FETCHSIZE this

    Returns the total number of items in the tied array associated withobject this. (Equivalent to scalar(@array)). For example:

    1. sub FETCHSIZE {
    2. my $self = shift;
    3. return scalar @{$self->{ARRAY}};
    4. }
  • STORESIZE this, count

    Sets the total number of items in the tied array associated withobject this to be count. If this makes the array larger thenclass's mapping of undef should be returned for new positions.If the array becomes smaller then entries beyond count should bedeleted.

    In our example, 'undef' is really an element containing$self->{ELEMSIZE} number of spaces. Observe:

    1. sub STORESIZE {
    2. my $self = shift;
    3. my $count = shift;
    4. if ( $count > $self->FETCHSIZE() ) {
    5. foreach ( $count - $self->FETCHSIZE() .. $count ) {
    6. $self->STORE( $_, '' );
    7. }
    8. } elsif ( $count < $self->FETCHSIZE() ) {
    9. foreach ( 0 .. $self->FETCHSIZE() - $count - 2 ) {
    10. $self->POP();
    11. }
    12. }
    13. }
  • EXTEND this, count

    Informative call that array is likely to grow to have count entries.Can be used to optimize allocation. This method need do nothing.

    In our example, we want to make sure there are no blank (undef)entries, so EXTEND will make use of STORESIZE to fill elementsas needed:

    1. sub EXTEND {
    2. my $self = shift;
    3. my $count = shift;
    4. $self->STORESIZE( $count );
    5. }
  • EXISTS this, key

    Verify that the element at index key exists in the tied array this.

    In our example, we will determine that if an element consists of$self->{ELEMSIZE} spaces only, it does not exist:

    1. sub EXISTS {
    2. my $self = shift;
    3. my $index = shift;
    4. return 0 if ! defined $self->{ARRAY}->[$index] ||
    5. $self->{ARRAY}->[$index] eq ' ' x $self->{ELEMSIZE};
    6. return 1;
    7. }
  • DELETE this, key

    Delete the element at index key from the tied array this.

    In our example, a deleted item is $self->{ELEMSIZE} spaces:

    1. sub DELETE {
    2. my $self = shift;
    3. my $index = shift;
    4. return $self->STORE( $index, '' );
    5. }
  • CLEAR this

    Clear (remove, delete, ...) all values from the tied array associated withobject this. For example:

    1. sub CLEAR {
    2. my $self = shift;
    3. return $self->{ARRAY} = [];
    4. }
  • PUSH this, LIST

    Append elements of LIST to the array. For example:

    1. sub PUSH {
    2. my $self = shift;
    3. my @list = @_;
    4. my $last = $self->FETCHSIZE();
    5. $self->STORE( $last + $_, $list[$_] ) foreach 0 .. $#list;
    6. return $self->FETCHSIZE();
    7. }
  • POP this

    Remove last element of the array and return it. For example:

    1. sub POP {
    2. my $self = shift;
    3. return pop @{$self->{ARRAY}};
    4. }
  • SHIFT this

    Remove the first element of the array (shifting other elements down)and return it. For example:

    1. sub SHIFT {
    2. my $self = shift;
    3. return shift @{$self->{ARRAY}};
    4. }
  • UNSHIFT this, LIST

    Insert LIST elements at the beginning of the array, moving existing elementsup to make room. For example:

    1. sub UNSHIFT {
    2. my $self = shift;
    3. my @list = @_;
    4. my $size = scalar( @list );
    5. # make room for our list
    6. @{$self->{ARRAY}}[ $size .. $#{$self->{ARRAY}} + $size ]
    7. = @{$self->{ARRAY}};
    8. $self->STORE( $_, $list[$_] ) foreach 0 .. $#list;
    9. }
  • SPLICE this, offset, length, LIST

    Perform the equivalent of splice on the array.

    offset is optional and defaults to zero, negative values count back from the end of the array.

    length is optional and defaults to rest of the array.

    LIST may be empty.

    Returns a list of the original length elements at offset.

    In our example, we'll use a little shortcut if there is a LIST:

    1. sub SPLICE {
    2. my $self = shift;
    3. my $offset = shift || 0;
    4. my $length = shift || $self->FETCHSIZE() - $offset;
    5. my @list = ();
    6. if ( @_ ) {
    7. tie @list, __PACKAGE__, $self->{ELEMSIZE};
    8. @list = @_;
    9. }
    10. return splice @{$self->{ARRAY}}, $offset, $length, @list;
    11. }
  • UNTIE this

    Will be called when untie happens. (See The untie Gotcha below.)

  • DESTROY this

    This method will be triggered when the tied variable needs to be destructed.As with the scalar tie class, this is almost never needed in alanguage that does its own garbage collection, so this time we'lljust leave it out.

Tying Hashes

Hashes were the first Perl data type to be tied (see dbmopen()). A classimplementing a tied hash should define the following methods: TIEHASH isthe constructor. FETCH and STORE access the key and value pairs. EXISTSreports whether a key is present in the hash, and DELETE deletes one.CLEAR empties the hash by deleting all the key and value pairs. FIRSTKEYand NEXTKEY implement the keys() and each() functions to iterate over allthe keys. SCALAR is triggered when the tied hash is evaluated in scalar context. UNTIE is called when untie happens, and DESTROY is called whenthe tied variable is garbage collected.

If this seems like a lot, then feel free to inherit from merely thestandard Tie::StdHash module for most of your methods, redefining only theinteresting ones. See Tie::Hash for details.

Remember that Perl distinguishes between a key not existing in the hash,and the key existing in the hash but having a corresponding value ofundef. The two possibilities can be tested with the exists() anddefined() functions.

Here's an example of a somewhat interesting tied hash class: it gives youa hash representing a particular user's dot files. You index into the hashwith the name of the file (minus the dot) and you get back that dot file'scontents. For example:

  1. use DotFiles;
  2. tie %dot, 'DotFiles';
  3. if ( $dot{profile} =~ /MANPATH/ ||
  4. $dot{login} =~ /MANPATH/ ||
  5. $dot{cshrc} =~ /MANPATH/ )
  6. {
  7. print "you seem to set your MANPATH\n";
  8. }

Or here's another sample of using our tied class:

  1. tie %him, 'DotFiles', 'daemon';
  2. foreach $f ( keys %him ) {
  3. printf "daemon dot file %s is size %d\n",
  4. $f, length $him{$f};
  5. }

In our tied hash DotFiles example, we use a regularhash for the object containing several importantfields, of which only the {LIST} field will be what theuser thinks of as the real hash.

  • USER

    whose dot files this object represents

  • HOME

    where those dot files live

  • CLOBBER

    whether we should try to change or remove those dot files

  • LIST

    the hash of dot file names and content mappings

Here's the start of Dotfiles.pm:

  1. package DotFiles;
  2. use Carp;
  3. sub whowasi { (caller(1))[3] . '()' }
  4. my $DEBUG = 0;
  5. sub debug { $DEBUG = @_ ? shift : 1 }

For our example, we want to be able to emit debugging info to help in tracingduring development. We keep also one convenience function aroundinternally to help print out warnings; whowasi() returns the function namethat calls it.

Here are the methods for the DotFiles tied hash.

  • TIEHASH classname, LIST

    This is the constructor for the class. That means it is expected toreturn a blessed reference through which the new object (probably but notnecessarily an anonymous hash) will be accessed.

    Here's the constructor:

    1. sub TIEHASH {
    2. my $self = shift;
    3. my $user = shift || $>;
    4. my $dotdir = shift || '';
    5. croak "usage: @{[&whowasi]} [USER [DOTDIR]]" if @_;
    6. $user = getpwuid($user) if $user =~ /^\d+$/;
    7. my $dir = (getpwnam($user))[7]
    8. || croak "@{[&whowasi]}: no user $user";
    9. $dir .= "/$dotdir" if $dotdir;
    10. my $node = {
    11. USER => $user,
    12. HOME => $dir,
    13. LIST => {},
    14. CLOBBER => 0,
    15. };
    16. opendir(DIR, $dir)
    17. || croak "@{[&whowasi]}: can't opendir $dir: $!";
    18. foreach $dot ( grep /^\./ && -f "$dir/$_", readdir(DIR)) {
    19. $dot =~ s/^\.//;
    20. $node->{LIST}{$dot} = undef;
    21. }
    22. closedir DIR;
    23. return bless $node, $self;
    24. }

    It's probably worth mentioning that if you're going to filetest thereturn values out of a readdir, you'd better prepend the directoryin question. Otherwise, because we didn't chdir() there, it wouldhave been testing the wrong file.

  • FETCH this, key

    This method will be triggered every time an element in the tied hash isaccessed (read). It takes one argument beyond its self reference: the keywhose value we're trying to fetch.

    Here's the fetch for our DotFiles example.

    1. sub FETCH {
    2. carp &whowasi if $DEBUG;
    3. my $self = shift;
    4. my $dot = shift;
    5. my $dir = $self->{HOME};
    6. my $file = "$dir/.$dot";
    7. unless (exists $self->{LIST}->{$dot} || -f $file) {
    8. carp "@{[&whowasi]}: no $dot file" if $DEBUG;
    9. return undef;
    10. }
    11. if (defined $self->{LIST}->{$dot}) {
    12. return $self->{LIST}->{$dot};
    13. } else {
    14. return $self->{LIST}->{$dot} = `cat $dir/.$dot`;
    15. }
    16. }

    It was easy to write by having it call the Unix cat(1) command, but itwould probably be more portable to open the file manually (and somewhatmore efficient). Of course, because dot files are a Unixy concept, we'renot that concerned.

  • STORE this, key, value

    This method will be triggered every time an element in the tied hash is set(written). It takes two arguments beyond its self reference: the index atwhich we're trying to store something, and the value we're trying to putthere.

    Here in our DotFiles example, we'll be careful not to letthem try to overwrite the file unless they've called the clobber()method on the original object reference returned by tie().

    1. sub STORE {
    2. carp &whowasi if $DEBUG;
    3. my $self = shift;
    4. my $dot = shift;
    5. my $value = shift;
    6. my $file = $self->{HOME} . "/.$dot";
    7. my $user = $self->{USER};
    8. croak "@{[&whowasi]}: $file not clobberable"
    9. unless $self->{CLOBBER};
    10. open(my $f, '>', $file) || croak "can't open $file: $!";
    11. print $f $value;
    12. close($f);
    13. }

    If they wanted to clobber something, they might say:

    1. $ob = tie %daemon_dots, 'daemon';
    2. $ob->clobber(1);
    3. $daemon_dots{signature} = "A true daemon\n";

    Another way to lay hands on a reference to the underlying object is touse the tied() function, so they might alternately have set clobberusing:

    1. tie %daemon_dots, 'daemon';
    2. tied(%daemon_dots)->clobber(1);

    The clobber method is simply:

    1. sub clobber {
    2. my $self = shift;
    3. $self->{CLOBBER} = @_ ? shift : 1;
    4. }
  • DELETE this, key

    This method is triggered when we remove an element from the hash,typically by using the delete() function. Again, we'llbe careful to check whether they really want to clobber files.

    1. sub DELETE {
    2. carp &whowasi if $DEBUG;
    3. my $self = shift;
    4. my $dot = shift;
    5. my $file = $self->{HOME} . "/.$dot";
    6. croak "@{[&whowasi]}: won't remove file $file"
    7. unless $self->{CLOBBER};
    8. delete $self->{LIST}->{$dot};
    9. my $success = unlink($file);
    10. carp "@{[&whowasi]}: can't unlink $file: $!" unless $success;
    11. $success;
    12. }

    The value returned by DELETE becomes the return value of the callto delete(). If you want to emulate the normal behavior of delete(),you should return whatever FETCH would have returned for this key.In this example, we have chosen instead to return a value which tellsthe caller whether the file was successfully deleted.

  • CLEAR this

    This method is triggered when the whole hash is to be cleared, usually byassigning the empty list to it.

    In our example, that would remove all the user's dot files! It's such adangerous thing that they'll have to set CLOBBER to something higher than1 to make it happen.

    1. sub CLEAR {
    2. carp &whowasi if $DEBUG;
    3. my $self = shift;
    4. croak "@{[&whowasi]}: won't remove all dot files for $self->{USER}"
    5. unless $self->{CLOBBER} > 1;
    6. my $dot;
    7. foreach $dot ( keys %{$self->{LIST}}) {
    8. $self->DELETE($dot);
    9. }
    10. }
  • EXISTS this, key

    This method is triggered when the user uses the exists() functionon a particular hash. In our example, we'll look at the {LIST}hash element for this:

    1. sub EXISTS {
    2. carp &whowasi if $DEBUG;
    3. my $self = shift;
    4. my $dot = shift;
    5. return exists $self->{LIST}->{$dot};
    6. }
  • FIRSTKEY this

    This method will be triggered when the user is goingto iterate through the hash, such as via a keys() or each()call.

    1. sub FIRSTKEY {
    2. carp &whowasi if $DEBUG;
    3. my $self = shift;
    4. my $a = keys %{$self->{LIST}};# reset each() iterator
    5. each %{$self->{LIST}}
    6. }
  • NEXTKEY this, lastkey

    This method gets triggered during a keys() or each() iteration. It has asecond argument which is the last key that had been accessed. This isuseful if you're carrying about ordering or calling the iterator from morethan one sequence, or not really storing things in a hash anywhere.

    For our example, we're using a real hash so we'll do just the simplething, but we'll have to go through the LIST field indirectly.

    1. sub NEXTKEY {
    2. carp &whowasi if $DEBUG;
    3. my $self = shift;
    4. return each %{ $self->{LIST} }
    5. }
  • SCALAR this

    This is called when the hash is evaluated in scalar context. In orderto mimic the behaviour of untied hashes, this method should return afalse value when the tied hash is considered empty. If this method doesnot exist, perl will make some educated guesses and return true whenthe hash is inside an iteration. If this isn't the case, FIRSTKEY iscalled, and the result will be a false value if FIRSTKEY returns the emptylist, true otherwise.

    However, you should not blindly rely on perl always doing the right thing. Particularly, perl will mistakenly return true when you clear the hash by repeatedly calling DELETE until it is empty. You are therefore advised to supply your own SCALAR method when you want to be absolutely sure that your hash behaves nicely in scalar context.

    In our example we can just call scalar on the underlying hashreferenced by $self->{LIST}:

    1. sub SCALAR {
    2. carp &whowasi if $DEBUG;
    3. my $self = shift;
    4. return scalar %{ $self->{LIST} }
    5. }
  • UNTIE this

    This is called when untie occurs. See The untie Gotcha below.

  • DESTROY this

    This method is triggered when a tied hash is about to go out ofscope. You don't really need it unless you're trying to add debuggingor have auxiliary state to clean up. Here's a very simple function:

    1. sub DESTROY {
    2. carp &whowasi if $DEBUG;
    3. }

Note that functions such as keys() and values() may return huge listswhen used on large objects, like DBM files. You may prefer to use theeach() function to iterate over such. Example:

  1. # print out history file offsets
  2. use NDBM_File;
  3. tie(%HIST, 'NDBM_File', '/usr/lib/news/history', 1, 0);
  4. while (($key,$val) = each %HIST) {
  5. print $key, ' = ', unpack('L',$val), "\n";
  6. }
  7. untie(%HIST);

Tying FileHandles

This is partially implemented now.

A class implementing a tied filehandle should define the followingmethods: TIEHANDLE, at least one of PRINT, PRINTF, WRITE, READLINE, GETC,READ, and possibly CLOSE, UNTIE and DESTROY. The class can also provide: BINMODE,OPEN, EOF, FILENO, SEEK, TELL - if the corresponding perl operators areused on the handle.

When STDERR is tied, its PRINT method will be called to issue warningsand error messages. This feature is temporarily disabled during the call, which means you can use warn() inside PRINT without starting a recursiveloop. And just like __WARN__ and __DIE__ handlers, STDERR's PRINTmethod may be called to report parser errors, so the caveats mentioned under %SIG in perlvar apply.

All of this is especially useful when perl is embedded in some other program, where output to STDOUT and STDERR may have to be redirected in some special way. See nvi and the Apache module for examples.

When tying a handle, the first argument to tie should begin with anasterisk. So, if you are tying STDOUT, use *STDOUT. If you haveassigned it to a scalar variable, say $handle, use *$handle.tie $handle ties the scalar variable $handle, not the handle insideit.

In our example we're going to create a shouting handle.

  1. package Shout;
  • TIEHANDLE classname, LIST

    This is the constructor for the class. That means it is expected toreturn a blessed reference of some sort. The reference can be used tohold some internal information.

    1. sub TIEHANDLE { print "<shout>\n"; my $i; bless \$i, shift }
  • WRITE this, LIST

    This method will be called when the handle is written to via thesyswrite function.

    1. sub WRITE {
    2. $r = shift;
    3. my($buf,$len,$offset) = @_;
    4. print "WRITE called, \$buf=$buf, \$len=$len, \$offset=$offset";
    5. }
  • PRINT this, LIST

    This method will be triggered every time the tied handle is printed towith the print() or say() functions. Beyond its self referenceit also expects the list that was passed to the print function.

    1. sub PRINT { $r = shift; $$r++; print join($,,map(uc($_),@_)),$\ }

    say() acts just like print() except $\ will be localized to \n soyou need do nothing special to handle say() in PRINT().

  • PRINTF this, LIST

    This method will be triggered every time the tied handle is printed towith the printf() function.Beyond its self reference it also expects the format and list that waspassed to the printf function.

    1. sub PRINTF {
    2. shift;
    3. my $fmt = shift;
    4. print sprintf($fmt, @_);
    5. }
  • READ this, LIST

    This method will be called when the handle is read from via the reador sysread functions.

    1. sub READ {
    2. my $self = shift;
    3. my $bufref = \$_[0];
    4. my(undef,$len,$offset) = @_;
    5. print "READ called, \$buf=$bufref, \$len=$len, \$offset=$offset";
    6. # add to $$bufref, set $len to number of characters read
    7. $len;
    8. }
  • READLINE this

    This method is called when the handle is read via <HANDLE>or readline HANDLE.

    As per readline, in scalar context it should returnthe next line, or undef for no more data. In list context it shouldreturn all remaining lines, or an empty list for no more data. The stringsreturned should include the input record separator $/ (see perlvar),unless it is undef (which means "slurp" mode).

    1. sub READLINE {
    2. my $r = shift;
    3. if (wantarray) {
    4. return ("all remaining\n",
    5. "lines up\n",
    6. "to eof\n");
    7. } else {
    8. return "READLINE called " . ++$$r . " times\n";
    9. }
    10. }
  • GETC this

    This method will be called when the getc function is called.

    1. sub GETC { print "Don't GETC, Get Perl"; return "a"; }
  • EOF this

    This method will be called when the eof function is called.

    Starting with Perl 5.12, an additional integer parameter will be passed. Itwill be zero if eof is called without parameter; 1 if eof is givena filehandle as a parameter, e.g. eof(FH); and 2 in the very specialcase that the tied filehandle is ARGV and eof is called with an emptyparameter list, e.g. eof().

    1. sub EOF { not length $stringbuf }
  • CLOSE this

    This method will be called when the handle is closed via the closefunction.

    1. sub CLOSE { print "CLOSE called.\n" }
  • UNTIE this

    As with the other types of ties, this method will be called when untie happens.It may be appropriate to "auto CLOSE" when this occurs. SeeThe untie Gotcha below.

  • DESTROY this

    As with the other types of ties, this method will be called when thetied handle is about to be destroyed. This is useful for debugging andpossibly cleaning up.

    1. sub DESTROY { print "</shout>\n" }

Here's how to use our little example:

  1. tie(*FOO,'Shout');
  2. print FOO "hello\n";
  3. $a = 4; $b = 6;
  4. print FOO $a, " plus ", $b, " equals ", $a + $b, "\n";
  5. print <FOO>;

UNTIE this

You can define for all tie types an UNTIE method that will be calledat untie(). See The untie Gotcha below.

The untie Gotcha

If you intend making use of the object returned from either tie() ortied(), and if the tie's target class defines a destructor, there is asubtle gotcha you must guard against.

As setup, consider this (admittedly rather contrived) example of atie; all it does is use a file to keep a log of the values assigned toa scalar.

  1. package Remember;
  2. use strict;
  3. use warnings;
  4. use IO::File;
  5. sub TIESCALAR {
  6. my $class = shift;
  7. my $filename = shift;
  8. my $handle = IO::File->new( "> $filename" )
  9. or die "Cannot open $filename: $!\n";
  10. print $handle "The Start\n";
  11. bless {FH => $handle, Value => 0}, $class;
  12. }
  13. sub FETCH {
  14. my $self = shift;
  15. return $self->{Value};
  16. }
  17. sub STORE {
  18. my $self = shift;
  19. my $value = shift;
  20. my $handle = $self->{FH};
  21. print $handle "$value\n";
  22. $self->{Value} = $value;
  23. }
  24. sub DESTROY {
  25. my $self = shift;
  26. my $handle = $self->{FH};
  27. print $handle "The End\n";
  28. close $handle;
  29. }
  30. 1;

Here is an example that makes use of this tie:

  1. use strict;
  2. use Remember;
  3. my $fred;
  4. tie $fred, 'Remember', 'myfile.txt';
  5. $fred = 1;
  6. $fred = 4;
  7. $fred = 5;
  8. untie $fred;
  9. system "cat myfile.txt";

This is the output when it is executed:

  1. The Start
  2. 1
  3. 4
  4. 5
  5. The End

So far so good. Those of you who have been paying attention will havespotted that the tied object hasn't been used so far. So lets add anextra method to the Remember class to allow comments to be included inthe file; say, something like this:

  1. sub comment {
  2. my $self = shift;
  3. my $text = shift;
  4. my $handle = $self->{FH};
  5. print $handle $text, "\n";
  6. }

And here is the previous example modified to use the comment method(which requires the tied object):

  1. use strict;
  2. use Remember;
  3. my ($fred, $x);
  4. $x = tie $fred, 'Remember', 'myfile.txt';
  5. $fred = 1;
  6. $fred = 4;
  7. comment $x "changing...";
  8. $fred = 5;
  9. untie $fred;
  10. system "cat myfile.txt";

When this code is executed there is no output. Here's why:

When a variable is tied, it is associated with the object which is thereturn value of the TIESCALAR, TIEARRAY, or TIEHASH function. Thisobject normally has only one reference, namely, the implicit referencefrom the tied variable. When untie() is called, that reference isdestroyed. Then, as in the first example above, the object'sdestructor (DESTROY) is called, which is normal for objects that haveno more valid references; and thus the file is closed.

In the second example, however, we have stored another reference tothe tied object in $x. That means that when untie() gets calledthere will still be a valid reference to the object in existence, sothe destructor is not called at that time, and thus the file is notclosed. The reason there is no output is because the file buffershave not been flushed to disk.

Now that you know what the problem is, what can you do to avoid it?Prior to the introduction of the optional UNTIE method the only waywas the good old -w flag. Which will spot any instances where you calluntie() and there are still valid references to the tied object. Ifthe second script above this near the top use warnings 'untie'or was run with the -w flag, Perl prints thiswarning message:

  1. untie attempted while 1 inner references still exist

To get the script to work properly and silence the warning make surethere are no valid references to the tied object before untie() iscalled:

  1. undef $x;
  2. untie $fred;

Now that UNTIE exists the class designer can decide which parts of theclass functionality are really associated with untie and which withthe object being destroyed. What makes sense for a given class dependson whether the inner references are being kept so that non-tie-relatedmethods can be called on the object. But in most cases it probably makessense to move the functionality that would have been in DESTROY to the UNTIEmethod.

If the UNTIE method exists then the warning above does not occur. Instead theUNTIE method is passed the count of "extra" references and can issue its ownwarning if appropriate. e.g. to replicate the no UNTIE case this method canbe used:

  1. sub UNTIE
  2. {
  3. my ($obj,$count) = @_;
  4. carp "untie attempted while $count inner references still exist" if $count;
  5. }

SEE ALSO

See DB_File or Config for some interesting tie() implementations.A good starting point for many tie() implementations is with one of themodules Tie::Scalar, Tie::Array, Tie::Hash, or Tie::Handle.

BUGS

The bucket usage information provided by scalar(%hash) is notavailable. What this means is that using %tied_hash in booleancontext doesn't work right (currently this always tests false,regardless of whether the hash is empty or hash elements).

Localizing tied arrays or hashes does not work. After exiting thescope the arrays or the hashes are not restored.

Counting the number of entries in a hash via scalar(keys(%hash))or scalar(values(%hash)) is inefficient since it needs to iteratethrough all the entries with FIRSTKEY/NEXTKEY.

Tied hash/array slices cause multiple FETCH/STORE pairs, there are notie methods for slice operations.

You cannot easily tie a multilevel data structure (such as a hash ofhashes) to a dbm file. The first problem is that all but GDBM andBerkeley DB have size limitations, but beyond that, you also have problemswith how references are to be represented on disk. Onemodule that does attempt to address this need is DBM::Deep. Check yournearest CPAN site as described in perlmodlib for source code. Notethat despite its name, DBM::Deep does not use dbm. Another earlier attemptat solving the problem is MLDBM, which is also available on the CPAN, butwhich has some fairly serious limitations.

Tied filehandles are still incomplete. sysopen(), truncate(),flock(), fcntl(), stat() and -X can't currently be trapped.

AUTHOR

Tom Christiansen

TIEHANDLE by Sven Verdoolaege <[email protected]> and Doug MacEachern <[email protected]>

UNTIE by Nick Ing-Simmons <[email protected]>

SCALAR by Tassilo von Parseval <[email protected]>

Tying Arrays by Casey West <[email protected]>

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Object OOPPerl DBM Filters (Berikutnya)