Wednesday, May 30, 2012

Writing CGI scripts with Fuzuli Programming Language

Fuzuli is also suitable for writing CGI (Common Gateway Interface) scripts. The logic underlying the CGI script is to define a content type for the document and print out the content using the standard input & output routines. Form elements posted by a client are generally handled using some environmental variables and standard input. Handling those variables varies according to the sending method of html forms.

Fuzuli has a different approach for CGI scripts. For example, in Php, a script file has a file extension of .php and it must be started with some html tags or a php starter

<?php

tag. While the file extension is .php, we have to declare that "we have finished writing html codes and now we are starting a new php area!". However, in Fuzuli, the default behavior is to write fuzuli code and writing html is an exception.

A fuzuli cgi script must define the location of the interpreter in the top level of the code:

#!/usr/bin/fuzuli

Secondly, before writing any output, content-type should be defined :


#!/usr/bin/fuzuli
(print "Content-type: text/html\n\n")


Ok. That is too much naive to CGI scripters. How to add html tags to this script? Follow the example below:

#!/usr/bin/fuzuli
(print "Content-type: text/html\n\n")
<?html

<h1>Hello World!</h1>

?>


Very nice, hah? So we can easily combine our logic and html! Ok lets do some html forms and handle them in Fuzuli.


#!/usr/lib/fuzuli/fuzuli
(print "Content-type: text/html\n\n")

<?html
   <form method="get">
   Enter your name:
   <input type="text" name="name">
   <input type="text" name="email">
   <input type="submit" value="Click">
</form>
?>

(let name (REQUEST "name"))
(let email (REQUEST "email"))

(if (isset name)
    (print "Posted name is : " name)
)

<?html
<h1>-----------------</h1>
?>

(if (isset email)
    (print "Posted email is: " email)
)



In the example above, the name and the email variables are sent from the html form. If they are set, their values are printed. Very simple. A Fuzuli loop can include an html block so one can generate html codes efficiently. Look at the code below:

#!/usr/lib/fuzuli/fuzuli

(print "Content-type: text/html\n\n")

<?html
<table border = "1">
?>

(let days 
  (list "sunday" 
        "monday" 
        "tuesday" 
        "wednesday"
        "thursday" 
        "friday" 
        "saturday"
   )
)
(for (let i 0) (< i (length days)) (inc i)
    (block
            <?html <tr><td><b> ?>

            (print i)

            <?html </b></td>

            <td><i>
            ?>

            (print (nth days i))


            <?html
                </b></td></tr>
            ?>
    )
)

<?html
</table>
?>      


Fuzuli supports cookie operations internally. setcookie and getcookie functions were implemented for setting and getting cookies, respectively. Note that cookies must be set before sending the content-type header.

setcookie (var "This is cookie value")
(let value (getcookie "var"))


The next step is to implement session functions. We are still discussing the session structure of Fuzuli. It seems it is the most convenient method to implement this functionality in Nafile (nfl) packages. Hope you like scripting with Fuzuli...

No comments:

Post a Comment

Thanks