CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Help with blessing an object (I think)

 



bacoms
New User

Mar 11, 2009, 12:16 PM

Post #1 of 4 (726 views)
Help with blessing an object (I think) Can't Post

This is my first post on this forum so hi all.

I'd really appreciate some help with this problem I'm having.

I've got the following snippet of code that works fine.

----------------------------------------------------------------------------------------------------------------------
# $_[2] = reference to hash of funds containing NameRef as key and Name as value

foreach my $nameRef (sort{lc(${$_[2]}{$a}) cmp lc(${$_[2]}{$b})} keys %{$_[2]})
----------------------------------------------------------------------------------------------------------------------

However I now need to replace the scalar value in the hash with an object reference.

The 'getName()' method in the object returns the Name so I though I could simply alter the snippet
to read:

----------------------------------------------------------------------------------------------------------------------
# $_[2] = reference to hash of funds containing NameRef as key and Name object as value

foreach my $nameRef (sort{lc(${$_[2]}{$a->getName()}) cmp lc(${$_[2]}{$b->getName()})} keys %{$_[2]})
----------------------------------------------------------------------------------------------------------------------

However when I run this I get the message:

>>>>>>>>> Can't call method "getName" without a package or object reference......

Clearly I have to 'bless' the reference to the 'getName' method but cannot figure out the syntax.

Thanks,Bryan


KevinR
Veteran


Mar 11, 2009, 12:32 PM

Post #2 of 4 (724 views)
Re: [bacoms] Help with blessing an object (I think) [In reply to] Can't Post

If everything else is correct, its just your syntax that is I think not right:


Code
${$_[2]}{$a->getName()}


try like this:


Code
$_[2]->{$a}->getName()

-------------------------------------------------


(This post was edited by KevinR on Mar 11, 2009, 12:32 PM)


bacoms
New User

Mar 12, 2009, 2:45 AM

Post #3 of 4 (710 views)
Re: [KevinR] Help with blessing an object (I think) [In reply to] Can't Post

Thanks for that however I still have the problem with blessing the object (Name) in which the 'getName' method resides. (I get the same message as before.)

I can't figure out the syntax for blessing the object reference when it's embedded on the sort statement. Any ideas.

Thanks, Bryan


bacoms
New User

Mar 12, 2009, 3:44 AM

Post #4 of 4 (706 views)
Re: [KevinR] Help with blessing an object (I think) [In reply to] Can't Post

Found the answer. The syntax is:

foreach my $fundRef (sort{lc(bless($_[2]->{$a}, "Name")->getName()) cmp lc(bless($_[2]->{$b}, "Name")->getName())} keys %{$_[2]})

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives