 |
 |
Using Perl just a little in a Cocoa app
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status:
Offline
|
|
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
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status:
Offline
|
|
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
|
|
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...
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status:
Offline
|
|
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
|
|
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.
)
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|