new, DESTROY and bless... 
Author Message
 new, DESTROY and bless...

I don't quite understand why the following does not print "gone...\n"?

package CLASS;

sub new {print "new!\n";}
sub DESTROY {print "gone...\n"; }

package main;
$instance = new CLASS;
undef $instance;

This can be done (as in the tests) with

sub new {print "new!\n"; my($x) = 'whatever'; bless (\$x); }

but could anybody explain why this is needed?
--

The joy of engineering is to find a straight line on a double
logarithmic diagram.



Fri, 14 Mar 1997 02:33:22 GMT  
 new, DESTROY and bless...

Quote:

>I don't quite understand why the following does not print "gone...\n"?

Ah progess, I am starting to understand perl5 :-)
You forgot to return a blessed object, so you do not actually
have a handle back into CLASS.  Try modifying the one line as
below.  PLEASE someone tell me I've got it right :-)

Quote:
>package CLASS;

>sub new {print "new!\n";}

sub new {print "new!\n"; bless {};}           #  <== blessed be ourself

Quote:
>sub DESTROY {print "gone...\n"; }

>package main;
>$instance = new CLASS;
>undef $instance;

Pax,
Joel
--
--
               T E C H N E   R E S E A R C H    L I M I T E D          

          Finchampstead, Wokingham   +   Phone: +44 (734) 730.260      
          Berkshire RG11 4YD (UK)    +   Fax:   +44 (734) 730.272      


Fri, 14 Mar 1997 18:29:14 GMT  
 new, DESTROY and bless...

Quote:

>I don't quite understand why the following does not print "gone...\n"?

>package CLASS;

>sub new {print "new!\n";}
>sub DESTROY {print "gone...\n"; }

>package main;
>$instance = new CLASS;
>undef $instance;

>This can be done (as in the tests) with

>sub new {print "new!\n"; my($x) = 'whatever'; bless (\$x); }

>but could anybody explain why this is needed?

It's because there's absolutely nothing special about the subroutine
name "new" at all. You have simply declared a subroutine &CLASS::new
which prints a message and returns the value 1 (print returns value
true in this scalar context). Your line
    $instance = new CLASS;
is exaclty equivalent to
    $instance = &Class::new();
in this case (since there's no need to inherit). The thing which
usually sets subroutines called "new" apart from other subroutines
is the fact that they create a reference to something and *bless* it.
It is that fact, and that alone, that makes an Perl5 object that can
have method calls invoked on it and is eligible for things like the
DESTROY destructor. It is purely convention that most constructors
are called "new". Examples of some other (conventional) constructors
are the TIESCALAR, TIEARRAY and TIEHASH methods.

--Malcolm

--

Oxford University Computing Services
"Widget. It's got a widget. A lovely widget. A widget it has got." --Jack Dee



Sun, 16 Mar 1997 05:28:19 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Need perl 5 help on new stuff (tie, bless, my, etc)

2. Tie/bless vs. bless/tie and segmentation fault

3. DESTROY not destroying

4. Puzzle: DESTROY not destroying

5. destroy and DESTROY in Tk/Perl

6. Blessing a reference in an XSUB

7. perl5 - ref and blessed scalar vs array references

8. Storable: error with blessed undef

9. type of blessed object?

10. Unconsistent behaviour of copied blessed objects

11. Blessing an HV into a package in a .xs file

12. The bless operator

 

 
Powered by phpBB® Forum Software