|
|
|
The Bare BonesYour first Pike program, which was examined above in this tutorial, was a program that printed "Hello world!" on the screen. Your second Pike program will be a World Wide Web browser, just like Netscape or Internet Explorer. Well, perhaps not just like Netscape or Internet Explorer. Both of those are very large programs, and our browser will be a very small program. We will just make it advanced enough to connect to a World Wide Web server, download a web page, and then display it on the screen. We start by creating a file, for example called webbrowser.pike: // The Very Simple World Wide Web Browser int main() { write("Welcome to the Very Simple WWW Browser!\n"); return 0; } // main This is almost the same program as the "hello world" program. The only difference is that it prints something else, and that we have added some comments. Two "slash" characters (//) means that the rest of the line is a comment. Comments are ignored by Pike, and are intended for humans who read your program. Pike has another type of comments, which start with /* and end with */. Such comments can span several lines: /* This is also a comment. For example, you can "comment out" parts of your program with this type of comment, so Pike doesn't run those parts. */ VariablesThe web browser must be told which web page it should download and display. As you may know, the "addresses" to web pages are called URLs (Uniform Resource Locators), and they look like pike.idonex.se or http://pike.idonex.se/ (where the second form is correct, but the first one usually works too). We add some code that lets the user type a URL, stores that URL in a variable, and also prints it on the screen as confirmation. The new additions are in bold face: // The Very Simple World Wide Web Browser int main() { write("Welcome to the Very Simple WWW Browser!\n"); string url; write("Type the address of the web page:\n"); url = Stdio.stdin->gets(); write("URL: " + url + "\n"); return 0; } // main There are four new things here:
When we run the program, it may look something like this. The user's commands and input are shown in italics:
|
||||||||||||||||||||