G'day...

Yeah, I should have used qw not qq, and after I sent it I thought "Why
didn't I just say keys %InheritableClassData instead.." :)

I'm trying to create a class that has data members which are the same
for all instances, including instances of derived classes, as well as
containing data members which exist in all instances, but values are
class dependent.

Considering that I've only more recently mastered the basics of
objects/classes in Perl - this is quite a daunting thing!

I've been reading through perltootc, but a lot of it goes straight over
the top of my head (or I only remember part of the concept Tom is
talking about).

I guess it means I just have to keep studying - although any help with
this task would be appreciated. :)

What I was trying to achieve with this:

--START--
: our %InheritableClassData = (
        DBH => undef,
        Q => undef,
        Handler => undef,
);

foreach (qq(DBH Q Handler)) {
        sub $_ {
                shift;
                $InheritableClassData{$_} = shift if @_;
                return $InheritableClassData{$_};
        }
}
---END---

Was really just an attempt at a shorthand version of:

--START--
: our %InheritableClassData = (
        DBH => undef,
        Q => undef,
        Handler => undef,
);

sub DBH {
        shift;
        $InheritableClassData{DBH} = shift if @_;
        return $InheritableClassData{DBH};
}

sub Q {
        shift;
        $InheritableClassData{Q} = shift if @_;
        return $InheritableClassData{Q};
}

sub Handler {
        shift;
        $InheritableClassData{Handler} = shift if @_;
        return $InheritableClassData{Handler};
}
---END---

Should I be using "my" or "our" here for the variables - these are the
variables I want present in all my derived classes and should be the
same for all.


Thanks heaps Charles, your help and assistance on this list is
appreciated by us all...


Regards,

 

Michael S. E. Kraus

Software Developer/Technical Support Specialist
Wild Technology Pty Ltd
________________________________
ABN 98 091 470 692
Level 4 Tiara, 306/9 Crystal Street, Waterloo NSW 2017, Australia
Telephone 1300-13-9453 |  Facsimile 1300-88-9453
http://www.wildtechnology.net

 

The information contained in this email message and any attachments may
be confidential information and may also be the subject of client legal
- legal professional privilege. If you are not the intended recipient,
any use, interference with, disclosure or copying of this material is
unauthorised and prohibited.   This email and any attachments are also
subject to copyright.  No part of them may be reproduced, adapted or
transmitted without the written permission of the copyright owner.  If
you have received this email in error, please immediately advise the
sender by return email and delete the message from your system.


-----Original Message-----
From: Charles K. Clarkson [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, 17 November 2004 5:30 PM
To: [EMAIL PROTECTED]
Subject: Spam:RE: Autmatic function creation

Michael Kraus <[EMAIL PROTECTED]> wrote:

: Just wondering if this code snippet will behave as expected,
: and if not then why not?  :)

    No, I don't think so. One error is in using the qq() operator
instead of the qw() operator. Even with that fix, perl throws syntax
errors.


: --START--
: our %InheritableClassData = (
:       DBH => undef,
:       Q => undef,
:       Handler => undef,
: );
: 
: foreach (qq(DBH Q Handler)) {
:       sub $_ {
:               shift;
:               $InheritableClassData{$_} = shift if @_;
:               return $InheritableClassData{$_};
:       }
: }

    We could test solutions by examining the symbol table.

    When creating new functions (or methods in an object), perl adds
entries to the symbol table (%::). If we store the keys previous to
setting the functions, we can delete() them from the table keys
afterward and find if any new symbols were added.

use Data::Dumper 'Dumper';
        .
        .
        .

# store current symbol table keys
my @prev_keys = keys %::;


foreach my $method ( keys %InheritableClassData ) {
    no strict 'refs';
    *$method = sub {
        shift;
        $InheritableClassData{$_} = shift if @_;
        return $InheritableClassData{$_};
    };
}


# store current symbol table
my %new_table = %::;

# delete old keys
delete @new_table{ @prev_keys };

print
    "Added symbol table entries\n",
    Dumper \%new_table;

__END__

    I get:

Added symbol table entries
$VAR1 = {
          'Handler' => *::Handler,
          'Q' => *::Q,
          'DBH' => *::DBH
        };



HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to