Re: call method by name

2011-04-12 Thread Lawrence Statton

On 04/12/2011 08:26 AM, 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.

 What is the syntax? please

 Best Regards
 Marcos Rebelo



#!/usr/bin/perl
use strict;
use warnings;
no strict 'refs'; # to allow you to use names as references

sub foo { print "Hello, Foo\n"; }
sub bar { print "Hello, Bar\n"; }
sub baz { print "Hello, Baz\n"; }
sub waldo { print "Hello, Waldo\n"; }
sub pepper { print "Hello, Pepper\n"; }
sub salt { print "Hello, Salt\n"; }
sub pork { print "Hello, Pork\n"; }

for my $verb (qw/foo bar baz waldo pepper salt pork/) {
  $verb->();
}

--L


Re: call method by name

2011-04-12 Thread Paul Johnson
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".

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

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




Re: call method by name

2011-04-12 Thread marcos rebelo
On Tue, Apr 12, 2011 at 15:54, Paul Johnson  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

>
> --
> Paul Johnson - p...@pjcj.net
> http://www.pjcj.net
>



-- 
Marcos Rebelo
http://www.oleber.com/
Milan Perl Mongers leader https://sites.google.com/site/milanperlmongers/
Webmaster of http://perl5notebook.oleber.com

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




Re: call method by name

2011-04-12 Thread Shawn H Corey

On 11-04-12 10:15 AM, marcos rebelo wrote:

On Tue, Apr 12, 2011 at 15:54, Paul Johnson  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



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


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

...

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

__END__

You also don't need $self since the subs are methods to the class and 
are in the same file.



--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early & often.

Eliminate software piracy:  use only FLOSS.

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




Re: call method by name

2011-04-12 Thread Paul Johnson
On Tue, Apr 12, 2011 at 10:30:06AM -0400, Shawn H Corey wrote:
> On 11-04-12 10:15 AM, marcos rebelo wrote:
> >On Tue, Apr 12, 2011 at 15:54, Paul Johnson  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
> >
> 
> What you're trying to do is called a symbolic reference.  It is not
> considered best practice.  Instead, use a hash:
> 
> my %sub_hash = (
>   foo => \&foo,
>   bar => \&bar,
> );
> 
> ...
> 
> foreach my $key ( ... ){
>   $sub_hash{$key};
> }
> 
> __END__
> 
> You also don't need $self since the subs are methods to the class
> and are in the same file.

I believe you are mistaken here.  The technique is fine, and $self is
necessary to call a method rather than a specific subroutine - consider
the case where there is a class hierachy and @ISA is searched.

Your solution would work well were we not dealing with OO.

Either that or I have misunderstood the original question.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

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




Re: call method by name

2011-04-12 Thread Uri Guttman
> "LS" == Lawrence Statton  writes:

  LS> On 04/12/2011 08:26 AM, 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.
  >> 
  >> What is the syntax? please
  >> 
  >> Best Regards
  >> Marcos Rebelo
  >> 

  LS> #!/usr/bin/perl
  LS> use strict;
  LS> use warnings;
  LS> no strict 'refs'; # to allow you to use names as references

that is wrong and very very bad. strict is there for a reason. never do
this. a dispatch table is the correct version of this.

also it doesn't address the actual question of methods by name - these
are plain subs.

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/




Re: call method by name

2011-04-12 Thread Uri Guttman
> "SHC" == Shawn H Corey  writes:

  SHC> On 11-04-12 10:15 AM, marcos rebelo wrote:
  >> On Tue, Apr 12, 2011 at 15:54, Paul Johnson  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/




Re: call method by name

2011-04-12 Thread Rob Dixon

On 12/04/2011 15:30, Shawn H Corey wrote:

On 11-04-12 10:15 AM, marcos rebelo wrote:

On Tue, Apr 12, 2011 at 15:54, Paul Johnson wrote:

On Tue, Apr 12, 2011 at 03:26:49PM +0200, marcos rebelo wrote:


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



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

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

...

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


This would be fine if the subroutines weren't method calls. But to
support polymorphism, the /only way/ to make a reference is by name, and
the /only way/ to call a method by reference is using a scalar variable
holding the method name as a string.

This is proper and correct, because even if we could write

  my $method_ref = \($object->method);  # Incorrect code

we would be taking a snapshot of the method at that moment, but later on
there is no guarantee that $object->method will execute the same code.
However

  my $method_ref = "method";
  $object->$method_ref;

will always do the right thing.

Rob

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