How do I interact with a Perl object that has a hash attribute?

前端 未结 3 1610
忘掉有多难
忘掉有多难 2021-02-10 20:22

I have a class with several variables, one of which is a hash (_runs):

sub new
{
    my ($class, $name) = @_;
    my $self = {
        _name => $name,
                


        
3条回答
  •  攒了一身酷
    2021-02-10 21:02

    You are storing references to array or hashes in your object. To use them with standard functions you'll need to dereference them. For example:

    @{ $self->{_array_ref_key} }; 
    %{ $self->{_hash_ref_key} };
    

    If you need pass parameters to standard function:

    push( @{ $self->{_array_ref_key} }, $some_value );
    for my $hash_key ( keys %{ $self->{_hash_ref_key} }) {
        $self->{_hash_ref_key}{$hash_key}; ## you can access hash value by reference
    }
    

    Also $self->{_hash_ref_key}{$hash_key} syntax is shortcut for $self->{_hash_ref_key}->{$hash_key} (which can make for sense if you see it first time).

    Also take a look at corresponding manual page.

提交回复
热议问题