home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / u_man / cat1 / perlobj.z / perlobj
Encoding:
Text File  |  2002-10-03  |  26.7 KB  |  727 lines

  1.  
  2.  
  3.  
  4. PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))                                                          PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      perlobj - Perl objects
  10.  
  11. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.      First of all, you need to understand what references are in Perl.  See
  13.      the _p_e_r_l_r_e_f manpage for that.  Second, if you still find the following
  14.      reference work too complicated, a tutorial on object-oriented programming
  15.      in Perl can be found in the _p_e_r_l_t_o_o_t manpage.
  16.  
  17.      If you're still with us, then here are three very simple definitions that
  18.      you should find reassuring.
  19.  
  20.      1.  An object is simply a reference that happens to know which class it
  21.          belongs to.
  22.  
  23.      2.  A class is simply a package that happens to provide methods to deal
  24.          with object references.
  25.  
  26.      3.  A method is simply a subroutine that expects an object reference (or
  27.          a package name, for class methods) as the first argument.
  28.  
  29.      We'll cover these points now in more depth.
  30.  
  31.      AAAAnnnn OOOObbbbjjjjeeeecccctttt iiiissss SSSSiiiimmmmppppllllyyyy aaaa RRRReeeeffffeeeerrrreeeennnncccceeee
  32.  
  33.      Unlike say C++, Perl doesn't provide any special syntax for constructors.
  34.      A constructor is merely a subroutine that returns a reference to
  35.      something "blessed" into a class, generally the class that the subroutine
  36.      is defined in.  Here is a typical constructor:
  37.  
  38.          package Critter;
  39.          sub new { bless {} }
  40.  
  41.      That word new isn't special.  You could have written a construct this
  42.      way, too:
  43.  
  44.          package Critter;
  45.          sub spawn { bless {} }
  46.  
  47.      In fact, this might even be preferable, because the C++ programmers won't
  48.      be tricked into thinking that new works in Perl as it does in C++.  It
  49.      doesn't.  We recommend that you name your constructors whatever makes
  50.      sense in the context of the problem you're solving.  For example,
  51.      constructors in the Tk extension to Perl are named after the widgets they
  52.      create.
  53.  
  54.      One thing that's different about Perl constructors compared with those in
  55.      C++ is that in Perl, they have to allocate their own memory.  (The other
  56.      things is that they don't automatically call overridden base-class
  57.      constructors.)  The {} allocates an anonymous hash containing no
  58.      key/value pairs, and returns it  The _b_l_e_s_s() takes that reference and
  59.      tells the object it references that it's now a Critter, and returns the
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))                                                          PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  71.  
  72.  
  73.  
  74.      reference.  This is for convenience, because the referenced object itself
  75.      knows that it has been blessed, and the reference to it could have been
  76.      returned directly, like this:
  77.  
  78.          sub new {
  79.              my $self = {};
  80.              bless $self;
  81.              return $self;
  82.          }
  83.  
  84.      In fact, you often see such a thing in more complicated constructors that
  85.      wish to call methods in the class as part of the construction:
  86.  
  87.          sub new {
  88.              my $self = {};
  89.              bless $self;
  90.              $self->initialize();
  91.              return $self;
  92.          }
  93.  
  94.      If you care about inheritance (and you should; see the section on
  95.      _M_o_d_u_l_e_s: _C_r_e_a_t_i_o_n, _U_s_e, _a_n_d _A_b_u_s_e in the _p_e_r_l_m_o_d_l_i_b manpage), then you
  96.      want to use the two-arg form of bless so that your constructors may be
  97.      inherited:
  98.  
  99.          sub new {
  100.              my $class = shift;
  101.              my $self = {};
  102.              bless $self, $class;
  103.              $self->initialize();
  104.              return $self;
  105.          }
  106.  
  107.      Or if you expect people to call not just CLASS->new() but also $obj-
  108.      >new(), then use something like this.  The _i_n_i_t_i_a_l_i_z_e() method used will
  109.      be of whatever $class we blessed the object into:
  110.  
  111.          sub new {
  112.              my $this = shift;
  113.              my $class = ref($this) || $this;
  114.              my $self = {};
  115.              bless $self, $class;
  116.              $self->initialize();
  117.              return $self;
  118.          }
  119.  
  120.      Within the class package, the methods will typically deal with the
  121.      reference as an ordinary reference.  Outside the class package, the
  122.      reference is generally treated as an opaque value that may be accessed
  123.      only through the class's methods.
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))                                                          PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  137.  
  138.  
  139.  
  140.      A constructor may re-bless a referenced object currently belonging to
  141.      another class, but then the new class is responsible for all cleanup
  142.      later.  The previous blessing is forgotten, as an object may belong to
  143.      only one class at a time.  (Although of course it's free to inherit
  144.      methods from many classes.)  If you find yourself having to do this, the
  145.      parent class is probably misbehaving, though.
  146.  
  147.      A clarification:  Perl objects are blessed.  References are not.  Objects
  148.      know which package they belong to.  References do not.  The _b_l_e_s_s()
  149.      function uses the reference to find the object.  Consider the following
  150.      example:
  151.  
  152.          $a = {};
  153.          $b = $a;
  154.          bless $a, BLAH;
  155.          print "\$b is a ", ref($b), "\n";
  156.  
  157.      This reports $b as being a BLAH, so obviously _b_l_e_s_s() operated on the
  158.      object and not on the reference.
  159.  
  160.      AAAA CCCCllllaaaassssssss iiiissss SSSSiiiimmmmppppllllyyyy aaaa PPPPaaaacccckkkkaaaaggggeeee
  161.  
  162.      Unlike say C++, Perl doesn't provide any special syntax for class
  163.      definitions.  You use a package as a class by putting method definitions
  164.      into the class.
  165.  
  166.      There is a special array within each package called @ISA, which says
  167.      where else to look for a method if you can't find it in the current
  168.      package.  This is how Perl implements inheritance.  Each element of the
  169.      @ISA array is just the name of another package that happens to be a class
  170.      package.  The classes are searched (depth first) for missing methods in
  171.      the order that they occur in @ISA.  The classes accessible through @ISA
  172.      are known as base classes of the current class.
  173.  
  174.      All classes implicitly inherit from class UNIVERSAL as their last base
  175.      class.  Several commonly used methods are automatically supplied in the
  176.      UNIVERSAL class; see the section on _D_e_f_a_u_l_t _U_N_I_V_E_R_S_A_L _m_e_t_h_o_d_s for more
  177.      details.
  178.  
  179.      If a missing method is found in one of the base classes, it is cached in
  180.      the current class for efficiency.  Changing @ISA or defining new
  181.      subroutines invalidates the cache and causes Perl to do the lookup again.
  182.  
  183.      If neither the current class, its named base classes, nor the UNIVERSAL
  184.      class contains the requested method, these three places are searched all
  185.      over again, this time looking for a method named _A_U_T_O_L_O_A_D().  If an
  186.      AUTOLOAD is found, this method is called on behalf of the missing method,
  187.      setting the package global $AUTOLOAD to be the fully qualified name of
  188.      the method that was intended to be called.
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))                                                          PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  203.  
  204.  
  205.  
  206.      If none of that works, Perl finally gives up and complains.
  207.  
  208.      Perl classes do method inheritance only.  Data inheritance is left up to
  209.      the class itself.  By and large, this is not a problem in Perl, because
  210.      most classes model the attributes of their object using an anonymous
  211.      hash, which serves as its own little namespace to be carved up by the
  212.      various classes that might want to do something with the object.  The
  213.      only problem with this is that you can't sure that you aren't using a
  214.      piece of the hash that isn't already used.  A reasonable workaround is to
  215.      prepend your fieldname in the hash with the package name.
  216.  
  217.          sub bump {
  218.              my $self = shift;
  219.              $self->{ __PACKAGE__ . ".count"}++;
  220.          }
  221.  
  222.  
  223.      AAAA MMMMeeeetttthhhhoooodddd iiiissss SSSSiiiimmmmppppllllyyyy aaaa SSSSuuuubbbbrrrroooouuuuttttiiiinnnneeee
  224.  
  225.      Unlike say C++, Perl doesn't provide any special syntax for method
  226.      definition.  (It does provide a little syntax for method invocation
  227.      though.  More on that later.)  A method expects its first argument to be
  228.      the object (reference) or package (string) it is being invoked on.  There
  229.      are just two types of methods, which we'll call class and instance.
  230.      (Sometimes you'll hear these called static and virtual, in honor of the
  231.      two C++ method types they most closely resemble.)
  232.  
  233.      A class method expects a class name as the first argument.  It provides
  234.      functionality for the class as a whole, not for any individual object
  235.      belonging to the class.  Constructors are typically class methods.  Many
  236.      class methods simply ignore their first argument, because they already
  237.      know what package they're in, and don't care what package they were
  238.      invoked via.  (These aren't necessarily the same, because class methods
  239.      follow the inheritance tree just like ordinary instance methods.)
  240.      Another typical use for class methods is to look up an object by name:
  241.  
  242.          sub find {
  243.              my ($class, $name) = @_;
  244.              $objtable{$name};
  245.          }
  246.  
  247.      An instance method expects an object reference as its first argument.
  248.      Typically it shifts the first argument into a "self" or "this" variable,
  249.      and then uses that as an ordinary reference.
  250.  
  251.          sub display {
  252.              my $self = shift;
  253.              my @keys = @_ ? @_ : sort keys %$self;
  254.              foreach $key (@keys) {
  255.                  print "\t$key => $self->{$key}\n";
  256.              }
  257.          }
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))                                                          PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  269.  
  270.  
  271.  
  272.      MMMMeeeetttthhhhoooodddd IIIInnnnvvvvooooccccaaaattttiiiioooonnnn
  273.  
  274.      There are two ways to invoke a method, one of which you're already
  275.      familiar with, and the other of which will look familiar.  Perl 4 already
  276.      had an "indirect object" syntax that you use when you say
  277.  
  278.          print STDERR "help!!!\n";
  279.  
  280.      This same syntax can be used to call either class or instance methods.
  281.      We'll use the two methods defined above, the class method to lookup an
  282.      object reference and the instance method to print out its attributes.
  283.  
  284.          $fred = find Critter "Fred";
  285.          display $fred 'Height', 'Weight';
  286.  
  287.      These could be combined into one statement by using a BLOCK in the
  288.      indirect object slot:
  289.  
  290.          display {find Critter "Fred"} 'Height', 'Weight';
  291.  
  292.      For C++ fans, there's also a syntax using -> notation that does exactly
  293.      the same thing.  The parentheses are required if there are any arguments.
  294.  
  295.          $fred = Critter->find("Fred");
  296.          $fred->display('Height', 'Weight');
  297.  
  298.      or in one statement,
  299.  
  300.          Critter->find("Fred")->display('Height', 'Weight');
  301.  
  302.      There are times when one syntax is more readable, and times when the
  303.      other syntax is more readable.  The indirect object syntax is less
  304.      cluttered, but it has the same ambiguity as ordinary list operators.
  305.      Indirect object method calls are parsed using the same rule as list
  306.      operators: "If it looks like a function, it is a function".  (Presuming
  307.      for the moment that you think two words in a row can look like a function
  308.      name.  C++ programmers seem to think so with some regularity, especially
  309.      when the first word is "new".)  Thus, the parentheses of
  310.  
  311.          new Critter ('Barney', 1.5, 70)
  312.  
  313.      are assumed to surround ALL the arguments of the method call, regardless
  314.      of what comes after.  Saying
  315.  
  316.          new Critter ('Bam' x 2), 1.4, 45
  317.  
  318.      would be equivalent to
  319.  
  320.          Critter->new('Bam' x 2), 1.4, 45
  321.  
  322.      which is unlikely to do what you want.
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))                                                          PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  335.  
  336.  
  337.  
  338.      There are times when you wish to specify which class's method to use.  In
  339.      this case, you can call your method as an ordinary subroutine call, being
  340.      sure to pass the requisite first argument explicitly:
  341.  
  342.          $fred =  MyCritter::find("Critter", "Fred");
  343.          MyCritter::display($fred, 'Height', 'Weight');
  344.  
  345.      Note however, that this does not do any inheritance.  If you wish merely
  346.      to specify that Perl should _S_T_A_R_T looking for a method in a particular
  347.      package, use an ordinary method call, but qualify the method name with
  348.      the package like this:
  349.  
  350.          $fred = Critter->MyCritter::find("Fred");
  351.          $fred->MyCritter::display('Height', 'Weight');
  352.  
  353.      If you're trying to control where the method search begins _a_n_d you're
  354.      executing in the class itself, then you may use the SUPER pseudo class,
  355.      which says to start looking in your base class's @ISA list without having
  356.      to name it explicitly:
  357.  
  358.          $self->SUPER::display('Height', 'Weight');
  359.  
  360.      Please note that the SUPER:: construct is meaningful _o_n_l_y within the
  361.      class.
  362.  
  363.      Sometimes you want to call a method when you don't know the method name
  364.      ahead of time.  You can use the arrow form, replacing the method name
  365.      with a simple scalar variable containing the method name:
  366.  
  367.          $method = $fast ? "findfirst" : "findbest";
  368.          $fred->$method(@args);
  369.  
  370.  
  371.      DDDDeeeeffffaaaauuuulllltttt UUUUNNNNIIIIVVVVEEEERRRRSSSSAAAALLLL mmmmeeeetttthhhhooooddddssss
  372.  
  373.      The UNIVERSAL package automatically contains the following methods that
  374.      are inherited by all other classes:
  375.  
  376.      isa(CLASS)
  377.          isa returns _t_r_u_e if its object is blessed into a subclass of CLASS
  378.  
  379.          isa is also exportable and can be called as a sub with two arguments.
  380.          This allows the ability to check what a reference points to. Example
  381.  
  382.              use UNIVERSAL qw(isa);
  383.  
  384.              if(isa($ref, 'ARRAY')) {
  385.                  #...
  386.              }
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))                                                          PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  401.  
  402.  
  403.  
  404.      can(METHOD)
  405.          can checks to see if its object has a method called METHOD, if it
  406.          does then a reference to the sub is returned, if it does not then
  407.          _u_n_d_e_f is returned.
  408.  
  409.      VERSION( [NEED] )
  410.          VERSION returns the version number of the class (package).  If the
  411.          NEED argument is given then it will check that the current version
  412.          (as defined by the $VERSION variable in the given package) not less
  413.          than NEED; it will die if this is not the case.  This method is
  414.          normally called as a class method.  This method is called
  415.          automatically by the VERSION form of use.
  416.  
  417.              use A 1.2 qw(some imported subs);
  418.              # implies:
  419.              A->VERSION(1.2);
  420.  
  421.  
  422.      NNNNOOOOTTTTEEEE:::: can directly uses Perl's internal code for method lookup, and isa
  423.      uses a very similar method and cache-ing strategy. This may cause strange
  424.      effects if the Perl code dynamically changes @ISA in any package.
  425.  
  426.      You may add other methods to the UNIVERSAL class via Perl or XS code.
  427.      You do not need to use UNIVERSAL in order to make these methods available
  428.      to your program.  This is necessary only if you wish to have isa
  429.      available as a plain subroutine in the current package.
  430.  
  431.      DDDDeeeessssttttrrrruuuuccccttttoooorrrrssss
  432.  
  433.      When the last reference to an object goes away, the object is
  434.      automatically destroyed.  (This may even be after you exit, if you've
  435.      stored references in global variables.)  If you want to capture control
  436.      just before the object is freed, you may define a DESTROY method in your
  437.      class.  It will automatically be called at the appropriate moment, and
  438.      you can do any extra cleanup you need to do.  Perl passes a reference to
  439.      the object under destruction as the first (and only) argument.  Beware
  440.      that the reference is a read-only value, and cannot be modified by
  441.      manipulating $_[0] within the destructor.  The object itself (i.e.  the
  442.      thingy the reference points to, namely ${$_[0]}, @{$_[0]}, %{$_[0]} etc.)
  443.      is not similarly constrained.
  444.  
  445.      If you arrange to re-bless the reference before the destructor returns,
  446.      perl will again call the DESTROY method for the re-blessed object after
  447.      the current one returns.  This can be used for clean delegation of object
  448.      destruction, or for ensuring that destructors in the base classes of your
  449.      choosing get called.  Explicitly calling DESTROY is also possible, but is
  450.      usually never needed.
  451.  
  452.      Do not confuse the foregoing with how objects _C_O_N_T_A_I_N_E_D in the current
  453.      one are destroyed.  Such objects will be freed and destroyed
  454.      automatically when the current object is freed, provided no other
  455.      references to them exist elsewhere.
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))                                                          PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  467.  
  468.  
  469.  
  470.      WWWWAAAARRRRNNNNIIIINNNNGGGG
  471.  
  472.      While indirect object syntax may well be appealing to English speakers
  473.      and to C++ programmers, be not seduced!  It suffers from two grave
  474.      problems.
  475.  
  476.      The first problem is that an indirect object is limited to a name, a
  477.      scalar variable, or a block, because it would have to do too much
  478.      lookahead otherwise, just like any other postfix dereference in the
  479.      language.  (These are the same quirky rules as are used for the
  480.      filehandle slot in functions like print and printf.)  This can lead to
  481.      horribly confusing precedence problems, as in these next two lines:
  482.  
  483.          move $obj->{FIELD};                 # probably wrong!
  484.          move $ary[$i];                      # probably wrong!
  485.  
  486.      Those actually parse as the very surprising:
  487.  
  488.          $obj->move->{FIELD};                # Well, lookee here
  489.          $ary->move->[$i];                   # Didn't expect this one, eh?
  490.  
  491.      Rather than what you might have expected:
  492.  
  493.          $obj->{FIELD}->move();              # You should be so lucky.
  494.          $ary[$i]->move;                     # Yeah, sure.
  495.  
  496.      The left side of ``->'' is not so limited, because it's an infix
  497.      operator, not a postfix operator.
  498.  
  499.      As if that weren't bad enough, think about this: Perl must guess _a_t
  500.      _c_o_m_p_i_l_e _t_i_m_e whether name and move above are functions or methods.
  501.      Usually Perl gets it right, but when it doesn't it, you get a function
  502.      call compiled as a method, or vice versa.  This can introduce subtle bugs
  503.      that are hard to unravel.  For example, calling a method new in indirect
  504.      notation--as C++ programmers are so wont to do--can be miscompiled into a
  505.      subroutine call if there's already a new function in scope.  You'd end up
  506.      calling the current package's new as a subroutine, rather than the
  507.      desired class's method.  The compiler tries to cheat by remembering
  508.      bareword requires, but the grief if it messes up just isn't worth the
  509.      years of debugging it would likely take you to to track such subtle bugs
  510.      down.
  511.  
  512.      The infix arrow notation using ``->'' doesn't suffer from either of these
  513.      disturbing ambiguities, so we recommend you use it exclusively.
  514.  
  515.      SSSSuuuummmmmmmmaaaarrrryyyy
  516.  
  517.      That's about all there is to it.  Now you need just to go off and buy a
  518.      book about object-oriented design methodology, and bang your forehead
  519.      with it for the next six months or so.
  520.  
  521.  
  522.  
  523.  
  524.  
  525.                                                                         PPPPaaaaggggeeee 8888
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))                                                          PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  533.  
  534.  
  535.  
  536.      TTTTwwwwoooo----PPPPhhhhaaaasssseeeedddd GGGGaaaarrrrbbbbaaaaggggeeee CCCCoooolllllllleeeeccccttttiiiioooonnnn
  537.  
  538.      For most purposes, Perl uses a fast and simple reference-based garbage
  539.      collection system.  For this reason, there's an extra dereference going
  540.      on at some level, so if you haven't built your Perl executable using your
  541.      C compiler's -O flag, performance will suffer.  If you _h_a_v_e built Perl
  542.      with cc -O, then this probably won't matter.
  543.  
  544.      A more serious concern is that unreachable memory with a non-zero
  545.      reference count will not normally get freed.  Therefore, this is a bad
  546.      idea:
  547.  
  548.          {
  549.              my $a;
  550.              $a = \$a;
  551.          }
  552.  
  553.      Even thought $a _s_h_o_u_l_d go away, it can't.  When building recursive data
  554.      structures, you'll have to break the self-reference yourself explicitly
  555.      if you don't care to leak.  For example, here's a self-referential node
  556.      such as one might use in a sophisticated tree structure:
  557.  
  558.          sub new_node {
  559.              my $self = shift;
  560.              my $class = ref($self) || $self;
  561.              my $node = {};
  562.              $node->{LEFT} = $node->{RIGHT} = $node;
  563.              $node->{DATA} = [ @_ ];
  564.              return bless $node => $class;
  565.          }
  566.  
  567.      If you create nodes like that, they (currently) won't go away unless you
  568.      break their self reference yourself.  (In other words, this is not to be
  569.      construed as a feature, and you shouldn't depend on it.)
  570.  
  571.      Almost.
  572.  
  573.      When an interpreter thread finally shuts down (usually when your program
  574.      exits), then a rather costly but complete mark-and-sweep style of garbage
  575.      collection is performed, and everything allocated by that thread gets
  576.      destroyed.  This is essential to support Perl as an embedded or a
  577.      multithreadable language.  For example, this program demonstrates Perl's
  578.      two-phased garbage collection:
  579.  
  580.          #!/usr/bin/perl
  581.          package Subtle;
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.                                                                         PPPPaaaaggggeeee 9999
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))                                                          PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  599.  
  600.  
  601.  
  602.          sub new {
  603.              my $test;
  604.              $test = \$test;
  605.              warn "CREATING " . \$test;
  606.              return bless \$test;
  607.          }
  608.  
  609.          sub DESTROY {
  610.              my $self = shift;
  611.              warn "DESTROYING $self";
  612.          }
  613.  
  614.          package main;
  615.  
  616.          warn "starting program";
  617.          {
  618.              my $a = Subtle->new;
  619.              my $b = Subtle->new;
  620.              $$a = 0;  # break selfref
  621.              warn "leaving block";
  622.          }
  623.  
  624.          warn "just exited block";
  625.          warn "time to die...";
  626.          exit;
  627.  
  628.      When run as /_t_m_p/_t_e_s_t, the following output is produced:
  629.  
  630.          starting program at /tmp/test line 18.
  631.          CREATING SCALAR(0x8e5b8) at /tmp/test line 7.
  632.          CREATING SCALAR(0x8e57c) at /tmp/test line 7.
  633.          leaving block at /tmp/test line 23.
  634.          DESTROYING Subtle=SCALAR(0x8e5b8) at /tmp/test line 13.
  635.          just exited block at /tmp/test line 26.
  636.          time to die... at /tmp/test line 27.
  637.          DESTROYING Subtle=SCALAR(0x8e57c) during global destruction.
  638.  
  639.      Notice that "global destruction" bit there?  That's the thread garbage
  640.      collector reaching the unreachable.
  641.  
  642.      Objects are always destructed, even when regular refs aren't and in fact
  643.      are destructed in a separate pass before ordinary refs just to try to
  644.      prevent object destructors from using refs that have been themselves
  645.      destructed.  Plain refs are only garbage-collected if the destruct level
  646.      is greater than 0.  You can test the higher levels of global destruction
  647.      by setting the PERL_DESTRUCT_LEVEL environment variable, presuming
  648.      -DDEBUGGING was enabled during perl build time.
  649.  
  650.      A more complete garbage collection strategy will be implemented at a
  651.      future date.
  652.  
  653.  
  654.  
  655.  
  656.  
  657.                                                                        PPPPaaaaggggeeee 11110000
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))                                                          PPPPEEEERRRRLLLLOOOOBBBBJJJJ((((1111))))
  665.  
  666.  
  667.  
  668.      In the meantime, the best solution is to create a non-recursive container
  669.      class that holds a pointer to the self-referential data structure.
  670.      Define a DESTROY method for the containing object's class that manually
  671.      breaks the circularities in the self-referential structure.
  672.  
  673. SSSSEEEEEEEE AAAALLLLSSSSOOOO
  674.      A kinder, gentler tutorial on object-oriented programming in Perl can be
  675.      found in the _p_e_r_l_t_o_o_t manpage.  You should also check out the _p_e_r_l_b_o_t
  676.      manpage for other object tricks, traps, and tips, as well as the
  677.      _p_e_r_l_m_o_d_l_i_b manpage for some style guides on constructing both modules and
  678.      classes.
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.                                                                        PPPPaaaaggggeeee 11111111
  724.  
  725.  
  726.  
  727.