I am getting a syntax error with the following code
print $stupid_obj->$methods[0], "\n";
If I have
$a_method = $methods[0];
print $stupid_obj->$a_method, "\n";
it works!
The error is
syntax error at ./molbiolog.perl line 106, near "$methods["
I tried
print $stupid_obj->${methods[0]}, "\n";
Same error.
I am a at a bit of a lose about this.
Aengus
> I am getting a syntax error with the following code
> print $stupid_obj->$methods[0], "\n";
> If I have
> $a_method = $methods[0];
> print $stupid_obj->$a_method, "\n";
> it works!
> The error is
> syntax error at ./molbiolog.perl line 106, near "$methods["
i found that
$object->${\$methods[0]}
works... but there must be another way...
--
http://tinita.de \ enter__| |__the___ _ _ ___
PerlQuotes: http://tinita.de/pq.html \ / _` / _ \/ _ \ '_(_-< of
MovieDB: http://tinita.de/tmdb.html \ \ _,_\ __/\ __/_| /__/ perception
~Mike L.
"Tina Mueller" <tin...@zedat.fu-berlin.de> wrote in message
news:a3bg1p$16l2hk$1...@fu-berlin.de...
TM> Aengus Stewart <aengus....@icrf.icnet.uk> wrote:
>> I am getting a syntax error with the following code
>> print $stupid_obj->$methods[0], "\n";
>> If I have
>> $a_method = $methods[0];
>> print $stupid_obj->$a_method, "\n";
>> it works!
>> The error is
>> syntax error at ./molbiolog.perl line 106, near "$methods["
TM> i found that
TM> $object->${\$methods[0]}
TM> works... but there must be another way...
you can't call a method indirectly except through a scalar
variable. your method is very obscure and not a good idea. you just have
to assign the method name first to a scalar and call it with that.
uri
--
Uri Guttman ------ u...@stemsystems.com -------- http://www.stemsystems.com
-- Stem is an Open Source Network Development Toolkit and Application Suite -
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
You need to cast the scalar reference to a method. Not sure if I got that
right, but this is how you do it:
$stupid_obj->&$methods[0]
or
$stupid_obj->$methods[0]->()
I prefer the latter one, it's art.
- Samuel aka Trahojen
> i found that
> $object->${\$methods[0]}
> works... but there must be another way...
In order to call an objects method indirectly, like at runtime, the method
name must be a scalar. The simple assignment of the array item to a scalar
solves this problem neatly. The above construct does the same thing by
forcing the value to become a reference to a scalar which that is then
derefereced, resulting in a scalar. Which is what one needs to invoke the
method this way.
jimbo
;-)
Aengus> If I have
Aengus> $a_method = $methods[0];
Aengus> print $stupid_obj->$a_method, "\n";
Aengus> it works!
Yes. An indirect method call must be a scalar. It's similar to how
an indirect filehandle always had to be a scalar, but late-model Perls
can now use alternate notation to get around it. Unfortunately,
there's no notation to get around this. :(
One alternative is to freeze the binding:
my $a_method = $stupid_obj->can($methods[0]);
$stupid_obj->$a_method(@args);
But that's really no better that what you just did. It just turns a
symbolic ref into a fixed coderef to speed up the later lookups.
print "Just another Perl hacker,"
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Right. The [0] is being parsed as an anonymous array constructor,
which isn't what you want.
>I tried
>print $stupid_obj->${methods[0]}, "\n";
>
>Same error.
I'm not sure what's going on here, but I don't think it's the same
error. (Other folks who want to follow up to this pointing out what
I've missed would be well advised to remember that "${methods[0]}" is
*legal* syntax in other contexts.)
>If I have
>$a_method = $methods[0];
>print $stupid_obj->$a_method, "\n";
>it works!
Use what works.
You could use
$stupid_obj->${\$methods[0]}
but what's the point? It would just be confusing.
--
--
Mark Jason Dominus m...@plover.com
Philadelphia Excursions Mailing List: http://www.plover.com/~mjd/excursions/
They're both syntax errors.
> In article <Isc68.22180$l93.3...@newsb.telia.net>,
> Trahojen <trahojen_s...@hotmail.com> wrote:
> >$stupid_obj->&$methods[0]
> >or
> >$stupid_obj->$methods[0]->()
> >
> >I prefer the latter one, it's art.
>
> They're both syntax errors.
So it's abstract art then ;)
--
Joe Schaefer Then the King smiled. "A hard day," he'd say, "full of
nizzardly worries. A long day," he'd say. "Now it's time for
some fun!"
-- Dr. Seuss