<?xml version="1.0" encoding="windows-1252"?>
<node id="255" title="perlfunc:tie" created="1999-08-24 18:42:35" updated="2005-08-15 07:37:39">
<type id="119">
perlfunc</type>
<author id="114">
gods</author>
<data>
<field name="doctext">
</field>
<field name="name">
&lt;P&gt;
tie - bind a variable to an object class 

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="synopsis">
&lt;P&gt;
tie 
&lt;FONT SIZE=-1&gt;VARIABLE,CLASSNAME,LIST&lt;/FONT&gt;

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="description">
&lt;P&gt;
This function binds a variable to a package class that will provide the implementation for the variable. 
&lt;FONT SIZE=-1&gt;VARIABLE&lt;/FONT&gt; is the name of the variable to be enchanted. 
&lt;FONT SIZE=-1&gt;CLASSNAME&lt;/FONT&gt; is the name of a class implementing objects of correct type. Any additional arguments are passed to the ``
&lt;CODE&gt;new()&lt;/CODE&gt;'' method of the class (meaning &lt;CODE&gt;TIESCALAR&lt;/CODE&gt;, &lt;CODE&gt;TIEARRAY&lt;/CODE&gt;, or &lt;CODE&gt;TIEHASH&lt;/CODE&gt;). Typically these are arguments such as might be passed to the &lt;CODE&gt;dbm_open()&lt;/CODE&gt;
function of 
&lt;FONT SIZE=-1&gt;C.&lt;/FONT&gt; The object returned by the ``&lt;CODE&gt;new()&lt;/CODE&gt;'' method is also returned by the [perlfunc:tie|tie()] function, which would be useful if you want to access other methods in 
&lt;FONT SIZE=-1&gt;CLASSNAME.&lt;/FONT&gt;

&lt;P&gt;
Note that functions such as [perlfunc:keys|keys()] and [perlfunc:values|values()] may return huge lists when used on large objects, like 
&lt;FONT SIZE=-1&gt;DBM&lt;/FONT&gt; files. You may prefer to use the
[perlfunc:each|each()] function to iterate over such. Example:

&lt;P&gt;
&lt;PRE&gt;    # print out history file offsets
    use NDBM_File;
    tie(%HIST, 'NDBM_File', '/usr/lib/news/history', 1, 0);
    while (($key,$val) = each %HIST) {
        print $key, ' = ', unpack('L',$val), &amp;quot;\n&amp;quot;;
    }
    untie(%HIST);
&lt;/PRE&gt;
&lt;P&gt;

&lt;FONT SIZE=-1&gt;A&lt;/FONT&gt; class implementing a hash should have the following
methods:

&lt;P&gt;
&lt;PRE&gt;    TIEHASH classname, LIST
    DESTROY this
    FETCH this, key
    STORE this, key, value
    DELETE this, key
    EXISTS this, key
    FIRSTKEY this
    NEXTKEY this, lastkey
&lt;/PRE&gt;
&lt;P&gt;

&lt;FONT SIZE=-1&gt;A&lt;/FONT&gt; class implementing an ordinary array should have the
following methods:

&lt;P&gt;
&lt;PRE&gt;    TIEARRAY classname, LIST
    DESTROY this
    FETCH this, key
    STORE this, key, value
    &amp;#091;others TBD&amp;#093;
&lt;/PRE&gt;
&lt;P&gt;

&lt;FONT SIZE=-1&gt;A&lt;/FONT&gt; class implementing a scalar should have the
following methods:

&lt;P&gt;
&lt;PRE&gt;    TIESCALAR classname, LIST
    DESTROY this
    FETCH this,
    STORE this, value
&lt;/PRE&gt;
&lt;P&gt;
Unlike [perlfunc:dbmopen|dbmopen()], the [perlfunc:tie|tie()] function will not use or require a module for you--you need to do that
explicitly yourself. See [perlfunc:lib:DB_File|the DB_File manpage]
or the &lt;EM&gt;Config&lt;/EM&gt; module for interesting [perlfunc:tie|tie()] implementations.

&lt;P&gt;
For further details see [perlman:perltie|the perltie manpage], &lt;U&gt;tied VARIABLE&lt;/U&gt;.

&lt;HR&gt;
</field>
</data>
</node>
