Hi, I've got a small question about the best way to preserve class data across calls to a cgi. In particular, I would like to set class defaults for an attribute, and preserve these defaults after inheriting the methods. For example, If I have the following code:
#!/usr/bin/perl package a; my $bar = 'bar'; sub foo { my $self = shift; @_ ? $bar = shift: $bar; } package b; use base 'a'; sub new { my $class = shift; my $self = bless {}, ref($class)||$class; $self->foo('cow'); return $self; } package main; my $object = do "persistB.obj"; print $object->foo();
where persistB.obj is simply a pre-built instance of class B:
$VAR1 = bless( {}, 'b' );
then the call to $object->foo() return 'bar', instead of the intended 'cow'.

Now, back in the real world I need this to work for a project involving multiple objects inheriting common functions. In the interest of ease, I would like to have a simply setup routine that each class definition uses to set defaults for various attributes, keeping most of the implementation in some parent class.

I originally thought of having a BEGIN code block call a subroutine in the child object, but that won't work as BEGIN doesn't get any arguments passed to it.

Any ideas?

TIA,
webby

In reply to Persistent Class data by webby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.