in reply to Design Question

Basically I want some way to create a large array in a sub-routine, keep it around as long as I need, and then to get rid of it. And preferrably I'd still want to keep this all in a single package.

Sound to me like you would benefit from OO. Depending on the details of what you are doing I would probably be thinking of either converting the whole lot into a single class, or perhaps into two classes, one for the "common_stuff" and one for the processStuff (which would contain a common_stuff object). Have a read of perltoot and perlboot and maybe perltootc too.


---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

Replies are listed 'Best First'.
Re: Re: Design Question
by Anonymous Monk on Aug 07, 2003 at 02:47 UTC

    I don't feel at all comfortable with Perl OO, to be honest: it feels... weird to me for some reason. But the more "rational" reason I've avoided it here is that my objects really have no attached methods and very little need for privacy. It's just passing (large, large) quantities of data around between subroutines in an orderly fashion. I suppose I can't avoid Perl OO forever, though.

      I don't feel at all comfortable with Perl OO, to be honest: it feels... weird to me for some reason.

      I think a lot of people give "OO" a lot more fear and awe than it deserves. I think partly this is because of all the multisyllable terms that the jargon freaks just love to insist is the cool parts of OO. (Polymorphism, Inheritance, Method Dispatch, Overloading, etc). Perl's OO is really close to the raw idea of OO: it tightly associates data with the methods/subs that operate on that data. (The fact that Perl also does pretty much the full repretoire of OO, with the exception of Data Hiding, but thats not an essential part of OO in my mind.)

      So lets consider this subroutine structure...

      sub make_complex_array { my @args=@_; # ... return $complex_array_ref } sub do_funky_stuff_with_complex_array { my $array=shift; # .... return $value; }

      To convert this to a class/object all we have to do is add one line and alter two! (Well as a sop to convention and aesthetics we'll rename the subs)

      package Complex::Array; sub new { my ($class,@args)=@_; # ... return bless $complex_array_ref,$class; } sub do_funky_stuff { my $array=shift; # .... return $value; }

      And whats so hard about this? Now the $array knows that it isnt just an array with indexes, it knows its a complex data strcture with a set of extended behaviour. If we need to cache things about the array we have a nice convient handle that we do so with.

      The point of this all is that you want to maintain state through a set of package level variables. This isnt a good idea a general rule (singletons aside). If you need to maintain state across multiple subroutine invocations you should use OO. Especially as in Perl its so damn easy. :-)

      my objects really have no attached methods and very little need for privacy.

      There is no privacy in Perl so thats no excuse. As for the methods from what youve described all of your subroutines are method candidates to me. If you like post a more beefy code example and ill refactor it into OO for you.

      HTH


      ---
      demerphq

      <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...