Week 9 - Perl & Lingo Get together

NetLingo, Perl text parsing, Communicating with your own scripts, Communicating with other scripts, Manipulating Director text

NetLingo

Director has some specific commands for the purpose of communicating over the internet.
To communicate with CGI script:
getNetText("http://stage.itp.tsoa.nyu.edu/~eqd6730/cgi/communicatedir.cgi?stuff=hello")

Same as above, but uses a property list to submit the CGI query, and does the URL encoding for you:
getNetText("http://stage.itp.tsoa.nyu.edu/~eqd6730/cgi/communicatedir.cgi, [#stuff:"hello"]))

So this is going on in the background. And you need to have a way to figure out when it is done.
How to do this?
getNetText actually returns a number by which you can keep track of your net query.
You do this by putting it into a variable:
netID = getNetText("http://stage.itp.tsoa.nyu.edu/~eqd6730/cgi/communicatedir.cgi, [#stuff:"hello"]))

You then check if it's done in the frame script by using the command:
netdone(netID)
This returns 0 if the net action is not done yet and 1 if it is done.

You can access what the cgi script sends back using the command:
netTextResult(netID)
member("from cgi").text = netTextResult(netID)

So, your frame script will probably look something like this:
if netdone(netID) then
    member("from cgi").text = netTextResult(netID)
end if


Also, you probably want to set your netID back to some default value once
your query has been answered, so that you aren't checking for netdone
unless there is actually a query that has been sent.

on beginsprite
    netID = -999
end
if netID <> -999 then
    if netdone(netID) then
        member("from cgi").text = netTextResult(netID)
        netID = -999
    end if
end if

For error checking, use the command:
netError(netID)
if netError(theNetID) = "OK" then

Also check out: preloadNetThing()

Some Perl stuff

Perl is particularly good at dealing with text:

The all-important split function
$line = "merlyn::118:10:Randal:/home/merlyn:/usr/bin/perl";
@fields = split(/:/,$line); # split $line, using : as delimiter
# now @fields is ("merlyn","","118","10","Randal",
# "/home/merlyn","/usr/bin/perl")

For lots more, see the Regular Expressions chapter in the Learning Perl book.

If you want to print characters that are part of the perl
language, you have to "escape" them, so if you want
to print

this is what it should look like in perl:
print "";

Communicating with your own CGI script

The only thing to say about this is that since you are writing the CGI script,
you are in control of both sides - and can be very specific about your formatting, etc.

Communicating with other CGI scripts

Doing this can be really fun - since you are "borrowing" from the collective
wisdom of the web.

General idea:
1. Find a cgi script that does something useful or fun..
2. Figure out the parameters it is sending by looking closely at the URL.
(You are effectively "reverse-engineering" the script...)
3. Test by sending in your own parameters.
4. Then parse out all the html, etc.

1. http://dictionary.reference.com/search?q=upset
This tells me that "search" is the name of the script
And the parameters it's sending here are "q=upset"

So in my netlingo:
param = [#q:member("word from user").text]
thenetid = getnettext("http://dictionary.reference.com/search", param)


Manipulating text in Director, Part 2

Useful commands for getting rid of extraneous html:

offset(stringExpression1, stringExpression2)
Tells you what character number a string starts at.

leftTagOffset = offset("<", member("from web dictionary").text)
rightTagOffset = offset(">", member("from web dictionary").text)

Tells you what character number the tag starts at and ends at

delete member("from web dictionary").char[leftTagOffset..rightTagOffset]
deletes the characters from leftTagOffset to rightTagOffset

member("from web dictionary").text.length
Tells you how many characters all together - useful if you want to get rid of things up to the end.

delete member("from web dictionary").char[6000..sprite(2).member.text.length]
Deletes from character 6000 to the end of the text field

Also see the SearchAndReplace handler in Dictionary.Dir example:
on SearchAndReplace(input, stringToFind, stringToInsert)
   blah blah
end