docs.roxen.comView this page in a printer friendly mode
DocsPike7.0TutorialYour Second Pike Program
Copyright © 2012, Roxen Internet Software
Suggestions, comments & compliments
manuals@roxen.com
 DEMO  DOCS  PIKE
 COMMUNITY  DOWNLOAD
www.roxen.com

   

The Bare Bones
Running the Program
Command-line Arguments and if
Methods and Loops
Magic Web Stuff
Syntactic Sugar
Showing the Page
Some Notes About WWW

The Bare Bones

Your 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.
*/

Variables

The 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:

  • We create a variable with the name url. This is called to define a variable. The variable has the type string, which means that we an use it to store strings in. A string is a sequence of characters.

  • Stdio.stdin->gets() is used to let the user type some text on the keyboard. When the user hits the return key, the text is returned as a string. (Stdio.stdin->gets() calls the method gets in the object stdin from the module Stdio, but you don't need to worry about that just yet.)

  • We take that string, and store it in the variable. This is called assignment, or to assign a value to the variable.

  • In the last line with write, we add three strings together:

    "URL: " + url + "\n"

When we run the program, it may look something like this. The user's commands and input are shown in italics:

pike webbrowser.pike
Welcome to the Very Simple WWW Browser!
Type the address of the web page:
http://www.roxen.com/
URL: http://www.roxen.com/