in reply to Re: symbolic references in a hash lookup
in thread symbolic references in a hash lookup

Thats really odd. With this form:
*$name = ${$accessors->{$name}}
I get these errors:

Undefined value assigned to typeglob at lib//EDS/liveQueue/Item.pm line 104.
Can't locate object method "status" via package "EDS::liveQueue::Item" at ./item_driver.pl line 88.
  • Comment on Re^2: symbolic references in a hash lookup

Replies are listed 'Best First'.
Re^3: symbolic references in a hash lookup
by ikegami (Patriarch) on Jun 12, 2009 at 18:19 UTC

    That didn't exist in your code, so I didn't use that.

    Symbolic refs only access package variables. You never assigned a value to the $setter package variable, the error message is correct.

    There's no reason to replace the simple if/else with a lookup table (which is what you are trying to do, really). In fact, why are you creating two subs when you only even want one? Simpler:

    for my $name (keys %{$accessors}){ my $accessor; if( $accessors->{$name} eq "setter" ){ $accessor = sub { ... }; } else { $accessor = sub { ... }; } no strict 'refs'; *$name = $accessor; }

      Symbolic refs only access package variables. You never assigned a value to the $setter package variable, the error message is correct.

      That explains a lot of things.

Re^3: symbolic references in a hash lookup
by NetWallah (Canon) on Jun 12, 2009 at 18:19 UTC
    ${$accessors->{$name}} is equivalent to ${'getter'} so it is meaningless.

    I would suggest restructuring your my $accessors (untested):

    my $accessors = { map ({$_=>{TYPE=>'getter',CODE=>undef}} qw |my gett +er names| ), map ({$_=>{TYPE=>'setter',CODE=>undef}} qw |setter +names2|) };
    Then set the CODE values:
    for (keys %$accessors){ if ($accessor->{$_}{TYPE} eq 'getter'){ $accessor->{$_}->{CODE} = sub {set code}; }else{ .. setter ... }

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)