>>>>> "SHC" == Shawn H Corey <shawnhco...@gmail.com> writes:

  SHC> On 11-04-12 10:15 AM, marcos rebelo wrote:
  >> On Tue, Apr 12, 2011 at 15:54, Paul Johnson<p...@pjcj.net>  wrote:
  >>> >  On Tue, Apr 12, 2011 at 03:26:49PM +0200, marcos rebelo wrote:
  >>>> >>  Hi all
  >>>> >>
  >>>> >>  I have a code like:
  >>>> >>
  >>>> >>  foreach my $key ( ... ) {
  >>>> >>      my $sub = "get_$key";
  >>>> >>      $self->$sub;
  >>>> >>      ...
  >>>> >>  }
  >>>> >>
  >>>> >>  If I can do this, I can also do it without the variable $sub.
  >>> >
  >>> >  You can do that.  You can also do it without the variable $sub.  But
  >>> >  please don't.  It is much clearer with the variable in place.
  >>> >
  >>>> >>  What is the syntax? please
  >>> >
  >>> >    $self->${\"get_$key"};
  >>> >
  >>> >  But please pretend that the answer is "no, you need the variable $sub".
  >> This one is so ugly
  >> 

  SHC> What you're trying to do is called a symbolic reference.  It is not
  SHC> considered best practice.  Instead, use a hash:

no it isn't. sub calls by name are symbolic refs and illegal under
strict. method calls by name are not and are fine to do. it is called
late dispatch or late binding.

  SHC> my %sub_hash = (
  SHC>   foo => \&foo,
  SHC>   bar => \&bar,
  SHC> );

  SHC> ...

  SHC> foreach my $key ( ... ){
  SHC>   $sub_hash{$key};

and that doesn't even make a sub call (method or plain).

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to