Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Using Perl just a little in a Cocoa app

Using Perl just a little in a Cocoa app
Thread Tools
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
May 21, 2003, 09:13 PM
 
I have a cocoa app through and through, and after weeks of writing my own parser for html with NSScanner and NSAttributedString, I found that there are Perl modules which will do this.
My solution works, but I suspect that the mainstream one will be faster and more robust. Most importantly, my understanding is that Perl will be fast.
I've been boning up on PerlObjCBridge and CamelBones, so I think this is all possible, but I still can't find how to do this in terms I can understand.

My question is: How can I run a perl subroutine in a Cocoa app? I'd like to either make it a method in my object or an object I can message from other objects. Reading the documentation, it seems like it's right there on the edge but I can't find it. LOTS of stuff about writing perl apps that use cocoa, but not the other way around.

Thanks
you are not your signature
     
Gametes  (op)
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
May 22, 2003, 01:52 AM
 
Ok, progress and a new problem.
1) install the CamelBones framework.
2) download HTML:arser and HTML::Tagset from cpan.org, then install them via readme instructions, with root enabled.
3) include the necessary files and objects in your header file.

Here's how the code looks (all in a method in Controller):

Code:
CBPerl *perlObject = [[CBPerl alloc] init]; [perlObject useModule: @"HTML::LinkExtor"]; [perlObject eval: @"$parser = HTML::LinkExtor->new()"]; parser = [perlObject namedObject: @"parser"]; [parser parse:[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.apple.com/"]]]; array = [parser links];
The problem is, that last method returns a number, and not an array of links as it should. No getting around it. Any ideas?

Thanks
you are not your signature
     
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status: Offline
Reply With Quote
May 22, 2003, 01:35 PM
 
What is the type of array? I'm not very familiar with CamelBones, but a quick stroll through the documentation indicates that you should be using a CBPerlArray (take a look at CBPerlArray.h). Remember, if you access arrays in a scalar context in Perl, you will get a number (eg the size of the array) instead of the actual array. That might help you...
     
Gametes  (op)
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
May 22, 2003, 09:23 PM
 
Yes! That's exactly my problem! Do tell more! I'm getting the length of the array instead of the array itself. However, I found the CBPerlArray error earlier and after replacing my calls to NSMutableArray with it, the problem still persists.

Here's my upgraded code, which still isn't setting my array "downloads" to the proper value:

Code:
CBPerl *perlObject = [[CBPerl alloc] init]; CBPerlArray *downloads = [[CBPerlArray alloc] init]; [perlObject useModule: @"HTML::LinkExtor"]; [perlObject eval:@"$parser = HTML::LinkExtor->new();"]; CBPerlObject *parser = [perlObject namedObject:@"parser"]; [parser parse:@"http://www.apple.com";]; [perlObject eval:@"@links = $parser->links"]; downloads = [perlObject namedArray:@"links"
This is with or without the "@" indicator for the array.
I keep going over it and all the types seem correct. It's like the data just isn't making the leap from perl to obj-c. Of course, i'm just overlooking something. Can anybody debug that code to work in a standard foundation tool?

Thanks
you are not your signature
     
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status: Offline
Reply With Quote
May 23, 2003, 12:21 PM
 
Hmm, I don't have time to take a full look at this right now, but you might want to try replacing the last line with this:

Code:
downloads = [perlObject namedArray:@"\@links"]
In perl, \@array_name returns a reference to the array. Maybe that's what you need to do to make this work. If this doesn't work and I get a chance to take a more prolonged look at CamelBones, I'll see if I can find a better answer. Of course, this may just be a bug... Try e-mailing the developer.
(Last edited by itai195; May 23, 2003 at 12:29 PM. )
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:34 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2