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

I have this object:
$VAR1 = bless( { 'first_row' => 0, 'aliases' => [], 'is_limited' => 1, 'joins_are_distinct' => undef, 'limit_clause' => '', 'custom_field' => bless( { '_class' => 'RT::CustomField', 'original_user' => undef,'_SB_Record_Primary_RecordCache_ke +y' => 'id=17', 'fetched' => {'valuesclass' => 1, 'order' => 1, 'canonicalizeclass' => 1 }, 'values' => { 'description' => 'Tipo de caso a ser tratado', 'maxvalues' => 1, 'name' => 'Case type', 'valuesclass' => undef, ...............
I am trying to access "Name" value (Case type) in this way: " $CustomField->Values->Name;" but I am getting error. Could you tell me what is he correct way?

Replies are listed 'Best First'.
Re: Get value from object
by tybalt89 (Monsignor) on Feb 09, 2019 at 20:26 UTC

    Just to see if this could be done :)

    NOTE: I faked the rest of your object since yours was incomplete :(

    #!/usr/bin/perl -l # https://perlmonks.org/?node_id=1229668 use strict; use warnings; my $CustomField = bless { foo => 1, values => bless({ maxvalues => 1, name => 'Case type', }, 'ClassOne'), bar => 2, }, 'ClassTwo'; use Data::Dumper; print Dumper $CustomField; print '$CustomField->Values->Name = ', $CustomField->Values->Name; ######################## just add this ( hehehe :) sub UNIVERSAL::AUTOLOAD { my $basename = (split /::/, $UNIVERSAL::AUTOLOAD)[-1]; $basename eq 'DESTROY' and return; my ($realname) = grep lc $basename eq lc, keys %{ $_[0] }; defined $realname or die "Missing the key '$basename'"; return $_[0]{$realname}; }

    Outputs:

    $VAR1 = bless( { 'values' => bless( { 'name' => 'Case type', 'maxvalues' => 1 }, 'ClassOne' ), 'bar' => 2, 'foo' => 1 }, 'ClassTwo' ); $CustomField->Values->Name = Case type
      Finally I got it: the correct way was: $CFVs->{custom_field}->{values}->{name}; Thanks for your help
        This abuses the internal representation of the object and breaks encapsulation. It can also bypass triggers or similar advanced features.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Get value from object
by hippo (Archbishop) on Feb 09, 2019 at 18:29 UTC
    but I am getting error.

    Hello danny20085. It might not have occurred to you but the exact text of that error message is going to be very useful to anyone trying to help you, as would the class name of the object. An SSCCE would be better yet.

Re: Get value from object
by NetWallah (Canon) on Feb 10, 2019 at 01:41 UTC
    Here is a more traditional way to accomplish this (Compared to tybalt89's far more elegant solution):
    #!/usr/bin/perl -l # https://perlmonks.org/?node_id=1229668 use strict; use warnings; my $OutsideObject= bless( { 'first_row' => 0, 'aliases' => [], 'is_limited' => 1, 'joins_are_distinct' => undef, 'limit_clause' => '', 'custom_field' => bless( { '_class' => 'RT::CustomField', 'original_user' => undef,'_SB_Record_Primary_RecordCache_ke +y' => 'id=17', 'fetched' => {'valuesclass' => 1, 'order' => 1, 'canonicalizeclass' => 1 }, 'values' => { 'description' => 'Tipo de caso a ser tratado', 'maxvalues' => 1, 'name' => 'Case type', 'valuesclass' => undef, }, #### LINES ADDED #### To complete }, 'CUSTOMCLASS'), #### Missing information }, "OUTER_CLASS"); ### From your post my $CustomField = $OutsideObject->{custom_field}; print "VALUES->NAME = ", $CustomField->Values->Name , "\n"; BEGIN{ package CUSTOMCLASS; sub Values { my ($self) = @_; $self->{_INTERNAL_POINTER} = "values"; return $self; # Allow chaining } sub Name{ my ($self) = @_; $self->{_INTERNAL_POINTER} and return $self->{ $self->{_INTERNAL_P +OINTER} }->{name}; return $self->{name}; } 1; }
    OUTPUT:
    VALUES->NAME = Case type

                    As a computer, I find your faith in technology amusing.

Re: Get value from object
by BillKSmith (Monsignor) on Feb 09, 2019 at 20:04 UTC
    You probably should be using a method to access a field of an object (unless you are actually trying to write that method).
    Bill
Re: Get value from object
by haj (Vicar) on Feb 09, 2019 at 18:30 UTC

    The keys in the hash are case sensitive:

    my $field = $CustomField->values->name;
Re: Get value from object
by tobyink (Canon) on Feb 10, 2019 at 11:01 UTC

    According to the documentation for RT::CustomField, the $CustomField->Values part should be fine. That will return an object, but depending on the field what type of object it returns can vary. You can find out what type of object it's returning using ref($CustomField->Values). Then check the documentation for that class to find out how to get its name.