true_atlantis has asked for the wisdom of the Perl Monks concerning the following question:

I have this class
package one; sub new { my ($class,%args)=@_; my $self=(); $self->{test}='test'; bless($self,$class); return($self); } 1;


how do i access the array of an object of type 'one'... for example
#!/usr/bin/perl use Data::Dumper; use one; my $x=one->new(); print(Dumper($x));

will print out
$VAR1 = bless( { 'test' => 'test' }, 'one' );

I want to get the keys of $x, is it possible?

Replies are listed 'Best First'.
Re: Access custom classes 'self' array
by kyle (Abbot) on Sep 11, 2007 at 03:49 UTC

    Your $x is still just a hash reference, and you can treat it like one, although doing so breaks encapsulation and is therefore not recommended. What this means is that you can just say keys %{$x} and get the keys. You can also say $x->{test} and access the "test" key.

    By the way, "my $self = ()" is equivalent to "my $self = undef". I think you mean "my $self = {}".

Re: Access custom classes 'self' array
by CountZero (Bishop) on Sep 11, 2007 at 06:03 UTC
    Did you read perlboot, perltoot, perltooc and perlbot? These perldoc files contain the basic knowledge of using objects in Perl.

    Or use one of the object-constructing modules (my present favourite being Moose, but that my be overkill and Class::Dot or such may be sufficient).

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Yes, I have read all of those. This is just a basic example to show the idea of what im trying to do. My real world situation is a bit different. I have a set of classes that are inherited from a base 'Object' class. I can write one function in the 'Object' class that takes care of all the inherited classes, or write the function 20 times in each of the inherited classes... Do you see what I am saying?
        OK, in that case you really should have a look at Moose. Although it is a bit "heavy", it has a very clear structure and allows for all forms of inheritance with well constructed build and destroy mechanisms. You don't even have to write your own new routine as it is already provided for you!

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Access custom classes 'self' array
by Corion (Patriarch) on Sep 11, 2007 at 06:48 UTC

    When dealing with references, I recommend tye's References Quick Reference. Of course you break encapsulation, but that's a different kettle of fish.

Re: Access custom classes 'self' array
by eric256 (Parson) on Sep 11, 2007 at 14:26 UTC

    Since $x is a blessed hashref you can treat it as a hashref and it will work. my @keys = keys %$x; It is recommended to use the available methods on the object instead.

    You mentioned that you have many classes that inherit from Object and they each need a method. If the method is always the same then put it in object, if it differs every time then put it in the child classes. If it is normally the same but sometimes different put it in the object class and override it in the child classes.


    ___________
    Eric Hodges