in reply to symbolic references in a hash lookup

Works for me after I fixed the compile errors (Missing bless, $s vs $self, $value not defined or initialised, missing curly bracket)
  • Comment on Re: symbolic references in a hash lookup

Replies are listed 'Best First'.
Re^2: symbolic references in a hash lookup
by bot403 (Beadle) on Jun 12, 2009 at 18:08 UTC
    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.

      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.

      ${$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)