Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Perl's equivalent to Python's hasattr and getattr? (class variable details)

by MistaMuShu (Beadle)
on Jul 28, 2005 at 23:46 UTC ( [id://479178]=note: print w/replies, xml ) Need Help??


in reply to Re: Perl's equivalent to Python's hasattr and getattr? (class variable details)
in thread Perl's equivalent to Python's hasattr and getattr? (class variable details)

Wow, that hits it right on the spot! I find it weird that this isn't builtin. Maybe I'll skim through and look up how Class::Accessor is implemented. Is it possible to do something similar theorically by manipulating symbol tables?
  • Comment on Re^2: Perl's equivalent to Python's hasattr and getattr? (class variable details)

Replies are listed 'Best First'.
Re^3: Perl's equivalent to Python's hasattr and getattr? (class variable details)
by rlb3 (Deacon) on Jul 29, 2005 at 00:03 UTC

    Class::Accessor is built using the symbol table. Here is the _mk_accessors as an example.

    { no strict 'refs'; sub _mk_accessors { my($self, $maker, @fields) = @_; my $class = ref $self || $self; # So we don't have to do lots of lookups inside the loop. $maker = $self->can($maker) unless ref $maker; foreach my $field (@fields) { if( $field eq 'DESTROY' ) { require Carp; &Carp::carp("Having a data accessor named DESTROY in +". "'$class' is unwise."); } my $accessor = $self->$maker($field); my $alias = "_${field}_accessor"; *{$class."\:\:$field"} = $accessor unless defined &{$class."\:\:$field"}; *{$class."\:\:$alias"} = $accessor unless defined &{$class."\:\:$alias"}; } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://479178]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-06-23 15:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.