Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Find out the type of thing bei ...Get a reference to the object ... (Berikutnya)
Keywords related to classes and object-orientedness

Bind a variable to an object class

Daftar Isi

  • tie VARIABLE,CLASSNAME,LIST

    This function binds a variable to a package class that will provide theimplementation for the variable. VARIABLE is the name of the variableto be enchanted. CLASSNAME is the name of a class implementing objectsof correct type. Any additional arguments are passed to the newmethod of the class (meaning TIESCALAR, TIEHANDLE, TIEARRAY,or TIEHASH). Typically these are arguments such as might be passedto the dbm_open() function of C. The object returned by the newmethod is also returned by the tie function, which would be usefulif you want to access other methods in CLASSNAME.

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

    A class implementing a hash should have the following methods:

    1. TIEHASH classname, LIST
    2. FETCH this, key
    3. STORE this, key, value
    4. DELETE this, key
    5. CLEAR this
    6. EXISTS this, key
    7. FIRSTKEY this
    8. NEXTKEY this, lastkey
    9. SCALAR this
    10. DESTROY this
    11. UNTIE this

    A class implementing an ordinary array should have the following methods:

    1. TIEARRAY classname, LIST
    2. FETCH this, key
    3. STORE this, key, value
    4. FETCHSIZE this
    5. STORESIZE this, count
    6. CLEAR this
    7. PUSH this, LIST
    8. POP this
    9. SHIFT this
    10. UNSHIFT this, LIST
    11. SPLICE this, offset, length, LIST
    12. EXTEND this, count
    13. DESTROY this
    14. UNTIE this

    A class implementing a filehandle should have the following methods:

    1. TIEHANDLE classname, LIST
    2. READ this, scalar, length, offset
    3. READLINE this
    4. GETC this
    5. WRITE this, scalar, length, offset
    6. PRINT this, LIST
    7. PRINTF this, format, LIST
    8. BINMODE this
    9. EOF this
    10. FILENO this
    11. SEEK this, position, whence
    12. TELL this
    13. OPEN this, mode, LIST
    14. CLOSE this
    15. DESTROY this
    16. UNTIE this

    A class implementing a scalar should have the following methods:

    1. TIESCALAR classname, LIST
    2. FETCH this,
    3. STORE this, value
    4. DESTROY this
    5. UNTIE this

    Not all methods indicated above need be implemented. See perltie,Tie::Hash, Tie::Array, Tie::Scalar, and Tie::Handle.

    Unlike dbmopen, the tie function will not use or require a modulefor you; you need to do that explicitly yourself. See DB_Fileor the Config module for interesting tie implementations.

    For further details see perltie, tied VARIABLE.

 
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) Find out the type of thing bei ...Get a reference to the object ... (Berikutnya)