You are not logged in.

#1 2011-06-03 08:23:58

answer_42
Member
From: Steenbergen, Netherlands
Registered: 2010-06-28
Posts: 8

[SOLVED] Perl AUTOLOAD

Hey,

I'm writing a module, and it is just working fine, but now I am trynig to let AUTOLOAD do all the hard work for me, so that I don't have to write all the accessors by hand. But I just don't get to work.

Here is the code I am talking about:

{
    my %_attrs = (
        _email    => ['read/write', ''],
        _pass    => ['read/write', ''],
        _ua        => ['read/write', ''],
        _mech    => ['', undef],
    );
 
    sub _accessible {
        my ($self, $attr, $mode) = @_;
        $_attrs{$attr}[0] =~ /$mode/
    }
}
 
sub AUTOLOAD {
    my ($self, $newval) = @_;

    if ($AUTOLOAD =~ /.*::get(_\w+)/ && $self->_accessible($1, 'read')) {
        # Not really anything useful yet
        print $AUTOLOAD, "\n";
    }
}

The error I get when running the code: (thats in the _accessible subroutine.

Use of uninitialized value in pattern match (m//) at /home/....

It seems as if I %_attrs runs out of the scope, but I thought that it is in a closure and I should be able to access the hash throug the subroutine _accesible. But it isn't working. Does anybody know what I'm doing wrong here?

Thanks, Sebastian

EDIT: I solved, there is nothing wrong with the code, I just called the accessor wrong...so now I'm feeling a little bit stupid...

Last edited by answer_42 (2011-06-03 10:20:10)

Offline

Board footer

Powered by FluxBB