Q: Prob with symbolic reference to obj method stored in array

0 views
Skip to first unread message

Aengus Stewart

unread,
Jan 31, 2002, 7:35:54 AM1/31/02
to
Hi,

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

Tina Mueller

unread,
Jan 31, 2002, 8:15:37 AM1/31/02
to
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["

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

chengkai liang

unread,
Jan 31, 2002, 9:06:38 AM1/31/02
to
Give a try to $stupid_obj->method[0]->();

~Mike L.
"Tina Mueller" <tin...@zedat.fu-berlin.de> wrote in message
news:a3bg1p$16l2hk$1...@fu-berlin.de...

Uri Guttman

unread,
Jan 31, 2002, 10:08:40 AM1/31/02
to
>>>>> "TM" == Tina Mueller <tin...@zedat.fu-berlin.de> writes:

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

Trahojen

unread,
Jan 31, 2002, 9:24:40 AM1/31/02
to
> 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";

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


jimbo

unread,
Jan 31, 2002, 10:54:03 AM1/31/02
to
"Tina Mueller" <tin...@zedat.fu-berlin.de> wrote

> 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
;-)


Randal L. Schwartz

unread,
Jan 31, 2002, 11:02:45 AM1/31/02
to
>>>>> "Aengus" == Aengus Stewart <aengus....@icrf.icnet.uk> writes:

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!

Mark Jason Dominus

unread,
Jan 31, 2002, 11:48:04 AM1/31/02
to
In article <beffb83e.02013...@posting.google.com>,

Aengus Stewart <aengus....@icrf.icnet.uk> wrote:
>I am getting a syntax error with the following code
>
>print $stupid_obj->$methods[0], "\n";

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/

Mark Jason Dominus

unread,
Jan 31, 2002, 5:30:22 PM1/31/02
to
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.

Joe Schaefer

unread,
Jan 31, 2002, 6:49:04 PM1/31/02
to
m...@plover.com (Mark Jason Dominus) writes:

> 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

Reply all
Reply to author
Forward
0 new messages