<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://classes.engineering.wustl.edu/cse330/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Wiseman</id>
	<title>CSE330 Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="http://classes.engineering.wustl.edu/cse330/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Wiseman"/>
	<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Special:Contributions/Wiseman"/>
	<updated>2026-11-15T21:32:42Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.34.4</generator>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Firefox_Extensions&amp;diff=136</id>
		<title>Firefox Extensions</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Firefox_Extensions&amp;diff=136"/>
		<updated>2009-11-18T20:18:50Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=XUL=&lt;br /&gt;
XUL (XML User-interface Language) is a markup language that describes the user interface. It is at the heart of Mozilla based browsers. Through XUL, you can defined input controls, toolbars, menus, dialogs, keyboard shortcuts and many more.  XUL could be used for Mozilla/Firefox extensions or stand-alone applications. In fact, Firefox's interface is written in XUL. If the security permissions are enabled, you can also use XUL to remotely modify the interface of the browser.&lt;br /&gt;
&lt;br /&gt;
You can access a XUL file just using regular URL. An alternative is to package several related files to a chrome package. There are two ways to create chrome packages, a JAR file and a chrome directory. The jar file is basically a ZIP file with chrome directory structure. Alternatively, you can directly put the chrome directory inside your browser's profile directory. Either way, chrome packages usually have three type of data: ''content'' (main windows, scripts etc.), ''skin'' (theme related files such as images, css etc.),, ''locale'' (local language related files).&lt;br /&gt;
&lt;br /&gt;
If you have installed a chrome packages, you can access its XUL files directly using Firefox specific protocol (chrome://). For example, since  Firefox user-interface is written in XUL, you can access its xul files using chrome:// protocol.&lt;br /&gt;
The typical format is &lt;br /&gt;
 chrome://&amp;lt;package name&amp;gt;/&amp;lt;part&amp;gt;/&amp;lt;file.xul&amp;gt; &lt;br /&gt;
&lt;br /&gt;
For example,&lt;br /&gt;
 chrome://browser/content/browser.xul&lt;br /&gt;
&lt;br /&gt;
will show you the main Firefox user-interface.&lt;br /&gt;
&lt;br /&gt;
== Hello World in XUL ==&lt;br /&gt;
&lt;br /&gt;
The following code displays ''Hello World'' message on the browser:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;?xml-stylesheet href=&amp;quot;chrome://global/skin/&amp;quot; type=&amp;quot;text/css&amp;quot;?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;window&lt;br /&gt;
    id=&amp;quot;helloworld-window&amp;quot;&lt;br /&gt;
    title=&amp;quot;Hello  World&amp;quot;&lt;br /&gt;
    orient=&amp;quot;horizontal&amp;quot;&lt;br /&gt;
    xmlns=&amp;quot;http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;label value=&amp;quot;Hello World!!! &amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/window&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this code, the first two lines specify that this is an XML file and it uses a stylesheet. Then XML file specifies a ''window'' element. This will be the main container. Its attributes contain an id and title as well as its orientation. Also there is a specifial attribute&lt;br /&gt;
&lt;br /&gt;
  xmlns=&amp;quot;http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a special attribute that informs the browser all its children are XUL.&lt;br /&gt;
&lt;br /&gt;
This windows only child is a ''label'' with the value ''Hello World!!!''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have the above XUL file, you can load it in your firefox and observe its behavior.&lt;br /&gt;
&lt;br /&gt;
== XUL  Packages ==&lt;br /&gt;
&lt;br /&gt;
The new file are added to they system using chrome packages. Packages are basically list of XUL files and supplementary material.  Once a package is installed, the files can be accessed using ''chrome://'' protocol. Firefox becomes aware of installed packages through ''[http://developer.mozilla.org/en/docs/Chrome_Manifest manifest]'' files.   The purpose of these files is to map the protocol access  to  the  actual locations in the physcical disk. While Mozilla reads all manifest files under chrome directory, Firefox  reads only extentions/*/chrome.manifest names. Manifest files have the keywords content, skin and locale for each type.&lt;br /&gt;
 content package-name file-path&lt;br /&gt;
 &lt;br /&gt;
 skin package-name skin-name file-path&lt;br /&gt;
&lt;br /&gt;
 locale package-name skin-name file-path&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
file-path could be a physicall directory ''file:///path-name-here'', or  a JAR file, ''jar:&amp;lt;filename.jar&amp;gt;!/&amp;lt;path_in_archive&amp;gt;''.&lt;br /&gt;
&lt;br /&gt;
===Installing XUL Packages===&lt;br /&gt;
&lt;br /&gt;
Both XUL installation and extension installation uses the same technique. You first need to create an XPI file. This is basically a zip file with some specific files inside.  Lets see an example installation of hello.xul (above)&lt;br /&gt;
&lt;br /&gt;
In order to install hello.xul, we will install a dummy extension. First, create a directory  (lets call it helloworld) and edit a new manifest file&lt;br /&gt;
&lt;br /&gt;
helloworld/chrome.manifest&lt;br /&gt;
  content helloworld chrome/&lt;br /&gt;
&lt;br /&gt;
This tells Firefox that the package helloworld has its contents under ''chrome'' directory. Next create this directory under helloworld and copy your hello.xul there.&lt;br /&gt;
Finally, create ''[http://kb.mozillazine.org/Install.rdf install.rdf]'' file under helloworld directory. &lt;br /&gt;
&lt;br /&gt;
''install.rdf'' is a specifal file that describes your extension. In the very basic format, you need at least the following:&lt;br /&gt;
&lt;br /&gt;
install.rdf&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;RDF xmlns=&amp;quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;quot;&lt;br /&gt;
     xmlns:em=&amp;quot;http://www.mozilla.org/2004/em-rdf#&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        &amp;lt;Description about=&amp;quot;urn:mozilla:install-manifest&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
                &amp;lt;em:id&amp;gt;{4f212b04-8063-11dc-8314-0800200c9a66}&amp;lt;/em:id&amp;gt;&lt;br /&gt;
                &amp;lt;em:name&amp;gt;Hello World &amp;lt;/em:name&amp;gt;&lt;br /&gt;
                &amp;lt;em:version&amp;gt;1.0&amp;lt;/em:version&amp;gt;&lt;br /&gt;
                &amp;lt;em:description&amp;gt;This is my hellow world&amp;lt;/em:description&amp;gt;&lt;br /&gt;
                &amp;lt;em:creator&amp;gt;Burchan&amp;lt;/em:creator&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
                &amp;lt;!-- Firefox --&amp;gt;&lt;br /&gt;
                &amp;lt;em:targetApplication&amp;gt;&lt;br /&gt;
                        &amp;lt;Description&amp;gt;&lt;br /&gt;
                                &amp;lt;em:id&amp;gt;{ec8030f7-c20a-464f-9b0e-13a3a9e97384}&amp;lt;/em:id&amp;gt;&lt;br /&gt;
                                &amp;lt;em:minVersion&amp;gt;0.9&amp;lt;/em:minVersion&amp;gt;&lt;br /&gt;
                                &amp;lt;em:maxVersion&amp;gt;3.0&amp;lt;/em:maxVersion&amp;gt;&lt;br /&gt;
                        &amp;lt;/Description&amp;gt;&lt;br /&gt;
                &amp;lt;/em:targetApplication&amp;gt;&lt;br /&gt;
&lt;br /&gt;
        &amp;lt;/Description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/RDF&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 ''&amp;lt;nowiki&amp;gt;&amp;lt;em:id&amp;gt;&amp;lt;/nowiki&amp;gt;'' describes the unique id for your extension. There are several ID generators available. You can use a web based one, such as [http://www.famkruithof.net/uuid/uuidgen]. ''&amp;lt;nowiki&amp;gt;&amp;lt;em:name&amp;gt;&amp;lt;/nowiki&amp;gt;'', ''&amp;lt;nowiki&amp;gt;&amp;lt;em:version&amp;gt;&amp;lt;/nowiki&amp;gt;'', ''&amp;lt;nowiki&amp;gt;&amp;lt;em:description&amp;gt;&amp;lt;/nowiki&amp;gt;'',&lt;br /&gt;
''&amp;lt;nowiki&amp;gt;&amp;lt;em:description&amp;gt;&amp;lt;/nowiki&amp;gt;'',''&amp;lt;nowiki&amp;gt;&amp;lt;em:creator&amp;gt;&amp;lt;/nowiki&amp;gt;'' are straight forward.&lt;br /&gt;
 ''&amp;lt;nowiki&amp;gt;&amp;lt;em:targetApplication&amp;gt;&amp;lt;/nowiki&amp;gt;'' describes which applications can use this extension, all Mozilla.org tools have their unique ids.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
At this step, your should have the following directory structure.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
helloworld&lt;br /&gt;
|-- chrome&lt;br /&gt;
|   `-- hello.xul&lt;br /&gt;
|-- chrome.manifest&lt;br /&gt;
`-- install.rdf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside helloworld directory, create a new zip file:&lt;br /&gt;
 zip -r ../helloworld.xpi *&lt;br /&gt;
&lt;br /&gt;
and open this file in Firefox. This will install your new extenstion. Now, you can reach your hello.xul with&lt;br /&gt;
 chrome://helloworld/content/hello.xul&lt;br /&gt;
===Modifying Firefox ===&lt;br /&gt;
&lt;br /&gt;
''chrome'' command ''overlay'' overwrites XUL pages with other XULs. The format is&lt;br /&gt;
 overlay chrome://first-package  chrome://overwrite-the-first-package&lt;br /&gt;
&lt;br /&gt;
You can use this command to overwrite Firefox XUL files. For example,&lt;br /&gt;
 content news_blogger chrome/content/&lt;br /&gt;
 overlay chrome://browser/content/browser.xul chrome://news_blogger/content/news_blogger.xul&lt;br /&gt;
&lt;br /&gt;
first defines a new chrome for ''news_blogger'' and then let its definitions replace existing firefox definitions. This way you can define new sidebars, toolboxes or context menus.&lt;br /&gt;
&lt;br /&gt;
====Toolbox====&lt;br /&gt;
&lt;br /&gt;
For toolboxes,  you can use &amp;amp;lt;toolbox element. After toolbox, you can define a toolbar in which you can put other items.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;amp;lt;overlay id=&amp;quot;picture-overlay&amp;quot;   xmlns=&amp;quot;http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;lt;toolbox id=&amp;quot;navigator-toolbox&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;lt;toolbar id=&amp;quot;picture-toolbar&amp;quot; accesskey=&amp;quot;T&amp;quot; class=&amp;quot;chromeclass-toolbar&amp;quot; context=&amp;quot;toolbar-context-menu&amp;quot; toolbarname=&amp;quot;picture toolbar&amp;quot; hidden=&amp;quot;false&amp;quot; persist=&amp;quot;hidden&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;lt;label id=&amp;quot;picture-url&amp;quot; value=&amp;quot;this is a test&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/toolbar&amp;gt;&lt;br /&gt;
&amp;amp;lt;/toolbox&amp;gt;&lt;br /&gt;
&amp;amp;lt;/overlay&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Sidebar====&lt;br /&gt;
&lt;br /&gt;
You can define a sidebar using a second XUL file. The first XUL defines a menu item. In this case, it is attached to the View→Sidebar menu using the viewSidebarMenuid, which opens or closes ''News Blogger'' in the sidebar when activated. The menu item is then hooked up to a broadcaster via the observes attribute. The new broadcaster for the sidebar is overlaid into Firefox's main broadcaster set. The menu-item attributes are abstracted to the overlay simply to allow it to be used in more than one place.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;overlay id=&amp;quot;news_bloggerOverlay&amp;quot;&lt;br /&gt;
         xmlns=&amp;quot;http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;menupopup id=&amp;quot;viewSidebarMenu&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;menuitem key=&amp;quot;key_NewsSidebar&amp;quot; observes=&amp;quot;viewNewsSidebar&amp;quot;  /&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/menupopup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;broadcasterset id=&amp;quot;mainBroadcasterSet&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;broadcaster id=&amp;quot;viewNewsSidebar&amp;quot;&lt;br /&gt;
                 &lt;br /&gt;
                 autoCheck=&amp;quot;false&amp;quot;&lt;br /&gt;
                 type=&amp;quot;checkbox&amp;quot;&lt;br /&gt;
                 group=&amp;quot;sidebar&amp;quot;&lt;br /&gt;
                 sidebarurl=&amp;quot;chrome://news_blogger/content/sidebar.xul&amp;quot;&lt;br /&gt;
                 sidebartitle=&amp;quot;News Blogger&amp;quot;&lt;br /&gt;
                 oncommand=&amp;quot;toggleSidebar('viewNewsSidebar');&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/broadcasterset&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/overlay&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then in the second XUL, you can define the sidebar the way you want.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;page id=&amp;quot;sbEmptySidebar&amp;quot; title=&amp;quot;News Blogger;&amp;quot;&lt;br /&gt;
         xmlns=&amp;quot;http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul&amp;quot;&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
...... your XUL elements are here......&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/page&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Context Menu Changes====&lt;br /&gt;
You can add new context menu (right mouse button) items using ''overlay''&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;overlay id=&amp;quot;news_bloggerOverlay&amp;quot;&lt;br /&gt;
         xmlns=&amp;quot;http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;amp;lt;popup id=&amp;quot;contentAreaContextMenu&amp;quot; insertafter=&amp;quot;context-searchselect&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;amp;lt;menuseparator id=&amp;quot;menuseperator&amp;quot; /&amp;gt;&lt;br /&gt;
      &amp;amp;lt;menuitem id=&amp;quot;add_article&amp;quot; label=&amp;quot;Add Article&amp;quot; oncommand=&amp;quot;AddArticle(event); event.preventBubble()&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;amp;lt;menuitem id=&amp;quot;add_picture&amp;quot; label=&amp;quot;Add Picture &amp;quot; oncommand=&amp;quot;AddPicture(event); event.preventBubble()&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;/popup&amp;gt;&lt;br /&gt;
&amp;amp;lt;/overlay&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This adds two new menu items just after  a firefox defined item (context-searchselect). It also sets the function to be called when these items are selected.&lt;br /&gt;
&lt;br /&gt;
==Adding Javascript==&lt;br /&gt;
&lt;br /&gt;
You can add javascript after within ''window'',''overlay'' or ''page'' elements. You can also refer to an existing file.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;script type=&amp;quot;application/x-javascript&amp;quot; src=&amp;quot;chrome://news_blogger/content/news_blogger.js&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;overlay id=&amp;quot;news_bloggerOverlay&amp;quot;&lt;br /&gt;
         xmlns=&amp;quot;http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;application/x-javascript&amp;quot; src=&amp;quot;chrome://news_blogger/content/news_blogger.js&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
......&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
==Adding HTML Elements==&lt;br /&gt;
If you want to use regular HTML elements, you need to include XHTML definition in your root tag.&lt;br /&gt;
 &amp;amp;lt;window id=&amp;quot;topWindow&amp;quot;  xmlns=&amp;quot;http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul&amp;quot;&lt;br /&gt;
        xmlns:html=&amp;quot;http://www.w3.org/1999/xhtml&amp;quot; &amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have included XHTML definitions, then you can put regular HTML elements using &amp;amp;lt;html:TAG&amp;gt; and &amp;amp;lt;/html:TAG&amp;gt;. For example, you can define an ''input'' field.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html:input type=&amp;quot;text&amp;quot; name=&amp;quot;myinput&amp;quot;/&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Links=&lt;br /&gt;
[http://www.hevanet.com/acorbin/xul/top.xul Example set of XUL elements]&lt;br /&gt;
&lt;br /&gt;
[http://www.xulplanet.com/references/elemref/  XUL Element References]&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=CSE_330_Online_Textbook_-_Table_of_Contents&amp;diff=135</id>
		<title>CSE 330 Online Textbook - Table of Contents</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=CSE_330_Online_Textbook_-_Table_of_Contents&amp;diff=135"/>
		<updated>2009-11-13T03:33:25Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Module 1: [[System Administration]]&lt;br /&gt;
&lt;br /&gt;
Module 2: [[PHP]]&lt;br /&gt;
&lt;br /&gt;
Module 3: [[Databases and MySQL]]&lt;br /&gt;
&lt;br /&gt;
Module 4: [[Javascript and AJAX]]&lt;br /&gt;
&lt;br /&gt;
Module 5: Creative Programming 1&lt;br /&gt;
&lt;br /&gt;
Module 6: [[Perl]]&lt;br /&gt;
&lt;br /&gt;
Module 7: [[Remote Procedure Calls]]&lt;br /&gt;
&lt;br /&gt;
Module 8: Creative Programming 2&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=CSE_330_Online_Textbook_-_Table_of_Contents&amp;diff=134</id>
		<title>CSE 330 Online Textbook - Table of Contents</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=CSE_330_Online_Textbook_-_Table_of_Contents&amp;diff=134"/>
		<updated>2009-11-13T03:30:23Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Module 1: [[System Administration]]&lt;br /&gt;
&lt;br /&gt;
Module 2: [[PHP]]&lt;br /&gt;
&lt;br /&gt;
Module 3: [[Databases and MySQL]]&lt;br /&gt;
&lt;br /&gt;
Module 4: [[Javascript and AJAX]]&lt;br /&gt;
&lt;br /&gt;
Module 5: Creative Programming 1&lt;br /&gt;
&lt;br /&gt;
Module 6: [[Perl]]&lt;br /&gt;
&lt;br /&gt;
Module 7: [[Remote Procedure Calls]]&lt;br /&gt;
&lt;br /&gt;
Module 8: Creative Programming 2&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Other Information:&lt;br /&gt;
&lt;br /&gt;
Project Ideas&lt;br /&gt;
&lt;br /&gt;
Firefox Extensions&lt;br /&gt;
&lt;br /&gt;
Content Management Systems&lt;br /&gt;
&lt;br /&gt;
Graphics Tools&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Remote_Procedure_Calls&amp;diff=133</id>
		<title>Remote Procedure Calls</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Remote_Procedure_Calls&amp;diff=133"/>
		<updated>2009-11-11T03:58:42Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Perl Frontier  Module */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=RPC=&lt;br /&gt;
Remote Procedure Calls (RPC) is a general computing paradigm for invoking procedures on one computer from another.  There are many forms of RPC technology, and we will focus on XML-RPC.&lt;br /&gt;
&lt;br /&gt;
=XML-RPC=&lt;br /&gt;
XML-RPC is an RPC protocol that uses XML encoding of RPC requests and responses over HTTP.&lt;br /&gt;
&lt;br /&gt;
==Calling Remote Procedures ==&lt;br /&gt;
&lt;br /&gt;
The calling procedure requests invokation of a function on a remote host using XML. A typical XML call looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;amp;lt;methodCall&amp;gt;&lt;br /&gt;
  &amp;amp;lt;methodName&amp;gt;FunctionName&amp;amp;lt;/methodName&amp;gt;&lt;br /&gt;
  &amp;amp;lt;params&amp;gt;&lt;br /&gt;
    &amp;amp;lt;param&amp;gt;&lt;br /&gt;
        &amp;amp;lt;value&amp;gt;&amp;amp;lt;DATATYPE&amp;gt;DATAVALUE&amp;amp;lt;/DATATYPE&amp;gt;&amp;amp;lt;/value&amp;gt;&lt;br /&gt;
    &amp;amp;lt;/param&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/params&amp;gt;&lt;br /&gt;
&amp;amp;lt;/methodCall&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The server invokes the function with the specified parameters then returns a response like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;amp;lt;methodResponse&amp;gt;&lt;br /&gt;
  &amp;amp;lt;params&amp;gt;&lt;br /&gt;
    &amp;amp;lt;param&amp;gt;&lt;br /&gt;
        &amp;amp;lt;value&amp;gt;&amp;amp;lt;DATATYPE&amp;gt;VALUE&amp;amp;lt;/DATATYPE&amp;gt;&amp;amp;lt;/value&amp;gt;&lt;br /&gt;
    &amp;amp;lt;/param&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/params&amp;gt;&lt;br /&gt;
&amp;amp;lt;/methodResponse&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Types==&lt;br /&gt;
&lt;br /&gt;
XML-RPC is generalized enough to transmit most any kind of data. The data types are specified like&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;DATATYPE&amp;gt;VALUE&amp;amp;lt;/DATATYPE&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The types include ''boolean'', ''dateTime'',''double'',''int'',''string''. Binary data has to be encoded at the sender in BASE-64 and decoded at the end by the receiver.  The encoded binary data is sent in a ''base64'' tag.  It also provides ''arrays'' and ''structs''.&lt;br /&gt;
&lt;br /&gt;
==XML-RPC Implementations ==&lt;br /&gt;
In order to use XML-RPC, you need to have an XML-RPC library installed for your platform and language. There are several available libraries, depending on your needs. You can find the list at http://www.xmlrpc.com/directory/1568/implementations.&lt;br /&gt;
&lt;br /&gt;
===XML-RPC for PHP(PHPXMLRPC)===&lt;br /&gt;
PHPXMLRPC is one of the several XML-RPC libraries for PHP. It is accessible at http://phpxmlrpc.sourceforge.net/&lt;br /&gt;
&lt;br /&gt;
In order to use this library, you need to download and unzip the package.  A handful of .inc files are included in the distribution for PHP clients and servers.  Include them in your PHP scripts to get access to the functionality in the library.  For example, xmlrpc.inc is the client library and xmlrpcs.inc is the server library.  It also provides several helper and wrapper functions that require other include files. You can look at the documentation to learn more about them. For this module, we will concentrate on client functionality.&lt;br /&gt;
&lt;br /&gt;
==== Creating a client in PHP ====&lt;br /&gt;
''xmlrpc_client'' is the XML-RPC client class. You can create a new client using&lt;br /&gt;
&lt;br /&gt;
 $client = new xmlrpc_client( server_url )&lt;br /&gt;
&lt;br /&gt;
It also supports basic HTTP authentication &lt;br /&gt;
&lt;br /&gt;
 $client=(  http(s)://user:password@server_url:port/path)&lt;br /&gt;
&lt;br /&gt;
For example, the following code creates a connection to the PHPXMLRPC demo server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?&lt;br /&gt;
include(&amp;quot;xmlrpc.inc&amp;quot;);&lt;br /&gt;
$client = new xmlrpc_client(&amp;quot;http://phpxmlrpc.sourceforge.net/server.php&amp;quot;);&lt;br /&gt;
?&amp;amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====PHPXMLRPC variables ====&lt;br /&gt;
The parameters(to or from remote procedures) are specified using the ''xmlrpcval'' function. This function can be called in different ways:&lt;br /&gt;
&lt;br /&gt;
 new xmlrpcval ( )&lt;br /&gt;
 new xmlrpcval ( string )&lt;br /&gt;
 new xmlrpcval ( VALUE, TYPE )&lt;br /&gt;
 new xmlrpcval ( ARRAY, ARRAY_TYPE )&lt;br /&gt;
&lt;br /&gt;
If ''xmlrpcval'' is called without a parameter, you need to set the variables by calling the ''addScalar'', ''addArray'' or ''addStruct'' functions to initialize the variable. If ''xmlrpcval'' is called with a string, the variable is assumed to be of type string, and the value is specified in the string parameter. A third option is to pass the value and type of a variable.  The fourth option is to create complex variables of arrays and structures.&lt;br /&gt;
&lt;br /&gt;
 $myInt = new xmlrpcvalue(1267, &amp;quot;int&amp;quot;);&lt;br /&gt;
 $myString = new xmlrpcvalue(&amp;quot;Hello, World!&amp;quot;, &amp;quot;string&amp;quot;);&lt;br /&gt;
 $myBool = new xmlrpcvalue(1, &amp;quot;boolean&amp;quot;);&lt;br /&gt;
 // note: this will serialize a php float value as xml-rpc string&lt;br /&gt;
 $myString2 = new xmlrpcvalue(1.24, &amp;quot;string&amp;quot;);&lt;br /&gt;
 $myArray = new xmlrpcval(&lt;br /&gt;
  array(&lt;br /&gt;
    new xmlrpcval(&amp;quot;Tom&amp;quot;),&lt;br /&gt;
    new xmlrpcval(&amp;quot;Dick&amp;quot;),&lt;br /&gt;
    new xmlrpcval(&amp;quot;Harry&amp;quot;)&lt;br /&gt;
  ),&lt;br /&gt;
  &amp;quot;array&amp;quot;);&lt;br /&gt;
 // recursive struct&lt;br /&gt;
 $myStruct = new xmlrpcval(&lt;br /&gt;
  array(&lt;br /&gt;
    &amp;quot;name&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;Tom&amp;quot;, &amp;quot;string&amp;quot;),&lt;br /&gt;
    &amp;quot;age&amp;quot; =&amp;gt; new xmlrpcval(34, &amp;quot;int&amp;quot;),&lt;br /&gt;
    &amp;quot;address&amp;quot; =&amp;gt; new xmlrpcval(&lt;br /&gt;
      array(&lt;br /&gt;
        &amp;quot;street&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;Fifht Ave&amp;quot;, &amp;quot;string&amp;quot;),&lt;br /&gt;
        &amp;quot;city&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;NY&amp;quot;, &amp;quot;string&amp;quot;)&lt;br /&gt;
      ), &lt;br /&gt;
      &amp;quot;struct&amp;quot;)&lt;br /&gt;
  ), &lt;br /&gt;
  &amp;quot;struct&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Once you have your variables ready, you can send them to the remote server.&lt;br /&gt;
&lt;br /&gt;
==== Preparing a message ====&lt;br /&gt;
&lt;br /&gt;
In order to make an RPC, first you need to prepare a message. This is done through the ''xmlrpcmsg'' class.&lt;br /&gt;
&lt;br /&gt;
 $message = new xmlrpcmsg(&amp;quot;REMOTE_FUNCTION&amp;quot;, PARAMETER_ARRAY);&lt;br /&gt;
&lt;br /&gt;
Remote function is the name of the function on the remote machine. Parameter array stores the parameters formed above. It can be skipped if the function does not take any parameters.&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code sends a message to the demo server to call an existing function:&lt;br /&gt;
&lt;br /&gt;
 $myParams=array(new xmlrpcval(23, &amp;quot;int&amp;quot;));&lt;br /&gt;
 $msg = new xmlrpcmsg(&amp;quot;examples.getStateName&amp;quot;, $myParams);&lt;br /&gt;
&lt;br /&gt;
==== Calling the function and receiving a response ====&lt;br /&gt;
The actual RPC is done when the client's ''send'' function is called.&lt;br /&gt;
&lt;br /&gt;
 $client-&amp;gt;return_type = 'phpvals';&lt;br /&gt;
 $resp = $client-&amp;gt;send($msg);&lt;br /&gt;
 if($resp-&amp;gt;faultCode())&lt;br /&gt;
   echo 'Error: ' . $resp-&amp;gt;faultString();&lt;br /&gt;
 else&lt;br /&gt;
   echo 'OK: got ' . $resp-&amp;gt;value();&lt;br /&gt;
&lt;br /&gt;
The client's return_type specifies either that the result should be stored in XML or as PHP variables. If there was an error during the call, the response's error code will be set. Otherwise, the value function returns the response value.&lt;br /&gt;
&lt;br /&gt;
===Perl Frontier  Module ===&lt;br /&gt;
Perl has a few different modules for XML-RPC.  We will use the Frontier::* modules, focusing on Frontier::Daemon which provides XML-RPC server functionality.&lt;br /&gt;
&lt;br /&gt;
You can initialize the daemon with the ''new'' function. The arguments include the port number to listen on and function names to serve to clients. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use Frontier::Daemon;&lt;br /&gt;
&lt;br /&gt;
$methods = { 'myservice.convert' =&amp;gt; \&amp;amp;convert };&lt;br /&gt;
&lt;br /&gt;
Frontier::Daemon-&amp;gt;new(LocalPort =&amp;gt; 4873,  methods =&amp;gt; $methods) or die &amp;quot;Couldn't start HTTP server: $!&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will set up the daemon at port 4873 and the function ''convert'' is called from the remote clients as ''myservice.convert''.&lt;br /&gt;
In the following example, the daemon receives a Base64 encoded image, decodes, saves, and modifies it, and then sends the modified image back in Base64. Note that it assumes just single user is using the system at any time.  If multiple users access the same server the files could be overwritten.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use MIME::Base64;&lt;br /&gt;
use Frontier::Daemon;&lt;br /&gt;
use Frontier::RPC2;&lt;br /&gt;
&lt;br /&gt;
$|=1;&lt;br /&gt;
print &amp;quot;server $$&amp;quot;;&lt;br /&gt;
$methods = { 'myservice.convert' =&amp;gt; \&amp;amp;convert };&lt;br /&gt;
&lt;br /&gt;
#initialize daemon&lt;br /&gt;
Frontier::Daemon-&amp;gt;new(LocalPort =&amp;gt; 4873,  methods =&amp;gt; $methods, ReuseAddr=&amp;gt;1) or die &amp;quot;Couldn't start HTTP server: $!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
sub convert&lt;br /&gt;
{&lt;br /&gt;
  ($name,$encoded) = @_; #client sends filename and file in base64&lt;br /&gt;
  print &amp;quot;Name:$name\n\n\n\n&amp;quot;;&lt;br /&gt;
  $decoded = MIME::Base64::decode($encoded);&lt;br /&gt;
  open(OUT,&amp;quot;&amp;gt;/tmp/$name&amp;quot;); #save the image&lt;br /&gt;
  print OUT $decoded;&lt;br /&gt;
  close(OUT);&lt;br /&gt;
  &lt;br /&gt;
  #put a label over the image&lt;br /&gt;
  system(&amp;quot;convert -pointsize 40 -draw \&amp;quot;text 200,200 'cse330'\&amp;quot; /tmp/$name /tmp/a2.jpg&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
  #read back the image&lt;br /&gt;
  open(IN,&amp;quot;&amp;lt;/tmp/a2.jpg&amp;quot;);&lt;br /&gt;
  binmode(IN);&lt;br /&gt;
  read IN,$de,1000000;&lt;br /&gt;
  close(OUT);&lt;br /&gt;
    &lt;br /&gt;
  #encode it&lt;br /&gt;
  $encoded = MIME::Base64::encode($de);&lt;br /&gt;
  #print $encoded;&lt;br /&gt;
  &lt;br /&gt;
  #send it back&lt;br /&gt;
  return {'success' =&amp;gt; &amp;quot;true&amp;quot;,'result' =&amp;gt; $encoded};&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the client side, you may have an upload page:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
  &amp;amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;image-xmlrpc.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
  Choose an image  file to upload: &amp;amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;amp;lt;br /&amp;gt;&lt;br /&gt;
  &amp;amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;amp;lt/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, a PHP based client that would send the uploaded image to the RPC server, and display the modified image.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?&lt;br /&gt;
  $uploadfile = $_FILES['uploadedfile']['tmp_name'];&lt;br /&gt;
  $handle = fopen($uploadfile,'r');&lt;br /&gt;
  $file_content = fread($handle,filesize($uploadfile)); //read uploaded file&lt;br /&gt;
  fclose($handle);&lt;br /&gt;
  $encoded = chunk_split(base64_encode($file_content)); //encode it&lt;br /&gt;
  include(&amp;quot;xmlrpc.inc&amp;quot;); //for xmlrpc operations&lt;br /&gt;
&lt;br /&gt;
  $client = new xmlrpc_client(&amp;quot;http://127.0.0.1:4873/RPC2&amp;quot;); //your host&lt;br /&gt;
 &lt;br /&gt;
  $myParams = array(new xmlrpcval($_FILES['uploadedfile']['name'], &amp;quot;string&amp;quot;),&lt;br /&gt;
                    new xmlrpcval($encoded, &amp;quot;string&amp;quot;));&lt;br /&gt;
 &lt;br /&gt;
  $message = new xmlrpcmsg(&amp;quot;myservice.convert&amp;quot;, $myParams);&lt;br /&gt;
  $resp = $client-&amp;gt;send($message);&lt;br /&gt;
  if ($resp-&amp;gt;faultCode())&lt;br /&gt;
  {&lt;br /&gt;
    echo 'KO. Error: '.$resp-&amp;gt;faultString();&lt;br /&gt;
  }&lt;br /&gt;
  else&lt;br /&gt;
  {&lt;br /&gt;
    $struct=$resp-&amp;gt;value();&lt;br /&gt;
    $retvalue = $struct-&amp;gt;structmem('success'); //value of the field success&lt;br /&gt;
    $resultval = $struct-&amp;gt;structmem('result'); //result field&lt;br /&gt;
    $result = $resultval-&amp;gt;scalarval(); //encoded image from the result&lt;br /&gt;
&lt;br /&gt;
    $decoded = base64_decode($result); //decode the base64 data&lt;br /&gt;
    $handle = fopen(&amp;quot;changed.jpg&amp;quot;,'w'); //save it to a new file&lt;br /&gt;
    $file_content = fwrite($handle,$decoded);&lt;br /&gt;
    fclose($handle);&lt;br /&gt;
&lt;br /&gt;
    echo &amp;quot;&amp;lt;img src='changed.jpg'/&amp;gt;&amp;lt;br&amp;gt;&amp;quot;; //display the new image&lt;br /&gt;
  }&lt;br /&gt;
?&amp;amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Remote_Procedure_Calls&amp;diff=132</id>
		<title>Remote Procedure Calls</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Remote_Procedure_Calls&amp;diff=132"/>
		<updated>2009-11-11T03:39:21Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* XML-RPC for PHP(PHPXMLRPC) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=RPC=&lt;br /&gt;
Remote Procedure Calls (RPC) is a general computing paradigm for invoking procedures on one computer from another.  There are many forms of RPC technology, and we will focus on XML-RPC.&lt;br /&gt;
&lt;br /&gt;
=XML-RPC=&lt;br /&gt;
XML-RPC is an RPC protocol that uses XML encoding of RPC requests and responses over HTTP.&lt;br /&gt;
&lt;br /&gt;
==Calling Remote Procedures ==&lt;br /&gt;
&lt;br /&gt;
The calling procedure requests invokation of a function on a remote host using XML. A typical XML call looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;amp;lt;methodCall&amp;gt;&lt;br /&gt;
  &amp;amp;lt;methodName&amp;gt;FunctionName&amp;amp;lt;/methodName&amp;gt;&lt;br /&gt;
  &amp;amp;lt;params&amp;gt;&lt;br /&gt;
    &amp;amp;lt;param&amp;gt;&lt;br /&gt;
        &amp;amp;lt;value&amp;gt;&amp;amp;lt;DATATYPE&amp;gt;DATAVALUE&amp;amp;lt;/DATATYPE&amp;gt;&amp;amp;lt;/value&amp;gt;&lt;br /&gt;
    &amp;amp;lt;/param&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/params&amp;gt;&lt;br /&gt;
&amp;amp;lt;/methodCall&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The server invokes the function with the specified parameters then returns a response like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;amp;lt;methodResponse&amp;gt;&lt;br /&gt;
  &amp;amp;lt;params&amp;gt;&lt;br /&gt;
    &amp;amp;lt;param&amp;gt;&lt;br /&gt;
        &amp;amp;lt;value&amp;gt;&amp;amp;lt;DATATYPE&amp;gt;VALUE&amp;amp;lt;/DATATYPE&amp;gt;&amp;amp;lt;/value&amp;gt;&lt;br /&gt;
    &amp;amp;lt;/param&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/params&amp;gt;&lt;br /&gt;
&amp;amp;lt;/methodResponse&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Types==&lt;br /&gt;
&lt;br /&gt;
XML-RPC is generalized enough to transmit most any kind of data. The data types are specified like&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;DATATYPE&amp;gt;VALUE&amp;amp;lt;/DATATYPE&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The types include ''boolean'', ''dateTime'',''double'',''int'',''string''. Binary data has to be encoded at the sender in BASE-64 and decoded at the end by the receiver.  The encoded binary data is sent in a ''base64'' tag.  It also provides ''arrays'' and ''structs''.&lt;br /&gt;
&lt;br /&gt;
==XML-RPC Implementations ==&lt;br /&gt;
In order to use XML-RPC, you need to have an XML-RPC library installed for your platform and language. There are several available libraries, depending on your needs. You can find the list at http://www.xmlrpc.com/directory/1568/implementations.&lt;br /&gt;
&lt;br /&gt;
===XML-RPC for PHP(PHPXMLRPC)===&lt;br /&gt;
PHPXMLRPC is one of the several XML-RPC libraries for PHP. It is accessible at http://phpxmlrpc.sourceforge.net/&lt;br /&gt;
&lt;br /&gt;
In order to use this library, you need to download and unzip the package.  A handful of .inc files are included in the distribution for PHP clients and servers.  Include them in your PHP scripts to get access to the functionality in the library.  For example, xmlrpc.inc is the client library and xmlrpcs.inc is the server library.  It also provides several helper and wrapper functions that require other include files. You can look at the documentation to learn more about them. For this module, we will concentrate on client functionality.&lt;br /&gt;
&lt;br /&gt;
==== Creating a client in PHP ====&lt;br /&gt;
''xmlrpc_client'' is the XML-RPC client class. You can create a new client using&lt;br /&gt;
&lt;br /&gt;
 $client = new xmlrpc_client( server_url )&lt;br /&gt;
&lt;br /&gt;
It also supports basic HTTP authentication &lt;br /&gt;
&lt;br /&gt;
 $client=(  http(s)://user:password@server_url:port/path)&lt;br /&gt;
&lt;br /&gt;
For example, the following code creates a connection to the PHPXMLRPC demo server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?&lt;br /&gt;
include(&amp;quot;xmlrpc.inc&amp;quot;);&lt;br /&gt;
$client = new xmlrpc_client(&amp;quot;http://phpxmlrpc.sourceforge.net/server.php&amp;quot;);&lt;br /&gt;
?&amp;amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====PHPXMLRPC variables ====&lt;br /&gt;
The parameters(to or from remote procedures) are specified using the ''xmlrpcval'' function. This function can be called in different ways:&lt;br /&gt;
&lt;br /&gt;
 new xmlrpcval ( )&lt;br /&gt;
 new xmlrpcval ( string )&lt;br /&gt;
 new xmlrpcval ( VALUE, TYPE )&lt;br /&gt;
 new xmlrpcval ( ARRAY, ARRAY_TYPE )&lt;br /&gt;
&lt;br /&gt;
If ''xmlrpcval'' is called without a parameter, you need to set the variables by calling the ''addScalar'', ''addArray'' or ''addStruct'' functions to initialize the variable. If ''xmlrpcval'' is called with a string, the variable is assumed to be of type string, and the value is specified in the string parameter. A third option is to pass the value and type of a variable.  The fourth option is to create complex variables of arrays and structures.&lt;br /&gt;
&lt;br /&gt;
 $myInt = new xmlrpcvalue(1267, &amp;quot;int&amp;quot;);&lt;br /&gt;
 $myString = new xmlrpcvalue(&amp;quot;Hello, World!&amp;quot;, &amp;quot;string&amp;quot;);&lt;br /&gt;
 $myBool = new xmlrpcvalue(1, &amp;quot;boolean&amp;quot;);&lt;br /&gt;
 // note: this will serialize a php float value as xml-rpc string&lt;br /&gt;
 $myString2 = new xmlrpcvalue(1.24, &amp;quot;string&amp;quot;);&lt;br /&gt;
 $myArray = new xmlrpcval(&lt;br /&gt;
  array(&lt;br /&gt;
    new xmlrpcval(&amp;quot;Tom&amp;quot;),&lt;br /&gt;
    new xmlrpcval(&amp;quot;Dick&amp;quot;),&lt;br /&gt;
    new xmlrpcval(&amp;quot;Harry&amp;quot;)&lt;br /&gt;
  ),&lt;br /&gt;
  &amp;quot;array&amp;quot;);&lt;br /&gt;
 // recursive struct&lt;br /&gt;
 $myStruct = new xmlrpcval(&lt;br /&gt;
  array(&lt;br /&gt;
    &amp;quot;name&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;Tom&amp;quot;, &amp;quot;string&amp;quot;),&lt;br /&gt;
    &amp;quot;age&amp;quot; =&amp;gt; new xmlrpcval(34, &amp;quot;int&amp;quot;),&lt;br /&gt;
    &amp;quot;address&amp;quot; =&amp;gt; new xmlrpcval(&lt;br /&gt;
      array(&lt;br /&gt;
        &amp;quot;street&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;Fifht Ave&amp;quot;, &amp;quot;string&amp;quot;),&lt;br /&gt;
        &amp;quot;city&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;NY&amp;quot;, &amp;quot;string&amp;quot;)&lt;br /&gt;
      ), &lt;br /&gt;
      &amp;quot;struct&amp;quot;)&lt;br /&gt;
  ), &lt;br /&gt;
  &amp;quot;struct&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Once you have your variables ready, you can send them to the remote server.&lt;br /&gt;
&lt;br /&gt;
==== Preparing a message ====&lt;br /&gt;
&lt;br /&gt;
In order to make an RPC, first you need to prepare a message. This is done through the ''xmlrpcmsg'' class.&lt;br /&gt;
&lt;br /&gt;
 $message = new xmlrpcmsg(&amp;quot;REMOTE_FUNCTION&amp;quot;, PARAMETER_ARRAY);&lt;br /&gt;
&lt;br /&gt;
Remote function is the name of the function on the remote machine. Parameter array stores the parameters formed above. It can be skipped if the function does not take any parameters.&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code sends a message to the demo server to call an existing function:&lt;br /&gt;
&lt;br /&gt;
 $myParams=array(new xmlrpcval(23, &amp;quot;int&amp;quot;));&lt;br /&gt;
 $msg = new xmlrpcmsg(&amp;quot;examples.getStateName&amp;quot;, $myParams);&lt;br /&gt;
&lt;br /&gt;
==== Calling the function and receiving a response ====&lt;br /&gt;
The actual RPC is done when the client's ''send'' function is called.&lt;br /&gt;
&lt;br /&gt;
 $client-&amp;gt;return_type = 'phpvals';&lt;br /&gt;
 $resp = $client-&amp;gt;send($msg);&lt;br /&gt;
 if($resp-&amp;gt;faultCode())&lt;br /&gt;
   echo 'Error: ' . $resp-&amp;gt;faultString();&lt;br /&gt;
 else&lt;br /&gt;
   echo 'OK: got ' . $resp-&amp;gt;value();&lt;br /&gt;
&lt;br /&gt;
The client's return_type specifies either that the result should be stored in XML or as PHP variables. If there was an error during the call, the response's error code will be set. Otherwise, the value function returns the response value.&lt;br /&gt;
&lt;br /&gt;
===Perl Frontier  Module ===&lt;br /&gt;
Perl has two modules for XML-RPC. XML::RPC provides a very basic XML-RPC functionality for both client and server side. An alternative is Frontier::* modules. For this module, we will concentrate on Frontier::Daemon which provides ''server'' functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can initialize the daemon by calling  new function. The arguments include the port address and function names to serve. For example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use Frontier::Daemon;&lt;br /&gt;
&lt;br /&gt;
$methods = {'myservice.convert' =&amp;gt; \&amp;amp;convert&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
Frontier::Daemon-&amp;gt;new(LocalPort =&amp;gt; 4873,  methods =&amp;gt; $methods)&lt;br /&gt;
        or die &amp;quot;Couldn't start HTTP server: $!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Will set up the daemon at port 4873 and the function ''convert'' is called from the remote clients as ''myservice.convert''.&lt;br /&gt;
In the following example,  the daemon receives a Base64 encoded image, decodes,save,modify it  and sends the modified image  back in Base64. Please note that it assumes just single user is using the system at any time, if multiple users access, the files could be overwritten.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use MIME::Base64;&lt;br /&gt;
use Frontier::Daemon;&lt;br /&gt;
use Frontier::RPC2;&lt;br /&gt;
&lt;br /&gt;
$|=1;&lt;br /&gt;
print &amp;quot;server $$&amp;quot;;&lt;br /&gt;
$methods = {'myservice.convert' =&amp;gt; \&amp;amp;convert&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
#initialize daemon&lt;br /&gt;
Frontier::Daemon-&amp;gt;new(LocalPort =&amp;gt; 4873,  methods =&amp;gt; $methods,ReuseAddr=&amp;gt;1)&lt;br /&gt;
        or die &amp;quot;Couldn't start HTTP server: $!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
sub convert{&lt;br /&gt;
   ($name,$encoded)=@_; #client sends filename and file in base64&lt;br /&gt;
   print &amp;quot;Name:$name\n\n\n\n&amp;quot;;&lt;br /&gt;
   $decoded = MIME::Base64::decode($encoded);&lt;br /&gt;
   open(OUT,&amp;quot;&amp;gt;/tmp/$name&amp;quot;); #save the image&lt;br /&gt;
     print OUT $decoded;&lt;br /&gt;
   close(OUT);&lt;br /&gt;
    #put a label over the image&lt;br /&gt;
    system(&amp;quot;convert -pointsize 40 -draw \&amp;quot;text 200,200 'cse330'\&amp;quot; /tmp/$name /tmp/a2.jpg&amp;quot;);&lt;br /&gt;
   #read back the image&lt;br /&gt;
   open(IN,&amp;quot;&amp;lt;/tmp/a2.jpg&amp;quot;);&lt;br /&gt;
   binmode(IN);&lt;br /&gt;
   read IN,$de,1000000;&lt;br /&gt;
   close(OUT);&lt;br /&gt;
    &lt;br /&gt;
   #encode it&lt;br /&gt;
   $encoded = MIME::Base64::encode($de);&lt;br /&gt;
   #print $encoded;&lt;br /&gt;
   #send it back&lt;br /&gt;
   return {'success' =&amp;gt; &amp;quot;true&amp;quot;,'result' =&amp;gt;$encoded};&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the client side, you may have an upload page:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;image-xmlrpc.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose an image  file to upload: &amp;amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally a php based client that would send the uploaded image to RPC server, and display the modified image.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?&lt;br /&gt;
&lt;br /&gt;
$uploadfile=  $_FILES['uploadedfile']['tmp_name'];&lt;br /&gt;
 $handle = fopen($uploadfile,'r');&lt;br /&gt;
       $file_content = fread($handle,filesize($uploadfile)); //read uploaded file&lt;br /&gt;
       fclose($handle);&lt;br /&gt;
       $encoded = chunk_split(base64_encode($file_content)); //encode it&lt;br /&gt;
       include(&amp;quot;xmlrpc.inc&amp;quot;); //for xmlrpc operations&lt;br /&gt;
&lt;br /&gt;
$client = new xmlrpc_client(&amp;quot;http://127.0.0.1:4873/RPC2&amp;quot;); //your host&lt;br /&gt;
 &lt;br /&gt;
 $myParams=array(new xmlrpcval($_FILES['uploadedfile']['name'], &amp;quot;string&amp;quot;),&lt;br /&gt;
                 new xmlrpcval($encoded, &amp;quot;string&amp;quot;)&lt;br /&gt;
 );&lt;br /&gt;
$message = new xmlrpcmsg(&amp;quot;myservice.convert&amp;quot;, $myParams);&lt;br /&gt;
$resp = $client-&amp;gt;send($message);&lt;br /&gt;
if ($resp-&amp;gt;faultCode())&lt;br /&gt;
  echo 'KO. Error: '.$resp-&amp;gt;faultString();&lt;br /&gt;
else{&lt;br /&gt;
  $struct=$resp-&amp;gt;value();&lt;br /&gt;
   $retvalue = $struct-&amp;gt;structmem('success'); //value of the field success&lt;br /&gt;
    $resultval = $struct-&amp;gt;structmem('result'); //result field&lt;br /&gt;
        $result = $resultval-&amp;gt;scalarval(); //encoded image from the result&lt;br /&gt;
&lt;br /&gt;
       $decoded = base64_decode($result); //decode the base64 data&lt;br /&gt;
$handle = fopen(&amp;quot;changed.jpg&amp;quot;,'w'); //save it to a new file&lt;br /&gt;
       $file_content = fwrite($handle,$decoded);&lt;br /&gt;
       fclose($handle);&lt;br /&gt;
&lt;br /&gt;
      echo &amp;quot;&amp;lt;img src='changed.jpg'/&amp;gt;&amp;lt;br&amp;gt;&amp;quot;; //display the new image&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
?&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Remote_Procedure_Calls&amp;diff=131</id>
		<title>Remote Procedure Calls</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Remote_Procedure_Calls&amp;diff=131"/>
		<updated>2009-11-11T03:02:23Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=RPC=&lt;br /&gt;
Remote Procedure Calls (RPC) is a general computing paradigm for invoking procedures on one computer from another.  There are many forms of RPC technology, and we will focus on XML-RPC.&lt;br /&gt;
&lt;br /&gt;
=XML-RPC=&lt;br /&gt;
XML-RPC is an RPC protocol that uses XML encoding of RPC requests and responses over HTTP.&lt;br /&gt;
&lt;br /&gt;
==Calling Remote Procedures ==&lt;br /&gt;
&lt;br /&gt;
The calling procedure requests invokation of a function on a remote host using XML. A typical XML call looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;amp;lt;methodCall&amp;gt;&lt;br /&gt;
  &amp;amp;lt;methodName&amp;gt;FunctionName&amp;amp;lt;/methodName&amp;gt;&lt;br /&gt;
  &amp;amp;lt;params&amp;gt;&lt;br /&gt;
    &amp;amp;lt;param&amp;gt;&lt;br /&gt;
        &amp;amp;lt;value&amp;gt;&amp;amp;lt;DATATYPE&amp;gt;DATAVALUE&amp;amp;lt;/DATATYPE&amp;gt;&amp;amp;lt;/value&amp;gt;&lt;br /&gt;
    &amp;amp;lt;/param&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/params&amp;gt;&lt;br /&gt;
&amp;amp;lt;/methodCall&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The server invokes the function with the specified parameters then returns a response like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;amp;lt;methodResponse&amp;gt;&lt;br /&gt;
  &amp;amp;lt;params&amp;gt;&lt;br /&gt;
    &amp;amp;lt;param&amp;gt;&lt;br /&gt;
        &amp;amp;lt;value&amp;gt;&amp;amp;lt;DATATYPE&amp;gt;VALUE&amp;amp;lt;/DATATYPE&amp;gt;&amp;amp;lt;/value&amp;gt;&lt;br /&gt;
    &amp;amp;lt;/param&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/params&amp;gt;&lt;br /&gt;
&amp;amp;lt;/methodResponse&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Types==&lt;br /&gt;
&lt;br /&gt;
XML-RPC is generalized enough to transmit most any kind of data. The data types are specified like&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;DATATYPE&amp;gt;VALUE&amp;amp;lt;/DATATYPE&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The types include ''boolean'', ''dateTime'',''double'',''int'',''string''. Binary data has to be encoded at the sender in BASE-64 and decoded at the end by the receiver.  The encoded binary data is sent in a ''base64'' tag.  It also provides ''arrays'' and ''structs''.&lt;br /&gt;
&lt;br /&gt;
==XML-RPC Implementations ==&lt;br /&gt;
In order to use XML-RPC, you need to have an XML-RPC library installed for your platform and language. There are several available libraries, depending on your needs. You can find the list at http://www.xmlrpc.com/directory/1568/implementations.&lt;br /&gt;
&lt;br /&gt;
===XML-RPC for PHP(PHPXMLRPC)===&lt;br /&gt;
PHPXMLRPC is one of the several XML-RPC libraries for PHP. It is accessible at http://phpxmlrpc.sourceforge.net/&lt;br /&gt;
&lt;br /&gt;
In order to use this library, all you need to do is to add the  include files xmlrpc.inc (to have a PHP client),  xmlrpcs.inc (to implement an RPC server). It also provides several helper and wrapper functions that require other include files. You can look at the documentation to learn more about them. For this module, we will concentrate on client functions.&lt;br /&gt;
&lt;br /&gt;
==== Creating a client in PHP ====&lt;br /&gt;
''xmlrpc_client'' provides a client object. You can create a new client using&lt;br /&gt;
$client=new xmlrpc_client ( server_url )&lt;br /&gt;
&lt;br /&gt;
It also provides the basic HTTP authentication &lt;br /&gt;
   $client=(  http(s)://user:password@server_url:port/path)&lt;br /&gt;
&lt;br /&gt;
  For example, the following code creates a connection to the demo server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?&lt;br /&gt;
include(&amp;quot;xmlrpc.inc&amp;quot;);&lt;br /&gt;
$client = new xmlrpc_client(&amp;quot;http://phpxmlrpc.sourceforge.net/server.php&amp;quot;);&lt;br /&gt;
?&amp;amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
====PHPXMLRPC variables ====&lt;br /&gt;
The parameters (to or from remote procedures) are specified using  ''xmlrpcval'' function. This function could be called in several format:&lt;br /&gt;
&lt;br /&gt;
 new xmlrpcval ( )&lt;br /&gt;
 new xmlrpcval ( string  )&lt;br /&gt;
 new xmlrpcval ( VALUE, TYPE )&lt;br /&gt;
 new xmlrpcval ( ARRAY, ARRAY_TYPE )&lt;br /&gt;
&lt;br /&gt;
If ''xmlrpcval'' is called without a parameter, you need to set the variables by calling addScalar, addArray or addStruct functions to initialize the variable. If ''xmlrpcval'' is called with a string, the variable is assumed to be of type ''string'', and the value is specified in the string parameter. A third option is to pass the value and type of a variable.  The fourth option is to create complex variables of arrays and structures.&lt;br /&gt;
 $myInt = new xmlrpcvalue(1267, &amp;quot;int&amp;quot;);&lt;br /&gt;
 $myString = new xmlrpcvalue(&amp;quot;Hello, World!&amp;quot;, &amp;quot;string&amp;quot;);&lt;br /&gt;
 $myBool = new xmlrpcvalue(1, &amp;quot;boolean&amp;quot;);&lt;br /&gt;
 // note: this will serialize a php float value as xml-rpc string&lt;br /&gt;
 $myString2 = new xmlrpcvalue(1.24, &amp;quot;string&amp;quot;);&lt;br /&gt;
 $myArray = new xmlrpcval(&lt;br /&gt;
  array(&lt;br /&gt;
    new xmlrpcval(&amp;quot;Tom&amp;quot;),&lt;br /&gt;
    new xmlrpcval(&amp;quot;Dick&amp;quot;),&lt;br /&gt;
    new xmlrpcval(&amp;quot;Harry&amp;quot;)&lt;br /&gt;
  ),&lt;br /&gt;
  &amp;quot;array&amp;quot;);&lt;br /&gt;
 // recursive struct&lt;br /&gt;
 $myStruct = new xmlrpcval(&lt;br /&gt;
  array(&lt;br /&gt;
    &amp;quot;name&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;Tom&amp;quot;, &amp;quot;string&amp;quot;),&lt;br /&gt;
    &amp;quot;age&amp;quot; =&amp;gt; new xmlrpcval(34, &amp;quot;int&amp;quot;),&lt;br /&gt;
    &amp;quot;address&amp;quot; =&amp;gt; new xmlrpcval(&lt;br /&gt;
      array(&lt;br /&gt;
        &amp;quot;street&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;Fifht Ave&amp;quot;, &amp;quot;string&amp;quot;),&lt;br /&gt;
        &amp;quot;city&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;NY&amp;quot;, &amp;quot;string&amp;quot;)&lt;br /&gt;
      ), &lt;br /&gt;
      &amp;quot;struct&amp;quot;)&lt;br /&gt;
  ), &lt;br /&gt;
  &amp;quot;struct&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have your variables ready, you can send them to the remote server.&lt;br /&gt;
&lt;br /&gt;
==== Preparing a message ====&lt;br /&gt;
&lt;br /&gt;
In order to make an RPC, first you need to prepare a message. This is done through ''xmlrpcmsg'' class.&lt;br /&gt;
&lt;br /&gt;
 $message = new xmlrpcmsg(&amp;quot;REMOTE_FUNCTION&amp;quot;, PARAMETER_ARRAY);&lt;br /&gt;
&lt;br /&gt;
Remote function is the name of the function on the remote machine. Parameter array stores the parameters. It can be skipped, if the function is not taking any parameters.&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code sends a message  the demo server  to call an existing function:&lt;br /&gt;
 $myParams=array(new xmlrpcval(23, &amp;quot;int&amp;quot;));&lt;br /&gt;
 $msg = new xmlrpcmsg(&amp;quot;examples.getStateName&amp;quot;, $myParams);&lt;br /&gt;
&lt;br /&gt;
==== Calling the function and receiving response ====&lt;br /&gt;
The actual RPC is done when the client's ''send'' function is called.&lt;br /&gt;
&lt;br /&gt;
 $client-&amp;gt;return_type = 'phpvals';&lt;br /&gt;
 $resp = $client-&amp;gt;send($msg);&lt;br /&gt;
 if ($resp-&amp;gt;faultCode())&lt;br /&gt;
   echo '  Error: '.$resp-&amp;gt;faultString();&lt;br /&gt;
 else&lt;br /&gt;
   echo 'OK: got '.$resp-&amp;gt;value();&lt;br /&gt;
&lt;br /&gt;
client's return_type specify either we want to get the response in XML or as php variable.&lt;br /&gt;
If there was an error during the call, the response's fault code will be set. Otherwise, the value function returns the response value.&lt;br /&gt;
&lt;br /&gt;
===Perl Frontier  Module ===&lt;br /&gt;
Perl has two modules for XML-RPC. XML::RPC provides a very basic XML-RPC functionality for both client and server side. An alternative is Frontier::* modules. For this module, we will concentrate on Frontier::Daemon which provides ''server'' functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can initialize the daemon by calling  new function. The arguments include the port address and function names to serve. For example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use Frontier::Daemon;&lt;br /&gt;
&lt;br /&gt;
$methods = {'myservice.convert' =&amp;gt; \&amp;amp;convert&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
Frontier::Daemon-&amp;gt;new(LocalPort =&amp;gt; 4873,  methods =&amp;gt; $methods)&lt;br /&gt;
        or die &amp;quot;Couldn't start HTTP server: $!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Will set up the daemon at port 4873 and the function ''convert'' is called from the remote clients as ''myservice.convert''.&lt;br /&gt;
In the following example,  the daemon receives a Base64 encoded image, decodes,save,modify it  and sends the modified image  back in Base64. Please note that it assumes just single user is using the system at any time, if multiple users access, the files could be overwritten.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use MIME::Base64;&lt;br /&gt;
use Frontier::Daemon;&lt;br /&gt;
use Frontier::RPC2;&lt;br /&gt;
&lt;br /&gt;
$|=1;&lt;br /&gt;
print &amp;quot;server $$&amp;quot;;&lt;br /&gt;
$methods = {'myservice.convert' =&amp;gt; \&amp;amp;convert&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
#initialize daemon&lt;br /&gt;
Frontier::Daemon-&amp;gt;new(LocalPort =&amp;gt; 4873,  methods =&amp;gt; $methods,ReuseAddr=&amp;gt;1)&lt;br /&gt;
        or die &amp;quot;Couldn't start HTTP server: $!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
sub convert{&lt;br /&gt;
   ($name,$encoded)=@_; #client sends filename and file in base64&lt;br /&gt;
   print &amp;quot;Name:$name\n\n\n\n&amp;quot;;&lt;br /&gt;
   $decoded = MIME::Base64::decode($encoded);&lt;br /&gt;
   open(OUT,&amp;quot;&amp;gt;/tmp/$name&amp;quot;); #save the image&lt;br /&gt;
     print OUT $decoded;&lt;br /&gt;
   close(OUT);&lt;br /&gt;
    #put a label over the image&lt;br /&gt;
    system(&amp;quot;convert -pointsize 40 -draw \&amp;quot;text 200,200 'cse330'\&amp;quot; /tmp/$name /tmp/a2.jpg&amp;quot;);&lt;br /&gt;
   #read back the image&lt;br /&gt;
   open(IN,&amp;quot;&amp;lt;/tmp/a2.jpg&amp;quot;);&lt;br /&gt;
   binmode(IN);&lt;br /&gt;
   read IN,$de,1000000;&lt;br /&gt;
   close(OUT);&lt;br /&gt;
    &lt;br /&gt;
   #encode it&lt;br /&gt;
   $encoded = MIME::Base64::encode($de);&lt;br /&gt;
   #print $encoded;&lt;br /&gt;
   #send it back&lt;br /&gt;
   return {'success' =&amp;gt; &amp;quot;true&amp;quot;,'result' =&amp;gt;$encoded};&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the client side, you may have an upload page:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;image-xmlrpc.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose an image  file to upload: &amp;amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally a php based client that would send the uploaded image to RPC server, and display the modified image.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?&lt;br /&gt;
&lt;br /&gt;
$uploadfile=  $_FILES['uploadedfile']['tmp_name'];&lt;br /&gt;
 $handle = fopen($uploadfile,'r');&lt;br /&gt;
       $file_content = fread($handle,filesize($uploadfile)); //read uploaded file&lt;br /&gt;
       fclose($handle);&lt;br /&gt;
       $encoded = chunk_split(base64_encode($file_content)); //encode it&lt;br /&gt;
       include(&amp;quot;xmlrpc.inc&amp;quot;); //for xmlrpc operations&lt;br /&gt;
&lt;br /&gt;
$client = new xmlrpc_client(&amp;quot;http://127.0.0.1:4873/RPC2&amp;quot;); //your host&lt;br /&gt;
 &lt;br /&gt;
 $myParams=array(new xmlrpcval($_FILES['uploadedfile']['name'], &amp;quot;string&amp;quot;),&lt;br /&gt;
                 new xmlrpcval($encoded, &amp;quot;string&amp;quot;)&lt;br /&gt;
 );&lt;br /&gt;
$message = new xmlrpcmsg(&amp;quot;myservice.convert&amp;quot;, $myParams);&lt;br /&gt;
$resp = $client-&amp;gt;send($message);&lt;br /&gt;
if ($resp-&amp;gt;faultCode())&lt;br /&gt;
  echo 'KO. Error: '.$resp-&amp;gt;faultString();&lt;br /&gt;
else{&lt;br /&gt;
  $struct=$resp-&amp;gt;value();&lt;br /&gt;
   $retvalue = $struct-&amp;gt;structmem('success'); //value of the field success&lt;br /&gt;
    $resultval = $struct-&amp;gt;structmem('result'); //result field&lt;br /&gt;
        $result = $resultval-&amp;gt;scalarval(); //encoded image from the result&lt;br /&gt;
&lt;br /&gt;
       $decoded = base64_decode($result); //decode the base64 data&lt;br /&gt;
$handle = fopen(&amp;quot;changed.jpg&amp;quot;,'w'); //save it to a new file&lt;br /&gt;
       $file_content = fwrite($handle,$decoded);&lt;br /&gt;
       fclose($handle);&lt;br /&gt;
&lt;br /&gt;
      echo &amp;quot;&amp;lt;img src='changed.jpg'/&amp;gt;&amp;lt;br&amp;gt;&amp;quot;; //display the new image&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
?&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Remote_Procedure_Calls&amp;diff=130</id>
		<title>Remote Procedure Calls</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Remote_Procedure_Calls&amp;diff=130"/>
		<updated>2009-11-11T02:36:48Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=XML-RPC=&lt;br /&gt;
XML-RPC is a spec and a set of implementations that allow software running on disparate operating systems, running in different environments to make procedure calls over the Internet.&lt;br /&gt;
&lt;br /&gt;
==Calling Remote Procedures ==&lt;br /&gt;
&lt;br /&gt;
In XML-RPC, the programs exchange information through XML. The calling procedure request a function on a remote host using XML. A typical XML remote call will be similar to the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;amp;lt;methodCall&amp;gt;&lt;br /&gt;
  &amp;amp;lt;methodName&amp;gt;FunctionName&amp;amp;lt;/methodName&amp;gt;&lt;br /&gt;
  &amp;amp;lt;params&amp;gt;&lt;br /&gt;
    &amp;amp;lt;param&amp;gt;&lt;br /&gt;
        &amp;amp;lt;value&amp;gt;&amp;amp;lt;DATATYPE&amp;gt;DATAVALUE&amp;amp;lt;/DATATYPE&amp;gt;&amp;amp;lt;/value&amp;gt;&lt;br /&gt;
    &amp;amp;lt;/param&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/params&amp;gt;&lt;br /&gt;
&amp;amp;lt;/methodCall&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then the remote function returns its response in XML&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;amp;lt;methodResponse&amp;gt;&lt;br /&gt;
  &amp;amp;lt;params&amp;gt;&lt;br /&gt;
    &amp;amp;lt;param&amp;gt;&lt;br /&gt;
        &amp;amp;lt;value&amp;gt;&amp;amp;lt;DATATYPE&amp;gt;VALUE&amp;amp;lt;/DATATYPE&amp;gt;&amp;amp;lt;/value&amp;gt;&lt;br /&gt;
    &amp;amp;lt;/param&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/params&amp;gt;&lt;br /&gt;
&amp;amp;lt;/methodResponse&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
==Data Types ==&lt;br /&gt;
&lt;br /&gt;
XML-RPC is very powerful and can transmit any data. The data types are specified with&lt;br /&gt;
     &amp;amp;lt;DATATYPE&amp;gt;VALUE&amp;amp;lt;/DATATYPE&amp;gt; &lt;br /&gt;
The types include ''boolean'', ''dateTime'',''double'',''integer'',''string''. The binary data has to be encoded at the sender origin in BASE-64 and decoded at the end by the receiver.  The encoded binary data is send in ''base64'' tag.  It also provides ''array''s and a method to define ''struct''.&lt;br /&gt;
&lt;br /&gt;
==XML-RPC Implementations ==&lt;br /&gt;
In order to use XML-RPC, you need to have an XML-RPC library installed in your platform. There are several available libraries depending on your platform. You can find the list at http://www.xmlrpc.com/directory/1568/implementations&lt;br /&gt;
&lt;br /&gt;
A typical RPC system could be a PHP page calling a remote procedure  to do some specific task in a server. The  passed parameters are usually based on browser's actions.&lt;br /&gt;
&lt;br /&gt;
===XML-RPC for PHP(PHPXMLRPC)===&lt;br /&gt;
PHPXMLRPC is one of the several XML-RPC libraries for PHP. It is accessible at http://phpxmlrpc.sourceforge.net/&lt;br /&gt;
&lt;br /&gt;
In order to use this library, all you need to do is to add the  include files xmlrpc.inc (to have a PHP client),  xmlrpcs.inc (to implement an RPC server). It also provides several helper and wrapper functions that require other include files. You can look at the documentation to learn more about them. For this module, we will concentrate on client functions.&lt;br /&gt;
&lt;br /&gt;
==== Creating a client in PHP ====&lt;br /&gt;
''xmlrpc_client'' provides a client object. You can create a new client using&lt;br /&gt;
$client=new xmlrpc_client ( server_url )&lt;br /&gt;
&lt;br /&gt;
It also provides the basic HTTP authentication &lt;br /&gt;
   $client=(  http(s)://user:password@server_url:port/path)&lt;br /&gt;
&lt;br /&gt;
  For example, the following code creates a connection to the demo server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?&lt;br /&gt;
include(&amp;quot;xmlrpc.inc&amp;quot;);&lt;br /&gt;
$client = new xmlrpc_client(&amp;quot;http://phpxmlrpc.sourceforge.net/server.php&amp;quot;);&lt;br /&gt;
?&amp;amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
====PHPXMLRPC variables ====&lt;br /&gt;
The parameters (to or from remote procedures) are specified using  ''xmlrpcval'' function. This function could be called in several format:&lt;br /&gt;
&lt;br /&gt;
 new xmlrpcval ( )&lt;br /&gt;
 new xmlrpcval ( string  )&lt;br /&gt;
 new xmlrpcval ( VALUE, TYPE )&lt;br /&gt;
 new xmlrpcval ( ARRAY, ARRAY_TYPE )&lt;br /&gt;
&lt;br /&gt;
If ''xmlrpcval'' is called without a parameter, you need to set the variables by calling addScalar, addArray or addStruct functions to initialize the variable. If ''xmlrpcval'' is called with a string, the variable is assumed to be of type ''string'', and the value is specified in the string parameter. A third option is to pass the value and type of a variable.  The fourth option is to create complex variables of arrays and structures.&lt;br /&gt;
 $myInt = new xmlrpcvalue(1267, &amp;quot;int&amp;quot;);&lt;br /&gt;
 $myString = new xmlrpcvalue(&amp;quot;Hello, World!&amp;quot;, &amp;quot;string&amp;quot;);&lt;br /&gt;
 $myBool = new xmlrpcvalue(1, &amp;quot;boolean&amp;quot;);&lt;br /&gt;
 // note: this will serialize a php float value as xml-rpc string&lt;br /&gt;
 $myString2 = new xmlrpcvalue(1.24, &amp;quot;string&amp;quot;);&lt;br /&gt;
 $myArray = new xmlrpcval(&lt;br /&gt;
  array(&lt;br /&gt;
    new xmlrpcval(&amp;quot;Tom&amp;quot;),&lt;br /&gt;
    new xmlrpcval(&amp;quot;Dick&amp;quot;),&lt;br /&gt;
    new xmlrpcval(&amp;quot;Harry&amp;quot;)&lt;br /&gt;
  ),&lt;br /&gt;
  &amp;quot;array&amp;quot;);&lt;br /&gt;
 // recursive struct&lt;br /&gt;
 $myStruct = new xmlrpcval(&lt;br /&gt;
  array(&lt;br /&gt;
    &amp;quot;name&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;Tom&amp;quot;, &amp;quot;string&amp;quot;),&lt;br /&gt;
    &amp;quot;age&amp;quot; =&amp;gt; new xmlrpcval(34, &amp;quot;int&amp;quot;),&lt;br /&gt;
    &amp;quot;address&amp;quot; =&amp;gt; new xmlrpcval(&lt;br /&gt;
      array(&lt;br /&gt;
        &amp;quot;street&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;Fifht Ave&amp;quot;, &amp;quot;string&amp;quot;),&lt;br /&gt;
        &amp;quot;city&amp;quot; =&amp;gt; new xmlrpcval(&amp;quot;NY&amp;quot;, &amp;quot;string&amp;quot;)&lt;br /&gt;
      ), &lt;br /&gt;
      &amp;quot;struct&amp;quot;)&lt;br /&gt;
  ), &lt;br /&gt;
  &amp;quot;struct&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have your variables ready, you can send them to the remote server.&lt;br /&gt;
&lt;br /&gt;
==== Preparing a message ====&lt;br /&gt;
&lt;br /&gt;
In order to make an RPC, first you need to prepare a message. This is done through ''xmlrpcmsg'' class.&lt;br /&gt;
&lt;br /&gt;
 $message = new xmlrpcmsg(&amp;quot;REMOTE_FUNCTION&amp;quot;, PARAMETER_ARRAY);&lt;br /&gt;
&lt;br /&gt;
Remote function is the name of the function on the remote machine. Parameter array stores the parameters. It can be skipped, if the function is not taking any parameters.&lt;br /&gt;
&lt;br /&gt;
For example, the following PHP code sends a message  the demo server  to call an existing function:&lt;br /&gt;
 $myParams=array(new xmlrpcval(23, &amp;quot;int&amp;quot;));&lt;br /&gt;
 $msg = new xmlrpcmsg(&amp;quot;examples.getStateName&amp;quot;, $myParams);&lt;br /&gt;
&lt;br /&gt;
==== Calling the function and receiving response ====&lt;br /&gt;
The actual RPC is done when the client's ''send'' function is called.&lt;br /&gt;
&lt;br /&gt;
 $client-&amp;gt;return_type = 'phpvals';&lt;br /&gt;
 $resp = $client-&amp;gt;send($msg);&lt;br /&gt;
 if ($resp-&amp;gt;faultCode())&lt;br /&gt;
   echo '  Error: '.$resp-&amp;gt;faultString();&lt;br /&gt;
 else&lt;br /&gt;
   echo 'OK: got '.$resp-&amp;gt;value();&lt;br /&gt;
&lt;br /&gt;
client's return_type specify either we want to get the response in XML or as php variable.&lt;br /&gt;
If there was an error during the call, the response's fault code will be set. Otherwise, the value function returns the response value.&lt;br /&gt;
&lt;br /&gt;
===Perl Frontier  Module ===&lt;br /&gt;
Perl has two modules for XML-RPC. XML::RPC provides a very basic XML-RPC functionality for both client and server side. An alternative is Frontier::* modules. For this module, we will concentrate on Frontier::Daemon which provides ''server'' functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can initialize the daemon by calling  new function. The arguments include the port address and function names to serve. For example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use Frontier::Daemon;&lt;br /&gt;
&lt;br /&gt;
$methods = {'myservice.convert' =&amp;gt; \&amp;amp;convert&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
Frontier::Daemon-&amp;gt;new(LocalPort =&amp;gt; 4873,  methods =&amp;gt; $methods)&lt;br /&gt;
        or die &amp;quot;Couldn't start HTTP server: $!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Will set up the daemon at port 4873 and the function ''convert'' is called from the remote clients as ''myservice.convert''.&lt;br /&gt;
In the following example,  the daemon receives a Base64 encoded image, decodes,save,modify it  and sends the modified image  back in Base64. Please note that it assumes just single user is using the system at any time, if multiple users access, the files could be overwritten.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use MIME::Base64;&lt;br /&gt;
use Frontier::Daemon;&lt;br /&gt;
use Frontier::RPC2;&lt;br /&gt;
&lt;br /&gt;
$|=1;&lt;br /&gt;
print &amp;quot;server $$&amp;quot;;&lt;br /&gt;
$methods = {'myservice.convert' =&amp;gt; \&amp;amp;convert&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
#initialize daemon&lt;br /&gt;
Frontier::Daemon-&amp;gt;new(LocalPort =&amp;gt; 4873,  methods =&amp;gt; $methods,ReuseAddr=&amp;gt;1)&lt;br /&gt;
        or die &amp;quot;Couldn't start HTTP server: $!&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
sub convert{&lt;br /&gt;
   ($name,$encoded)=@_; #client sends filename and file in base64&lt;br /&gt;
   print &amp;quot;Name:$name\n\n\n\n&amp;quot;;&lt;br /&gt;
   $decoded = MIME::Base64::decode($encoded);&lt;br /&gt;
   open(OUT,&amp;quot;&amp;gt;/tmp/$name&amp;quot;); #save the image&lt;br /&gt;
     print OUT $decoded;&lt;br /&gt;
   close(OUT);&lt;br /&gt;
    #put a label over the image&lt;br /&gt;
    system(&amp;quot;convert -pointsize 40 -draw \&amp;quot;text 200,200 'cse330'\&amp;quot; /tmp/$name /tmp/a2.jpg&amp;quot;);&lt;br /&gt;
   #read back the image&lt;br /&gt;
   open(IN,&amp;quot;&amp;lt;/tmp/a2.jpg&amp;quot;);&lt;br /&gt;
   binmode(IN);&lt;br /&gt;
   read IN,$de,1000000;&lt;br /&gt;
   close(OUT);&lt;br /&gt;
    &lt;br /&gt;
   #encode it&lt;br /&gt;
   $encoded = MIME::Base64::encode($de);&lt;br /&gt;
   #print $encoded;&lt;br /&gt;
   #send it back&lt;br /&gt;
   return {'success' =&amp;gt; &amp;quot;true&amp;quot;,'result' =&amp;gt;$encoded};&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the client side, you may have an upload page:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;form enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;image-xmlrpc.php&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
&amp;amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;MAX_FILE_SIZE&amp;quot; value=&amp;quot;100000&amp;quot; /&amp;gt;&lt;br /&gt;
Choose an image  file to upload: &amp;amp;lt;input name=&amp;quot;uploadedfile&amp;quot; type=&amp;quot;file&amp;quot; /&amp;gt;&amp;amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload File&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally a php based client that would send the uploaded image to RPC server, and display the modified image.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;?&lt;br /&gt;
&lt;br /&gt;
$uploadfile=  $_FILES['uploadedfile']['tmp_name'];&lt;br /&gt;
 $handle = fopen($uploadfile,'r');&lt;br /&gt;
       $file_content = fread($handle,filesize($uploadfile)); //read uploaded file&lt;br /&gt;
       fclose($handle);&lt;br /&gt;
       $encoded = chunk_split(base64_encode($file_content)); //encode it&lt;br /&gt;
       include(&amp;quot;xmlrpc.inc&amp;quot;); //for xmlrpc operations&lt;br /&gt;
&lt;br /&gt;
$client = new xmlrpc_client(&amp;quot;http://127.0.0.1:4873/RPC2&amp;quot;); //your host&lt;br /&gt;
 &lt;br /&gt;
 $myParams=array(new xmlrpcval($_FILES['uploadedfile']['name'], &amp;quot;string&amp;quot;),&lt;br /&gt;
                 new xmlrpcval($encoded, &amp;quot;string&amp;quot;)&lt;br /&gt;
 );&lt;br /&gt;
$message = new xmlrpcmsg(&amp;quot;myservice.convert&amp;quot;, $myParams);&lt;br /&gt;
$resp = $client-&amp;gt;send($message);&lt;br /&gt;
if ($resp-&amp;gt;faultCode())&lt;br /&gt;
  echo 'KO. Error: '.$resp-&amp;gt;faultString();&lt;br /&gt;
else{&lt;br /&gt;
  $struct=$resp-&amp;gt;value();&lt;br /&gt;
   $retvalue = $struct-&amp;gt;structmem('success'); //value of the field success&lt;br /&gt;
    $resultval = $struct-&amp;gt;structmem('result'); //result field&lt;br /&gt;
        $result = $resultval-&amp;gt;scalarval(); //encoded image from the result&lt;br /&gt;
&lt;br /&gt;
       $decoded = base64_decode($result); //decode the base64 data&lt;br /&gt;
$handle = fopen(&amp;quot;changed.jpg&amp;quot;,'w'); //save it to a new file&lt;br /&gt;
       $file_content = fwrite($handle,$decoded);&lt;br /&gt;
       fclose($handle);&lt;br /&gt;
&lt;br /&gt;
      echo &amp;quot;&amp;lt;img src='changed.jpg'/&amp;gt;&amp;lt;br&amp;gt;&amp;quot;; //display the new image&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
?&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=107</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=107"/>
		<updated>2009-10-26T03:32:59Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* HTML Scanning */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language with syntax similar to bash and PHP.  It's a high level, general purpose language that is used primarily to automate repetitive tasks, and to parse and process text.  Indeed, text processing was the original intended use for the language, although it has grown to be an extremely flexible language for many different types of processing.&lt;br /&gt;
&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways.  The programs, or scripts, themselves are plain text files, usually ending in '.pl' to indicate that it is a Perl script.  Given a Perl script file, the most common ways to execute the script are shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pass the script as input to the 'perl' executable: &amp;lt;pre&amp;gt;perl -w SCRIPT&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Execute the script directly by adding the following line to the top of the script:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, change the script to be executable, and finally execute the script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x SCRIPT&lt;br /&gt;
./SCRIPT &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While flexibility is one of the major strengths of Perl, the flexibility in the language itself can often lead to unintended consequences.  To overcome this, it is good practice to explicitly enable warnings and strict behavior for you scripts.  The ''-w'' above when running perl gets you part of the way there, and although it's not necessary to supply the ''-w'' it is nearly always the right thing to do.  You should also add the line:&lt;br /&gt;
&lt;br /&gt;
 use strict;&lt;br /&gt;
&lt;br /&gt;
to all of your scripts, near the top, to enable strict error checking.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Standard variables start with a dollar sign, ''$'', at the beginning of their names.  Unlike shell scripts, the ''$'' is always used, regardless of whether the variable is being set or accessed.  As with other scripting languages, you don't specify the type of the variable; it will be used treated as a integer, floating point number, string, etc, depending on how it is used in your script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Comments start with the number/sharp/hash sign, ''#'', and extend until the end of the line.  All non-comment statements end with a semicolon, '';''.  You can print  variables with the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
Variables can be used inside strings, and the value of that variable will be included in the string. (Note that you can also use the ''printf'' function when you need specific formatting of the output).&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string:&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global by default, so you need to specify locality if you want local variables. This can be done with the ''local'' and ''my'' keywords. Generally, ''my'' is safer and faster, and is therefore used most often.  If a variable is defined by ''my'' in a block, it will be local to the block.  If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $i=0;&lt;br /&gt;
 while($i&amp;lt;5)&lt;br /&gt;
 {&lt;br /&gt;
   my $squared = $i*$i;&lt;br /&gt;
   $cubed = $i*$i*$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print $squared;&lt;br /&gt;
 print $cubed;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above program segment, ''$squared'' is defined with ''my'' inside the while loop, so it is only defined inside that loop.  ''$cubed'' is not given any special scope, so it is a global variable.  Hence, after the loop, ''$squared'' will be undefined, but ''$cubed'' with have the value 64 (the last value it was assigned in the loop).&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
Arrays are represented with two different symbols, depending on whether you are referring to elements of the array, or the entire array.  Array variable names start with ''@'' when declaring them or when you are referring to the array itself.  The normal ''$'' is used when you want to access individual elements of the array.  Declaring arrays is fairly easy.  You can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
 $student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
 $student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
 $student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, lists strings or variables enclosed in parentheses, ''(value1,value2,...,valuen )'', are interpreted as arrays. You can access the contents with an array index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
You can also print the whole array:&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
The above will concatenate all the elements together without a delimiter.  Alternatively, you can print the array inside of a quotes&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print each elements separated by a space.&lt;br /&gt;
&lt;br /&gt;
The current size of an array can be accessed with the ''scalar()'' function:&lt;br /&gt;
&lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can also use special syntax to access the index of the last element in an array like so:&lt;br /&gt;
&lt;br /&gt;
 $last_index = $#student;&lt;br /&gt;
&lt;br /&gt;
Note that you start the array variable with ''$#'' instead of ''@'' or just ''$''.  In the example above, ''scalar(@student)'' is 4, and ''$#student'' is 3.&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
&lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be numbers or a characters:&lt;br /&gt;
&lt;br /&gt;
 @newarray = (1 .. 20);&lt;br /&gt;
 @newchars = ('l' .. 't');&lt;br /&gt;
&lt;br /&gt;
There is also support for adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''push()'' and ''unshift()'' take an array and a variable as arguments:&lt;br /&gt;
&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
VARIABLE can actually be a single variable or another array:&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student, @newstudents;&lt;br /&gt;
 push @student, (&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''pop()'' and ''shift()'' take only the array as an argument:&lt;br /&gt;
&lt;br /&gt;
 $laststudent = pop(@student);&lt;br /&gt;
&lt;br /&gt;
You can use the ''slice'' operation to retrieve portions of an array.&lt;br /&gt;
&lt;br /&gt;
 @newarray = @student[1..3]; # elements 1-3&lt;br /&gt;
 @newarray = @student[1..3,5,7,9..15]; # elements 1-3,5,7,9-15&lt;br /&gt;
&lt;br /&gt;
The ''splice()'' function lets you replace arbitrary elements within an array.  Here is the general syntax:&lt;br /&gt;
&lt;br /&gt;
 splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY);&lt;br /&gt;
  &lt;br /&gt;
Here is an example that replaces element 3 with ''Jerry'' and elements 4 with ''Lena'':&lt;br /&gt;
&lt;br /&gt;
 splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
The ''split()'' operation creates an array from a string. It takes a delimiter and a string as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The above will produce an array with 5 elements (''test'',''array'',''1'',''2'',''3'').&lt;br /&gt;
&lt;br /&gt;
The ''join()'' function does the reverse, i.e., it creates a string from the elements of an array. It takes a delimiter and an array as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
The above will form one string with every element from ''@student'' separated by a comma.&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays, which are similar to associate arrays. Whereas arrays are represented with the ''@'', hash arrays are represented by the ''%''symbol.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Leela&amp;quot;, 25,&lt;br /&gt;
         &amp;quot;Fry&amp;quot;, 28,&lt;br /&gt;
         &amp;quot;Bender&amp;quot;, 4,&lt;br /&gt;
         &amp;quot;Lord Nibbler&amp;quot;, &amp;quot;Unknown&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Elements within a hash array accessed by prefacing the hash array name with ''$'' and then using curly braces for the index.  For example:&lt;br /&gt;
&lt;br /&gt;
 $ages{&amp;quot;Leela&amp;quot;};	# Returns 25&lt;br /&gt;
 $ages{&amp;quot;Fry&amp;quot;};		# Returns 28&lt;br /&gt;
 $ages{&amp;quot;Bender&amp;quot;};	# Returns 4&lt;br /&gt;
 $ages{&amp;quot;Lord Nibbler&amp;quot;};	# Returns &amp;quot;Unknown&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
&lt;br /&gt;
''if'' statements work as they do in most other languages.  The general format is&lt;br /&gt;
&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #some statements&lt;br /&gt;
 }&lt;br /&gt;
 elsif ( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #whatever&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
   #other stuff&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
The standard comparison operators work as usual, including ''=='', ''&amp;lt;'', ''&amp;gt;'', etc.  The only thing to be aware of is that you should use ''eq'' and ''neq'' to compare strings instead of ''=='' and ''!=''.  &lt;br /&gt;
&lt;br /&gt;
 if($string eq &amp;quot;some string&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;strings are equal\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 elsif($int == 5)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;integer is five\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
&lt;br /&gt;
There are three main types of loops.  ''for'' and ''while'' loops work similarly to other languages.  Here are the general forms:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while(condition)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here are simple examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $sum=0;&lt;br /&gt;
 $i=1;&lt;br /&gt;
 while($i&amp;lt;=10)&lt;br /&gt;
 {&lt;br /&gt;
   $sum=$sum+$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print &amp;quot;sum is $sum\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''next'' and ''last'' are special commands that will jump to the next loop iteration, or to the end of the loop, respectively.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   if($j == 2) { next; }&lt;br /&gt;
   if($j == 4) { last; }&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
 j is 0&lt;br /&gt;
 j is 1&lt;br /&gt;
 j is 3&lt;br /&gt;
&lt;br /&gt;
Finally, there is the ''foreach'' loop.  ''foreach'' loops are most useful when iterating through arrays or hashs.  Here is the general form:&lt;br /&gt;
&lt;br /&gt;
 foreach $iterator (@array)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, to print out all the values in an array, you could do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @names = ('Bugs', 'Daffy', 'Marvin');&lt;br /&gt;
 foreach $name (@names)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$name\n&amp;quot;;&lt;br /&gt;
 } &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl functions are identified with the keyword ''sub'' followed by the function name. Arguments passed to functions are stored in a special array ''@_''.  You can either access all of the arguments by assigning them to local variables via ''@_'' or you can use the ''shift'' function.  Here is the general form for a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my $arg1 = shift;&lt;br /&gt;
   my $arg2 = shift;&lt;br /&gt;
   ...&lt;br /&gt;
   my $argn = shift;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is a simple function to add two numbers and a call to that function (note that functions can be defined above or below where they are called):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add&lt;br /&gt;
 {&lt;br /&gt;
   my ($x,$y) = @_;&lt;br /&gt;
   return ($x+$y);&lt;br /&gt;
 }  &lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;sum of 2+5=&amp;quot; . add(2,5) . &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are simply Perl libraries that provide new or enhanced functionality.  Although some modules are distributed as part of the core Perl distribution, most of them are written and maintained by other Perl users or developers who needed some specific functionality that was missing.  The [http://www.cpan.org CPAN] repository is the portal for finding and getting modules.  CPAN also contains manuals for the modules, most of which contain examples.  Generally speaking, the modules are all perl programs, although sometimes they require libraries from other languages.&lt;br /&gt;
&lt;br /&gt;
The typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl.  For current Ubuntu installs, the perl version is 5.10, so the modules are in ''/usr/lib/perl/5.10/''.  You can define other locations by setting the ''PERL5LIB'' environmental variable. &lt;br /&gt;
&lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
A module is contained in ''.pm'' files located in one of the module directories (either the default or through PERL5LIB).  They are accessed in a perl script by including them via the ''use'' command.  Most modules are grouped in to common packages. For example, there is a ''DBI'' package that contains many database interface modules.  One of those modules is the ''mysql'' module.  To include that module (assuming you have it installed on your system) in your script, you would add this line:&lt;br /&gt;
&lt;br /&gt;
 use DBI::mysql;&lt;br /&gt;
&lt;br /&gt;
If a module you want is not installed on your system, you need to install it either manually or through the automated ''cpan'' command.  The later approach takes a bit of set up, but it makes installing new modules much simpler.&lt;br /&gt;
&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
Search the CPAN website for the module you want, then download the module.  Untar it (or unzip it) and change to the directory containing the source code.  You will notice that there is a file named ''Makefile.PL''. It's a perl script that generates a makefile for your perl installation.  Run this:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile you need to build the module.  If you want to install the module to a non-default module location, set the PREFIX environment variable:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile, you can build the module, test it, and install it. Most Perl modules require testing to ensure they are functioning correctly on your system, so doing the tests is part of the build process.  If a critical component of the test fails, you may have to resolve any problems before moving on.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself.  In some cases, the dependencies can easily spiral out of control so that it becomes very difficult to install everything manually, which is why the CPAN approach is preferred. &lt;br /&gt;
&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that comes with the perl package that uses the CPAN perl module. It is a tool that installs new modules with minimum user interaction and that auto installs prerequisites.  To install new modules system-wide, you'll need to run ''cpan'' as root.  So, run:&lt;br /&gt;
 &lt;br /&gt;
 sudo cpan&lt;br /&gt;
&lt;br /&gt;
If it is the first time you are running ''cpan'', you will need to answer some configuration questions.  When prompted, you should select the automated configuration to get things going without any extra hassle.  You will then get to the ''cpan'' prompt, where you can run commands to search for and install new modules.&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The first thing should probably install is ''Bundle::CPAN'' which installs a bunch of packages that make cpan easier to use.  To do that, at the ''cpan&amp;gt;'' prompt, run:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' install Bundle::CPAN&lt;br /&gt;
&lt;br /&gt;
Note that as it installs, it will probably ask if it's ok to install additional packages and such along the way.  It's generally ok to accept whatever it asks when you are prompted.  The ''help'' command shows you all the available commands and options. You can search for a module with the ''i'' command:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
You can install a module with the ''install'' command:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' install MODULE&lt;br /&gt;
&lt;br /&gt;
''install'' downloads the module, compiles and tests it, and finally installs it if everything went well. The downloaded modules are stored under your ''~/.cpan '' directory (unless you specified another one during configuration), in case you need to look at source code or anything related to the module.  Although ''cpan'' will download any perl modules that are needed by the module you actually want to install, it might fail if any of the modules rely on non-perl resources elsewhere on the system (e.g., graphics libraries).  If that happens, you can go to the build directory, ''~/.cpan/build/MODULE'', and try to fix the problem.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect to a mysql server, you need to have the DBI module and DBD::mysql installed on your system.  The DBI is a generic database interface that is relatively easy to use, and a good overall description of the DBI is [http://www.perl.com/pub/a/1999/10/DBI.html here].&lt;br /&gt;
&lt;br /&gt;
Once you have the correct modules installed, you can connect to a mysql server with the function:&lt;br /&gt;
&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
&lt;br /&gt;
The database description contains the type of the database (so that the DBI knows how to connect), the name of the database and the host name.  It returns a handle that you will use to execute SQL statements on that database.  Here is an example to connect the grades databases on the localhost:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 use DBI;&lt;br /&gt;
 $dbh=DBI-&amp;gt;connect(&amp;quot;DBI:mysql:database=grades&amp;quot;,'dbuser','dbpass') or die &amp;quot;error connecting to database&amp;quot;;  #note that $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established, you can send SQL queries to the database server. First you need to prepare your SQL statement with the ''prepare'' function on the database handle:&lt;br /&gt;
&lt;br /&gt;
 $stmth = $dbh-&amp;gt;prepare(&amp;quot;SQL STATEMENT&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' returns a statement handle that you will use when executing or working with that statement.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Aladdin','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about quotation marks. It's usually simpler to use the ''qq'' function (which is just a generalized quotation mechanism):&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Aladdin','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with the ''execute'' function:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' also lets you bind variables so that you don't have to repeat them.  For example, if you want to have a generic way to insert new values, you can prepare your statement like this:&lt;br /&gt;
&lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Then, to execute the statement, you supply the values to fill in for each ''?'':&lt;br /&gt;
&lt;br /&gt;
 $inserth-&amp;gt;execute('Aladdin','Arabian Nights','Adventurer') or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
When executing statements that return data, the results are also accessed through the statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results.  This function returns an array, and you can access the columns in each sql row as members of the perl array. As ''fetchrow_array'' returns only one row, you call it continuously until it stops returning rows.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sql=qq(select name,jobtitle from employee);&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
 $sth-&amp;gt;execute() or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
 while(@row=$sth-&amp;gt;fetchrow_array)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$row[0] $row[1] \n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
The Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, having some attributes and children, and finishing with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements. For example, consider the following HTML page:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This document can be represented in a tree format:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hierarchical relationships in the tree. For example, the ''&amp;lt;html&amp;gt;'' node has two direct children, ''&amp;lt;head&amp;gt;'' and ''&amp;lt;body&amp;gt;'', and these elements have their own children. The Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree from an HTML document. You can find more about HTML::Trees [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod here]. In order to use ''HTML::Tree''s, you have to use the ''HTML::TreeBuilder'' module.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 use HTML::TreeBuilder;&lt;br /&gt;
 $tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will create a new tree for you. You can then use this tree to parse an HTML file. There are two ways to parse HTML, either directly with ''parse_file'', or by parsing individual strings with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can print the contents of an HTML::Tree by calling the ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before parsing new data, you need to empty the existing parsed data in the tree with the ''delete'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
This section details an example that captures science headlines from an HTML document from [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded the web page to ''bbc.html'', you can build a tree from it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information from the document. The ''look_down'' function on an HTML::Tree or HTML::Element is used to return any child satisfying some criterion:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, the following code will return the set of hyperlink HTML::Elements in the page, and put them into an array.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The return value can be a variable instead of array:&lt;br /&gt;
&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
In this case''look_down'' returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of the ''look_down'' function comes from the ability to specify further criteria like so:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has SOMEATTRIBUTE with value ITSVALUE, and the function specified in look_down returns 1.  Usually this function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contain URLs to cnn.com.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 if($_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;) =~ /www.cnn.com/)&lt;br /&gt;
                 {&lt;br /&gt;
                   return 1;&lt;br /&gt;
                 }&lt;br /&gt;
                 return 0;&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing the ''href'' attribute of the element and using a regular expression to match it against ''www.cnn.com''.&lt;br /&gt;
&lt;br /&gt;
To actually find news headlines, we need to determine the characteristics of the bbc.html file to discern what the headlines look like.  For example, this page has a main headline followed by a description:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
		        Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case, it's the hyperlinks that have class types ''tsh'' and ''shl'' that we are interested in, so we look down to find those class types:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/;&lt;br /&gt;
         });&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 foreach $link (@links)&lt;br /&gt;
 {&lt;br /&gt;
   print $link-&amp;gt;as_text;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines, not the summaries. If you look at the BBC html document, the headlines are inside a ''&amp;lt;div&amp;gt;'' element.  The summaries are in the following ''&amp;lt;div&amp;gt;''.  So, we can reach those summaries by first moving to the parent of the hyperlink HTML::Element and then going to the next sibling of that parent:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 foreach $link (@links)&lt;br /&gt;
 {&lt;br /&gt;
   print $link-&amp;gt;as_text;&lt;br /&gt;
   print $link-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, you have to be careful about these kind of operations.  If, for example, parent was undefined, the above code would be halted during execution with an error.  It's therefore important to always check if $link-&amp;gt;parent is actually defined before following the pointer.  Similarly, you would need to check if $link-&amp;gt;parent-&amp;gt;right was defined.&lt;br /&gt;
&lt;br /&gt;
Finally, you can use the LWP::UserAgent module to make web requests to get the contents of a page and pass those contents to the tree all in one perl script.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || # class name is tsh&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href') =~ m/science/) || # or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~ m/hl$/; # or their class name ends with hl&lt;br /&gt;
         });&lt;br /&gt;
&lt;br /&gt;
foreach $link (@links)&lt;br /&gt;
{&lt;br /&gt;
  print $link-&amp;gt;as_text . &amp;quot;:&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
  if( !($link-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;))&lt;br /&gt;
  {&lt;br /&gt;
    print $link-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text . &amp;quot;\n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
  else&lt;br /&gt;
  {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Additional examples can be found in the Perl documentation for Tree Scanning [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod here].&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=106</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=106"/>
		<updated>2009-10-26T02:59:07Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Parsing HTML with Perl */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language with syntax similar to bash and PHP.  It's a high level, general purpose language that is used primarily to automate repetitive tasks, and to parse and process text.  Indeed, text processing was the original intended use for the language, although it has grown to be an extremely flexible language for many different types of processing.&lt;br /&gt;
&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways.  The programs, or scripts, themselves are plain text files, usually ending in '.pl' to indicate that it is a Perl script.  Given a Perl script file, the most common ways to execute the script are shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pass the script as input to the 'perl' executable: &amp;lt;pre&amp;gt;perl -w SCRIPT&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Execute the script directly by adding the following line to the top of the script:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, change the script to be executable, and finally execute the script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x SCRIPT&lt;br /&gt;
./SCRIPT &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While flexibility is one of the major strengths of Perl, the flexibility in the language itself can often lead to unintended consequences.  To overcome this, it is good practice to explicitly enable warnings and strict behavior for you scripts.  The ''-w'' above when running perl gets you part of the way there, and although it's not necessary to supply the ''-w'' it is nearly always the right thing to do.  You should also add the line:&lt;br /&gt;
&lt;br /&gt;
 use strict;&lt;br /&gt;
&lt;br /&gt;
to all of your scripts, near the top, to enable strict error checking.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Standard variables start with a dollar sign, ''$'', at the beginning of their names.  Unlike shell scripts, the ''$'' is always used, regardless of whether the variable is being set or accessed.  As with other scripting languages, you don't specify the type of the variable; it will be used treated as a integer, floating point number, string, etc, depending on how it is used in your script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Comments start with the number/sharp/hash sign, ''#'', and extend until the end of the line.  All non-comment statements end with a semicolon, '';''.  You can print  variables with the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
Variables can be used inside strings, and the value of that variable will be included in the string. (Note that you can also use the ''printf'' function when you need specific formatting of the output).&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string:&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global by default, so you need to specify locality if you want local variables. This can be done with the ''local'' and ''my'' keywords. Generally, ''my'' is safer and faster, and is therefore used most often.  If a variable is defined by ''my'' in a block, it will be local to the block.  If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $i=0;&lt;br /&gt;
 while($i&amp;lt;5)&lt;br /&gt;
 {&lt;br /&gt;
   my $squared = $i*$i;&lt;br /&gt;
   $cubed = $i*$i*$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print $squared;&lt;br /&gt;
 print $cubed;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above program segment, ''$squared'' is defined with ''my'' inside the while loop, so it is only defined inside that loop.  ''$cubed'' is not given any special scope, so it is a global variable.  Hence, after the loop, ''$squared'' will be undefined, but ''$cubed'' with have the value 64 (the last value it was assigned in the loop).&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
Arrays are represented with two different symbols, depending on whether you are referring to elements of the array, or the entire array.  Array variable names start with ''@'' when declaring them or when you are referring to the array itself.  The normal ''$'' is used when you want to access individual elements of the array.  Declaring arrays is fairly easy.  You can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
 $student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
 $student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
 $student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, lists strings or variables enclosed in parentheses, ''(value1,value2,...,valuen )'', are interpreted as arrays. You can access the contents with an array index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
You can also print the whole array:&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
The above will concatenate all the elements together without a delimiter.  Alternatively, you can print the array inside of a quotes&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print each elements separated by a space.&lt;br /&gt;
&lt;br /&gt;
The current size of an array can be accessed with the ''scalar()'' function:&lt;br /&gt;
&lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can also use special syntax to access the index of the last element in an array like so:&lt;br /&gt;
&lt;br /&gt;
 $last_index = $#student;&lt;br /&gt;
&lt;br /&gt;
Note that you start the array variable with ''$#'' instead of ''@'' or just ''$''.  In the example above, ''scalar(@student)'' is 4, and ''$#student'' is 3.&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
&lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be numbers or a characters:&lt;br /&gt;
&lt;br /&gt;
 @newarray = (1 .. 20);&lt;br /&gt;
 @newchars = ('l' .. 't');&lt;br /&gt;
&lt;br /&gt;
There is also support for adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''push()'' and ''unshift()'' take an array and a variable as arguments:&lt;br /&gt;
&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
VARIABLE can actually be a single variable or another array:&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student, @newstudents;&lt;br /&gt;
 push @student, (&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''pop()'' and ''shift()'' take only the array as an argument:&lt;br /&gt;
&lt;br /&gt;
 $laststudent = pop(@student);&lt;br /&gt;
&lt;br /&gt;
You can use the ''slice'' operation to retrieve portions of an array.&lt;br /&gt;
&lt;br /&gt;
 @newarray = @student[1..3]; # elements 1-3&lt;br /&gt;
 @newarray = @student[1..3,5,7,9..15]; # elements 1-3,5,7,9-15&lt;br /&gt;
&lt;br /&gt;
The ''splice()'' function lets you replace arbitrary elements within an array.  Here is the general syntax:&lt;br /&gt;
&lt;br /&gt;
 splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY);&lt;br /&gt;
  &lt;br /&gt;
Here is an example that replaces element 3 with ''Jerry'' and elements 4 with ''Lena'':&lt;br /&gt;
&lt;br /&gt;
 splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
The ''split()'' operation creates an array from a string. It takes a delimiter and a string as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The above will produce an array with 5 elements (''test'',''array'',''1'',''2'',''3'').&lt;br /&gt;
&lt;br /&gt;
The ''join()'' function does the reverse, i.e., it creates a string from the elements of an array. It takes a delimiter and an array as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
The above will form one string with every element from ''@student'' separated by a comma.&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays, which are similar to associate arrays. Whereas arrays are represented with the ''@'', hash arrays are represented by the ''%''symbol.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Leela&amp;quot;, 25,&lt;br /&gt;
         &amp;quot;Fry&amp;quot;, 28,&lt;br /&gt;
         &amp;quot;Bender&amp;quot;, 4,&lt;br /&gt;
         &amp;quot;Lord Nibbler&amp;quot;, &amp;quot;Unknown&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Elements within a hash array accessed by prefacing the hash array name with ''$'' and then using curly braces for the index.  For example:&lt;br /&gt;
&lt;br /&gt;
 $ages{&amp;quot;Leela&amp;quot;};	# Returns 25&lt;br /&gt;
 $ages{&amp;quot;Fry&amp;quot;};		# Returns 28&lt;br /&gt;
 $ages{&amp;quot;Bender&amp;quot;};	# Returns 4&lt;br /&gt;
 $ages{&amp;quot;Lord Nibbler&amp;quot;};	# Returns &amp;quot;Unknown&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
&lt;br /&gt;
''if'' statements work as they do in most other languages.  The general format is&lt;br /&gt;
&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #some statements&lt;br /&gt;
 }&lt;br /&gt;
 elsif ( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #whatever&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
   #other stuff&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
The standard comparison operators work as usual, including ''=='', ''&amp;lt;'', ''&amp;gt;'', etc.  The only thing to be aware of is that you should use ''eq'' and ''neq'' to compare strings instead of ''=='' and ''!=''.  &lt;br /&gt;
&lt;br /&gt;
 if($string eq &amp;quot;some string&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;strings are equal\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 elsif($int == 5)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;integer is five\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
&lt;br /&gt;
There are three main types of loops.  ''for'' and ''while'' loops work similarly to other languages.  Here are the general forms:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while(condition)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here are simple examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $sum=0;&lt;br /&gt;
 $i=1;&lt;br /&gt;
 while($i&amp;lt;=10)&lt;br /&gt;
 {&lt;br /&gt;
   $sum=$sum+$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print &amp;quot;sum is $sum\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''next'' and ''last'' are special commands that will jump to the next loop iteration, or to the end of the loop, respectively.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   if($j == 2) { next; }&lt;br /&gt;
   if($j == 4) { last; }&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
 j is 0&lt;br /&gt;
 j is 1&lt;br /&gt;
 j is 3&lt;br /&gt;
&lt;br /&gt;
Finally, there is the ''foreach'' loop.  ''foreach'' loops are most useful when iterating through arrays or hashs.  Here is the general form:&lt;br /&gt;
&lt;br /&gt;
 foreach $iterator (@array)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, to print out all the values in an array, you could do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @names = ('Bugs', 'Daffy', 'Marvin');&lt;br /&gt;
 foreach $name (@names)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$name\n&amp;quot;;&lt;br /&gt;
 } &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl functions are identified with the keyword ''sub'' followed by the function name. Arguments passed to functions are stored in a special array ''@_''.  You can either access all of the arguments by assigning them to local variables via ''@_'' or you can use the ''shift'' function.  Here is the general form for a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my $arg1 = shift;&lt;br /&gt;
   my $arg2 = shift;&lt;br /&gt;
   ...&lt;br /&gt;
   my $argn = shift;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is a simple function to add two numbers and a call to that function (note that functions can be defined above or below where they are called):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add&lt;br /&gt;
 {&lt;br /&gt;
   my ($x,$y) = @_;&lt;br /&gt;
   return ($x+$y);&lt;br /&gt;
 }  &lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;sum of 2+5=&amp;quot; . add(2,5) . &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are simply Perl libraries that provide new or enhanced functionality.  Although some modules are distributed as part of the core Perl distribution, most of them are written and maintained by other Perl users or developers who needed some specific functionality that was missing.  The [http://www.cpan.org CPAN] repository is the portal for finding and getting modules.  CPAN also contains manuals for the modules, most of which contain examples.  Generally speaking, the modules are all perl programs, although sometimes they require libraries from other languages.&lt;br /&gt;
&lt;br /&gt;
The typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl.  For current Ubuntu installs, the perl version is 5.10, so the modules are in ''/usr/lib/perl/5.10/''.  You can define other locations by setting the ''PERL5LIB'' environmental variable. &lt;br /&gt;
&lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
A module is contained in ''.pm'' files located in one of the module directories (either the default or through PERL5LIB).  They are accessed in a perl script by including them via the ''use'' command.  Most modules are grouped in to common packages. For example, there is a ''DBI'' package that contains many database interface modules.  One of those modules is the ''mysql'' module.  To include that module (assuming you have it installed on your system) in your script, you would add this line:&lt;br /&gt;
&lt;br /&gt;
 use DBI::mysql;&lt;br /&gt;
&lt;br /&gt;
If a module you want is not installed on your system, you need to install it either manually or through the automated ''cpan'' command.  The later approach takes a bit of set up, but it makes installing new modules much simpler.&lt;br /&gt;
&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
Search the CPAN website for the module you want, then download the module.  Untar it (or unzip it) and change to the directory containing the source code.  You will notice that there is a file named ''Makefile.PL''. It's a perl script that generates a makefile for your perl installation.  Run this:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile you need to build the module.  If you want to install the module to a non-default module location, set the PREFIX environment variable:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile, you can build the module, test it, and install it. Most Perl modules require testing to ensure they are functioning correctly on your system, so doing the tests is part of the build process.  If a critical component of the test fails, you may have to resolve any problems before moving on.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself.  In some cases, the dependencies can easily spiral out of control so that it becomes very difficult to install everything manually, which is why the CPAN approach is preferred. &lt;br /&gt;
&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that comes with the perl package that uses the CPAN perl module. It is a tool that installs new modules with minimum user interaction and that auto installs prerequisites.  To install new modules system-wide, you'll need to run ''cpan'' as root.  So, run:&lt;br /&gt;
 &lt;br /&gt;
 sudo cpan&lt;br /&gt;
&lt;br /&gt;
If it is the first time you are running ''cpan'', you will need to answer some configuration questions.  When prompted, you should select the automated configuration to get things going without any extra hassle.  You will then get to the ''cpan'' prompt, where you can run commands to search for and install new modules.&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The first thing should probably install is ''Bundle::CPAN'' which installs a bunch of packages that make cpan easier to use.  To do that, at the ''cpan&amp;gt;'' prompt, run:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' install Bundle::CPAN&lt;br /&gt;
&lt;br /&gt;
Note that as it installs, it will probably ask if it's ok to install additional packages and such along the way.  It's generally ok to accept whatever it asks when you are prompted.  The ''help'' command shows you all the available commands and options. You can search for a module with the ''i'' command:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
You can install a module with the ''install'' command:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' install MODULE&lt;br /&gt;
&lt;br /&gt;
''install'' downloads the module, compiles and tests it, and finally installs it if everything went well. The downloaded modules are stored under your ''~/.cpan '' directory (unless you specified another one during configuration), in case you need to look at source code or anything related to the module.  Although ''cpan'' will download any perl modules that are needed by the module you actually want to install, it might fail if any of the modules rely on non-perl resources elsewhere on the system (e.g., graphics libraries).  If that happens, you can go to the build directory, ''~/.cpan/build/MODULE'', and try to fix the problem.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect to a mysql server, you need to have the DBI module and DBD::mysql installed on your system.  The DBI is a generic database interface that is relatively easy to use, and a good overall description of the DBI is [http://www.perl.com/pub/a/1999/10/DBI.html here].&lt;br /&gt;
&lt;br /&gt;
Once you have the correct modules installed, you can connect to a mysql server with the function:&lt;br /&gt;
&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
&lt;br /&gt;
The database description contains the type of the database (so that the DBI knows how to connect), the name of the database and the host name.  It returns a handle that you will use to execute SQL statements on that database.  Here is an example to connect the grades databases on the localhost:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 use DBI;&lt;br /&gt;
 $dbh=DBI-&amp;gt;connect(&amp;quot;DBI:mysql:database=grades&amp;quot;,'dbuser','dbpass') or die &amp;quot;error connecting to database&amp;quot;;  #note that $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established, you can send SQL queries to the database server. First you need to prepare your SQL statement with the ''prepare'' function on the database handle:&lt;br /&gt;
&lt;br /&gt;
 $stmth = $dbh-&amp;gt;prepare(&amp;quot;SQL STATEMENT&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' returns a statement handle that you will use when executing or working with that statement.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Aladdin','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about quotation marks. It's usually simpler to use the ''qq'' function (which is just a generalized quotation mechanism):&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Aladdin','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with the ''execute'' function:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' also lets you bind variables so that you don't have to repeat them.  For example, if you want to have a generic way to insert new values, you can prepare your statement like this:&lt;br /&gt;
&lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Then, to execute the statement, you supply the values to fill in for each ''?'':&lt;br /&gt;
&lt;br /&gt;
 $inserth-&amp;gt;execute('Aladdin','Arabian Nights','Adventurer') or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
When executing statements that return data, the results are also accessed through the statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results.  This function returns an array, and you can access the columns in each sql row as members of the perl array. As ''fetchrow_array'' returns only one row, you call it continuously until it stops returning rows.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sql=qq(select name,jobtitle from employee);&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
 $sth-&amp;gt;execute() or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
 while(@row=$sth-&amp;gt;fetchrow_array)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$row[0] $row[1] \n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
The Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, having some attributes and children, and finishing with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements. For example, consider the following HTML page:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This document can be represented in a tree format:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hierarchical relationships in the tree. For example, the ''&amp;lt;html&amp;gt;'' node has two direct children, ''&amp;lt;head&amp;gt;'' and ''&amp;lt;body&amp;gt;'', and these elements have their own children. The Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree from an HTML document. You can find more about HTML::Trees [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod here]. In order to use ''HTML::Tree''s, you have to use the ''HTML::TreeBuilder'' module.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 use HTML::TreeBuilder;&lt;br /&gt;
 $tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will create a new tree for you. You can then use this tree to parse an HTML file. There are two ways to parse HTML, either directly with ''parse_file'', or by parsing individual strings with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can print the contents of an HTML::Tree by calling the ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before parsing new data, you need to empty the existing parsed data in the tree with the ''delete'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
Lets say we are interested in capturing headlines for  science news at  [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded this web page to ''bbc.html'', you can build a tree on it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information. Function ''look_down'' on an HTML::Tree or HTML::Element will return any child satisfying some criteria&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, following code will return all  HTML::Elements that are actually  hyperlinks in the page  and put it to an array.&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
left hand side of the call could be a variable instead of array,&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
In this case, instead of returning all links, it returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of look_down function comes from ability to specify further criteria in the following format:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has its SOMEATTRIBUTE has ITVALUE and the function specified in look_down returns 1. Usually that function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contains URLs to cse330 website.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 $_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;)=~m/www.cse330.org/&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing ''href'' attribute and using a regular expression to match it to ''www.cse330.org''.&lt;br /&gt;
&lt;br /&gt;
Now, as we said we are interested in capturing the BBC news headlines, we need to look at bbc.html to see what are the characteristics of the HTML::Elements containing the headlines.&lt;br /&gt;
&lt;br /&gt;
For example, in the current website, the main headline has the following lines (headline followed by a description)&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
....&lt;br /&gt;
            &lt;br /&gt;
		&lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence if we look down on the hyperlinks that have  class types ''tsh'' and ''shl'', we will get the hyperlinks that contain the headlines.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines but not the summaries. But no worries, if you look at the BBC html code, the headlines are inside a &amp;lt;div&amp;gt; element, and the summaries are in the next &amp;lt;div&amp;gt;. So we can reach those summaries by first moving to parent of the hyperlink and reaching the next sibling of that parent.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
  print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you should be careful about these kind of operations, if, for example parent was undefined, the above code was going to be interrupted during run time. A better way is  first to check if $links[$i]-&amp;gt;parent was defined and then check if parent's right sibling was defined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, you can use LWP::UserAgent to actually make a request to the website, get the contents and pass those contents to the tree, instead of downloading the a website and calling the parser  on that file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || #that are tsh  class&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href')=~m/science/&lt;br /&gt;
             )             ||# or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/ #or their class name ends with hl&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
 if( !($links[$i]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;)) {print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;}&lt;br /&gt;
  else {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some other examples can be found at Perl documentation for Tree Scanning at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod]&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=105</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=105"/>
		<updated>2009-10-26T02:58:04Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Parsing HTML with Perl */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language with syntax similar to bash and PHP.  It's a high level, general purpose language that is used primarily to automate repetitive tasks, and to parse and process text.  Indeed, text processing was the original intended use for the language, although it has grown to be an extremely flexible language for many different types of processing.&lt;br /&gt;
&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways.  The programs, or scripts, themselves are plain text files, usually ending in '.pl' to indicate that it is a Perl script.  Given a Perl script file, the most common ways to execute the script are shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pass the script as input to the 'perl' executable: &amp;lt;pre&amp;gt;perl -w SCRIPT&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Execute the script directly by adding the following line to the top of the script:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, change the script to be executable, and finally execute the script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x SCRIPT&lt;br /&gt;
./SCRIPT &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While flexibility is one of the major strengths of Perl, the flexibility in the language itself can often lead to unintended consequences.  To overcome this, it is good practice to explicitly enable warnings and strict behavior for you scripts.  The ''-w'' above when running perl gets you part of the way there, and although it's not necessary to supply the ''-w'' it is nearly always the right thing to do.  You should also add the line:&lt;br /&gt;
&lt;br /&gt;
 use strict;&lt;br /&gt;
&lt;br /&gt;
to all of your scripts, near the top, to enable strict error checking.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Standard variables start with a dollar sign, ''$'', at the beginning of their names.  Unlike shell scripts, the ''$'' is always used, regardless of whether the variable is being set or accessed.  As with other scripting languages, you don't specify the type of the variable; it will be used treated as a integer, floating point number, string, etc, depending on how it is used in your script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Comments start with the number/sharp/hash sign, ''#'', and extend until the end of the line.  All non-comment statements end with a semicolon, '';''.  You can print  variables with the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
Variables can be used inside strings, and the value of that variable will be included in the string. (Note that you can also use the ''printf'' function when you need specific formatting of the output).&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string:&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global by default, so you need to specify locality if you want local variables. This can be done with the ''local'' and ''my'' keywords. Generally, ''my'' is safer and faster, and is therefore used most often.  If a variable is defined by ''my'' in a block, it will be local to the block.  If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $i=0;&lt;br /&gt;
 while($i&amp;lt;5)&lt;br /&gt;
 {&lt;br /&gt;
   my $squared = $i*$i;&lt;br /&gt;
   $cubed = $i*$i*$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print $squared;&lt;br /&gt;
 print $cubed;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above program segment, ''$squared'' is defined with ''my'' inside the while loop, so it is only defined inside that loop.  ''$cubed'' is not given any special scope, so it is a global variable.  Hence, after the loop, ''$squared'' will be undefined, but ''$cubed'' with have the value 64 (the last value it was assigned in the loop).&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
Arrays are represented with two different symbols, depending on whether you are referring to elements of the array, or the entire array.  Array variable names start with ''@'' when declaring them or when you are referring to the array itself.  The normal ''$'' is used when you want to access individual elements of the array.  Declaring arrays is fairly easy.  You can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
 $student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
 $student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
 $student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, lists strings or variables enclosed in parentheses, ''(value1,value2,...,valuen )'', are interpreted as arrays. You can access the contents with an array index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
You can also print the whole array:&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
The above will concatenate all the elements together without a delimiter.  Alternatively, you can print the array inside of a quotes&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print each elements separated by a space.&lt;br /&gt;
&lt;br /&gt;
The current size of an array can be accessed with the ''scalar()'' function:&lt;br /&gt;
&lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can also use special syntax to access the index of the last element in an array like so:&lt;br /&gt;
&lt;br /&gt;
 $last_index = $#student;&lt;br /&gt;
&lt;br /&gt;
Note that you start the array variable with ''$#'' instead of ''@'' or just ''$''.  In the example above, ''scalar(@student)'' is 4, and ''$#student'' is 3.&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
&lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be numbers or a characters:&lt;br /&gt;
&lt;br /&gt;
 @newarray = (1 .. 20);&lt;br /&gt;
 @newchars = ('l' .. 't');&lt;br /&gt;
&lt;br /&gt;
There is also support for adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''push()'' and ''unshift()'' take an array and a variable as arguments:&lt;br /&gt;
&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
VARIABLE can actually be a single variable or another array:&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student, @newstudents;&lt;br /&gt;
 push @student, (&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''pop()'' and ''shift()'' take only the array as an argument:&lt;br /&gt;
&lt;br /&gt;
 $laststudent = pop(@student);&lt;br /&gt;
&lt;br /&gt;
You can use the ''slice'' operation to retrieve portions of an array.&lt;br /&gt;
&lt;br /&gt;
 @newarray = @student[1..3]; # elements 1-3&lt;br /&gt;
 @newarray = @student[1..3,5,7,9..15]; # elements 1-3,5,7,9-15&lt;br /&gt;
&lt;br /&gt;
The ''splice()'' function lets you replace arbitrary elements within an array.  Here is the general syntax:&lt;br /&gt;
&lt;br /&gt;
 splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY);&lt;br /&gt;
  &lt;br /&gt;
Here is an example that replaces element 3 with ''Jerry'' and elements 4 with ''Lena'':&lt;br /&gt;
&lt;br /&gt;
 splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
The ''split()'' operation creates an array from a string. It takes a delimiter and a string as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The above will produce an array with 5 elements (''test'',''array'',''1'',''2'',''3'').&lt;br /&gt;
&lt;br /&gt;
The ''join()'' function does the reverse, i.e., it creates a string from the elements of an array. It takes a delimiter and an array as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
The above will form one string with every element from ''@student'' separated by a comma.&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays, which are similar to associate arrays. Whereas arrays are represented with the ''@'', hash arrays are represented by the ''%''symbol.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Leela&amp;quot;, 25,&lt;br /&gt;
         &amp;quot;Fry&amp;quot;, 28,&lt;br /&gt;
         &amp;quot;Bender&amp;quot;, 4,&lt;br /&gt;
         &amp;quot;Lord Nibbler&amp;quot;, &amp;quot;Unknown&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Elements within a hash array accessed by prefacing the hash array name with ''$'' and then using curly braces for the index.  For example:&lt;br /&gt;
&lt;br /&gt;
 $ages{&amp;quot;Leela&amp;quot;};	# Returns 25&lt;br /&gt;
 $ages{&amp;quot;Fry&amp;quot;};		# Returns 28&lt;br /&gt;
 $ages{&amp;quot;Bender&amp;quot;};	# Returns 4&lt;br /&gt;
 $ages{&amp;quot;Lord Nibbler&amp;quot;};	# Returns &amp;quot;Unknown&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
&lt;br /&gt;
''if'' statements work as they do in most other languages.  The general format is&lt;br /&gt;
&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #some statements&lt;br /&gt;
 }&lt;br /&gt;
 elsif ( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #whatever&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
   #other stuff&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
The standard comparison operators work as usual, including ''=='', ''&amp;lt;'', ''&amp;gt;'', etc.  The only thing to be aware of is that you should use ''eq'' and ''neq'' to compare strings instead of ''=='' and ''!=''.  &lt;br /&gt;
&lt;br /&gt;
 if($string eq &amp;quot;some string&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;strings are equal\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 elsif($int == 5)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;integer is five\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
&lt;br /&gt;
There are three main types of loops.  ''for'' and ''while'' loops work similarly to other languages.  Here are the general forms:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while(condition)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here are simple examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $sum=0;&lt;br /&gt;
 $i=1;&lt;br /&gt;
 while($i&amp;lt;=10)&lt;br /&gt;
 {&lt;br /&gt;
   $sum=$sum+$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print &amp;quot;sum is $sum\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''next'' and ''last'' are special commands that will jump to the next loop iteration, or to the end of the loop, respectively.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   if($j == 2) { next; }&lt;br /&gt;
   if($j == 4) { last; }&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
 j is 0&lt;br /&gt;
 j is 1&lt;br /&gt;
 j is 3&lt;br /&gt;
&lt;br /&gt;
Finally, there is the ''foreach'' loop.  ''foreach'' loops are most useful when iterating through arrays or hashs.  Here is the general form:&lt;br /&gt;
&lt;br /&gt;
 foreach $iterator (@array)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, to print out all the values in an array, you could do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @names = ('Bugs', 'Daffy', 'Marvin');&lt;br /&gt;
 foreach $name (@names)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$name\n&amp;quot;;&lt;br /&gt;
 } &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl functions are identified with the keyword ''sub'' followed by the function name. Arguments passed to functions are stored in a special array ''@_''.  You can either access all of the arguments by assigning them to local variables via ''@_'' or you can use the ''shift'' function.  Here is the general form for a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my $arg1 = shift;&lt;br /&gt;
   my $arg2 = shift;&lt;br /&gt;
   ...&lt;br /&gt;
   my $argn = shift;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is a simple function to add two numbers and a call to that function (note that functions can be defined above or below where they are called):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add&lt;br /&gt;
 {&lt;br /&gt;
   my ($x,$y) = @_;&lt;br /&gt;
   return ($x+$y);&lt;br /&gt;
 }  &lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;sum of 2+5=&amp;quot; . add(2,5) . &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are simply Perl libraries that provide new or enhanced functionality.  Although some modules are distributed as part of the core Perl distribution, most of them are written and maintained by other Perl users or developers who needed some specific functionality that was missing.  The [http://www.cpan.org CPAN] repository is the portal for finding and getting modules.  CPAN also contains manuals for the modules, most of which contain examples.  Generally speaking, the modules are all perl programs, although sometimes they require libraries from other languages.&lt;br /&gt;
&lt;br /&gt;
The typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl.  For current Ubuntu installs, the perl version is 5.10, so the modules are in ''/usr/lib/perl/5.10/''.  You can define other locations by setting the ''PERL5LIB'' environmental variable. &lt;br /&gt;
&lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
A module is contained in ''.pm'' files located in one of the module directories (either the default or through PERL5LIB).  They are accessed in a perl script by including them via the ''use'' command.  Most modules are grouped in to common packages. For example, there is a ''DBI'' package that contains many database interface modules.  One of those modules is the ''mysql'' module.  To include that module (assuming you have it installed on your system) in your script, you would add this line:&lt;br /&gt;
&lt;br /&gt;
 use DBI::mysql;&lt;br /&gt;
&lt;br /&gt;
If a module you want is not installed on your system, you need to install it either manually or through the automated ''cpan'' command.  The later approach takes a bit of set up, but it makes installing new modules much simpler.&lt;br /&gt;
&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
Search the CPAN website for the module you want, then download the module.  Untar it (or unzip it) and change to the directory containing the source code.  You will notice that there is a file named ''Makefile.PL''. It's a perl script that generates a makefile for your perl installation.  Run this:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile you need to build the module.  If you want to install the module to a non-default module location, set the PREFIX environment variable:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile, you can build the module, test it, and install it. Most Perl modules require testing to ensure they are functioning correctly on your system, so doing the tests is part of the build process.  If a critical component of the test fails, you may have to resolve any problems before moving on.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself.  In some cases, the dependencies can easily spiral out of control so that it becomes very difficult to install everything manually, which is why the CPAN approach is preferred. &lt;br /&gt;
&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that comes with the perl package that uses the CPAN perl module. It is a tool that installs new modules with minimum user interaction and that auto installs prerequisites.  To install new modules system-wide, you'll need to run ''cpan'' as root.  So, run:&lt;br /&gt;
 &lt;br /&gt;
 sudo cpan&lt;br /&gt;
&lt;br /&gt;
If it is the first time you are running ''cpan'', you will need to answer some configuration questions.  When prompted, you should select the automated configuration to get things going without any extra hassle.  You will then get to the ''cpan'' prompt, where you can run commands to search for and install new modules.&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The first thing should probably install is ''Bundle::CPAN'' which installs a bunch of packages that make cpan easier to use.  To do that, at the ''cpan&amp;gt;'' prompt, run:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' install Bundle::CPAN&lt;br /&gt;
&lt;br /&gt;
Note that as it installs, it will probably ask if it's ok to install additional packages and such along the way.  It's generally ok to accept whatever it asks when you are prompted.  The ''help'' command shows you all the available commands and options. You can search for a module with the ''i'' command:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
You can install a module with the ''install'' command:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' install MODULE&lt;br /&gt;
&lt;br /&gt;
''install'' downloads the module, compiles and tests it, and finally installs it if everything went well. The downloaded modules are stored under your ''~/.cpan '' directory (unless you specified another one during configuration), in case you need to look at source code or anything related to the module.  Although ''cpan'' will download any perl modules that are needed by the module you actually want to install, it might fail if any of the modules rely on non-perl resources elsewhere on the system (e.g., graphics libraries).  If that happens, you can go to the build directory, ''~/.cpan/build/MODULE'', and try to fix the problem.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect to a mysql server, you need to have the DBI module and DBD::mysql installed on your system.  The DBI is a generic database interface that is relatively easy to use, and a good overall description of the DBI is [http://www.perl.com/pub/a/1999/10/DBI.html here].&lt;br /&gt;
&lt;br /&gt;
Once you have the correct modules installed, you can connect to a mysql server with the function:&lt;br /&gt;
&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
&lt;br /&gt;
The database description contains the type of the database (so that the DBI knows how to connect), the name of the database and the host name.  It returns a handle that you will use to execute SQL statements on that database.  Here is an example to connect the grades databases on the localhost:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 use DBI;&lt;br /&gt;
 $dbh=DBI-&amp;gt;connect(&amp;quot;DBI:mysql:database=grades&amp;quot;,'dbuser','dbpass') or die &amp;quot;error connecting to database&amp;quot;;  #note that $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established, you can send SQL queries to the database server. First you need to prepare your SQL statement with the ''prepare'' function on the database handle:&lt;br /&gt;
&lt;br /&gt;
 $stmth = $dbh-&amp;gt;prepare(&amp;quot;SQL STATEMENT&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' returns a statement handle that you will use when executing or working with that statement.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Aladdin','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about quotation marks. It's usually simpler to use the ''qq'' function (which is just a generalized quotation mechanism):&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Aladdin','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with the ''execute'' function:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' also lets you bind variables so that you don't have to repeat them.  For example, if you want to have a generic way to insert new values, you can prepare your statement like this:&lt;br /&gt;
&lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Then, to execute the statement, you supply the values to fill in for each ''?'':&lt;br /&gt;
&lt;br /&gt;
 $inserth-&amp;gt;execute('Aladdin','Arabian Nights','Adventurer') or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
When executing statements that return data, the results are also accessed through the statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results.  This function returns an array, and you can access the columns in each sql row as members of the perl array. As ''fetchrow_array'' returns only one row, you call it continuously until it stops returning rows.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sql=qq(select name,jobtitle from employee);&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
 $sth-&amp;gt;execute() or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
 while(@row=$sth-&amp;gt;fetchrow_array)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$row[0] $row[1] \n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
The Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, having some attributes and children, and finishing with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements. For example, consider the following HTML page:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This document can be represented in a tree format:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hierarchical relationships in the tree. For example, the ''&amp;lt;html&amp;gt;'' node has two direct children, ''&amp;lt;head&amp;gt;'' and ''&amp;lt;body&amp;gt;'', and these elements have their own children. The Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree from an HTML document. You can find more about HTML::Trees [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod here]. In order to use ''HTML::Tree''s, you have to use the ''HTML::TreeBuilder'' module.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
 use HTML::TreeBuilder;&lt;br /&gt;
 $tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will create a new tree for you. You can then use this tree to parse an HTML file. There are two ways to parse HTML, either directly with ''parse_file'', or by parsing individual strings with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can print the contents of an HTML::Tree by calling the ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before parsing new data, you need to empty the existing parsed data in the tree with the ''delete'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
Lets say we are interested in capturing headlines for  science news at  [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded this web page to ''bbc.html'', you can build a tree on it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information. Function ''look_down'' on an HTML::Tree or HTML::Element will return any child satisfying some criteria&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, following code will return all  HTML::Elements that are actually  hyperlinks in the page  and put it to an array.&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
left hand side of the call could be a variable instead of array,&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
In this case, instead of returning all links, it returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of look_down function comes from ability to specify further criteria in the following format:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has its SOMEATTRIBUTE has ITVALUE and the function specified in look_down returns 1. Usually that function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contains URLs to cse330 website.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 $_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;)=~m/www.cse330.org/&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing ''href'' attribute and using a regular expression to match it to ''www.cse330.org''.&lt;br /&gt;
&lt;br /&gt;
Now, as we said we are interested in capturing the BBC news headlines, we need to look at bbc.html to see what are the characteristics of the HTML::Elements containing the headlines.&lt;br /&gt;
&lt;br /&gt;
For example, in the current website, the main headline has the following lines (headline followed by a description)&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
....&lt;br /&gt;
            &lt;br /&gt;
		&lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence if we look down on the hyperlinks that have  class types ''tsh'' and ''shl'', we will get the hyperlinks that contain the headlines.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines but not the summaries. But no worries, if you look at the BBC html code, the headlines are inside a &amp;lt;div&amp;gt; element, and the summaries are in the next &amp;lt;div&amp;gt;. So we can reach those summaries by first moving to parent of the hyperlink and reaching the next sibling of that parent.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
  print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you should be careful about these kind of operations, if, for example parent was undefined, the above code was going to be interrupted during run time. A better way is  first to check if $links[$i]-&amp;gt;parent was defined and then check if parent's right sibling was defined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, you can use LWP::UserAgent to actually make a request to the website, get the contents and pass those contents to the tree, instead of downloading the a website and calling the parser  on that file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || #that are tsh  class&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href')=~m/science/&lt;br /&gt;
             )             ||# or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/ #or their class name ends with hl&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
 if( !($links[$i]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;)) {print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;}&lt;br /&gt;
  else {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some other examples can be found at Perl documentation for Tree Scanning at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod]&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=104</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=104"/>
		<updated>2009-10-26T02:48:15Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Connecting MySQL with Perl */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language with syntax similar to bash and PHP.  It's a high level, general purpose language that is used primarily to automate repetitive tasks, and to parse and process text.  Indeed, text processing was the original intended use for the language, although it has grown to be an extremely flexible language for many different types of processing.&lt;br /&gt;
&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways.  The programs, or scripts, themselves are plain text files, usually ending in '.pl' to indicate that it is a Perl script.  Given a Perl script file, the most common ways to execute the script are shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pass the script as input to the 'perl' executable: &amp;lt;pre&amp;gt;perl -w SCRIPT&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Execute the script directly by adding the following line to the top of the script:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, change the script to be executable, and finally execute the script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x SCRIPT&lt;br /&gt;
./SCRIPT &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While flexibility is one of the major strengths of Perl, the flexibility in the language itself can often lead to unintended consequences.  To overcome this, it is good practice to explicitly enable warnings and strict behavior for you scripts.  The ''-w'' above when running perl gets you part of the way there, and although it's not necessary to supply the ''-w'' it is nearly always the right thing to do.  You should also add the line:&lt;br /&gt;
&lt;br /&gt;
 use strict;&lt;br /&gt;
&lt;br /&gt;
to all of your scripts, near the top, to enable strict error checking.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Standard variables start with a dollar sign, ''$'', at the beginning of their names.  Unlike shell scripts, the ''$'' is always used, regardless of whether the variable is being set or accessed.  As with other scripting languages, you don't specify the type of the variable; it will be used treated as a integer, floating point number, string, etc, depending on how it is used in your script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Comments start with the number/sharp/hash sign, ''#'', and extend until the end of the line.  All non-comment statements end with a semicolon, '';''.  You can print  variables with the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
Variables can be used inside strings, and the value of that variable will be included in the string. (Note that you can also use the ''printf'' function when you need specific formatting of the output).&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string:&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global by default, so you need to specify locality if you want local variables. This can be done with the ''local'' and ''my'' keywords. Generally, ''my'' is safer and faster, and is therefore used most often.  If a variable is defined by ''my'' in a block, it will be local to the block.  If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $i=0;&lt;br /&gt;
 while($i&amp;lt;5)&lt;br /&gt;
 {&lt;br /&gt;
   my $squared = $i*$i;&lt;br /&gt;
   $cubed = $i*$i*$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print $squared;&lt;br /&gt;
 print $cubed;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above program segment, ''$squared'' is defined with ''my'' inside the while loop, so it is only defined inside that loop.  ''$cubed'' is not given any special scope, so it is a global variable.  Hence, after the loop, ''$squared'' will be undefined, but ''$cubed'' with have the value 64 (the last value it was assigned in the loop).&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
Arrays are represented with two different symbols, depending on whether you are referring to elements of the array, or the entire array.  Array variable names start with ''@'' when declaring them or when you are referring to the array itself.  The normal ''$'' is used when you want to access individual elements of the array.  Declaring arrays is fairly easy.  You can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
 $student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
 $student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
 $student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, lists strings or variables enclosed in parentheses, ''(value1,value2,...,valuen )'', are interpreted as arrays. You can access the contents with an array index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
You can also print the whole array:&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
The above will concatenate all the elements together without a delimiter.  Alternatively, you can print the array inside of a quotes&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print each elements separated by a space.&lt;br /&gt;
&lt;br /&gt;
The current size of an array can be accessed with the ''scalar()'' function:&lt;br /&gt;
&lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can also use special syntax to access the index of the last element in an array like so:&lt;br /&gt;
&lt;br /&gt;
 $last_index = $#student;&lt;br /&gt;
&lt;br /&gt;
Note that you start the array variable with ''$#'' instead of ''@'' or just ''$''.  In the example above, ''scalar(@student)'' is 4, and ''$#student'' is 3.&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
&lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be numbers or a characters:&lt;br /&gt;
&lt;br /&gt;
 @newarray = (1 .. 20);&lt;br /&gt;
 @newchars = ('l' .. 't');&lt;br /&gt;
&lt;br /&gt;
There is also support for adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''push()'' and ''unshift()'' take an array and a variable as arguments:&lt;br /&gt;
&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
VARIABLE can actually be a single variable or another array:&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student, @newstudents;&lt;br /&gt;
 push @student, (&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''pop()'' and ''shift()'' take only the array as an argument:&lt;br /&gt;
&lt;br /&gt;
 $laststudent = pop(@student);&lt;br /&gt;
&lt;br /&gt;
You can use the ''slice'' operation to retrieve portions of an array.&lt;br /&gt;
&lt;br /&gt;
 @newarray = @student[1..3]; # elements 1-3&lt;br /&gt;
 @newarray = @student[1..3,5,7,9..15]; # elements 1-3,5,7,9-15&lt;br /&gt;
&lt;br /&gt;
The ''splice()'' function lets you replace arbitrary elements within an array.  Here is the general syntax:&lt;br /&gt;
&lt;br /&gt;
 splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY);&lt;br /&gt;
  &lt;br /&gt;
Here is an example that replaces element 3 with ''Jerry'' and elements 4 with ''Lena'':&lt;br /&gt;
&lt;br /&gt;
 splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
The ''split()'' operation creates an array from a string. It takes a delimiter and a string as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The above will produce an array with 5 elements (''test'',''array'',''1'',''2'',''3'').&lt;br /&gt;
&lt;br /&gt;
The ''join()'' function does the reverse, i.e., it creates a string from the elements of an array. It takes a delimiter and an array as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
The above will form one string with every element from ''@student'' separated by a comma.&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays, which are similar to associate arrays. Whereas arrays are represented with the ''@'', hash arrays are represented by the ''%''symbol.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Leela&amp;quot;, 25,&lt;br /&gt;
         &amp;quot;Fry&amp;quot;, 28,&lt;br /&gt;
         &amp;quot;Bender&amp;quot;, 4,&lt;br /&gt;
         &amp;quot;Lord Nibbler&amp;quot;, &amp;quot;Unknown&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Elements within a hash array accessed by prefacing the hash array name with ''$'' and then using curly braces for the index.  For example:&lt;br /&gt;
&lt;br /&gt;
 $ages{&amp;quot;Leela&amp;quot;};	# Returns 25&lt;br /&gt;
 $ages{&amp;quot;Fry&amp;quot;};		# Returns 28&lt;br /&gt;
 $ages{&amp;quot;Bender&amp;quot;};	# Returns 4&lt;br /&gt;
 $ages{&amp;quot;Lord Nibbler&amp;quot;};	# Returns &amp;quot;Unknown&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
&lt;br /&gt;
''if'' statements work as they do in most other languages.  The general format is&lt;br /&gt;
&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #some statements&lt;br /&gt;
 }&lt;br /&gt;
 elsif ( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #whatever&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
   #other stuff&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
The standard comparison operators work as usual, including ''=='', ''&amp;lt;'', ''&amp;gt;'', etc.  The only thing to be aware of is that you should use ''eq'' and ''neq'' to compare strings instead of ''=='' and ''!=''.  &lt;br /&gt;
&lt;br /&gt;
 if($string eq &amp;quot;some string&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;strings are equal\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 elsif($int == 5)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;integer is five\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
&lt;br /&gt;
There are three main types of loops.  ''for'' and ''while'' loops work similarly to other languages.  Here are the general forms:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while(condition)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here are simple examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $sum=0;&lt;br /&gt;
 $i=1;&lt;br /&gt;
 while($i&amp;lt;=10)&lt;br /&gt;
 {&lt;br /&gt;
   $sum=$sum+$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print &amp;quot;sum is $sum\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''next'' and ''last'' are special commands that will jump to the next loop iteration, or to the end of the loop, respectively.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   if($j == 2) { next; }&lt;br /&gt;
   if($j == 4) { last; }&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
 j is 0&lt;br /&gt;
 j is 1&lt;br /&gt;
 j is 3&lt;br /&gt;
&lt;br /&gt;
Finally, there is the ''foreach'' loop.  ''foreach'' loops are most useful when iterating through arrays or hashs.  Here is the general form:&lt;br /&gt;
&lt;br /&gt;
 foreach $iterator (@array)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, to print out all the values in an array, you could do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @names = ('Bugs', 'Daffy', 'Marvin');&lt;br /&gt;
 foreach $name (@names)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$name\n&amp;quot;;&lt;br /&gt;
 } &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl functions are identified with the keyword ''sub'' followed by the function name. Arguments passed to functions are stored in a special array ''@_''.  You can either access all of the arguments by assigning them to local variables via ''@_'' or you can use the ''shift'' function.  Here is the general form for a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my $arg1 = shift;&lt;br /&gt;
   my $arg2 = shift;&lt;br /&gt;
   ...&lt;br /&gt;
   my $argn = shift;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is a simple function to add two numbers and a call to that function (note that functions can be defined above or below where they are called):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add&lt;br /&gt;
 {&lt;br /&gt;
   my ($x,$y) = @_;&lt;br /&gt;
   return ($x+$y);&lt;br /&gt;
 }  &lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;sum of 2+5=&amp;quot; . add(2,5) . &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are simply Perl libraries that provide new or enhanced functionality.  Although some modules are distributed as part of the core Perl distribution, most of them are written and maintained by other Perl users or developers who needed some specific functionality that was missing.  The [http://www.cpan.org CPAN] repository is the portal for finding and getting modules.  CPAN also contains manuals for the modules, most of which contain examples.  Generally speaking, the modules are all perl programs, although sometimes they require libraries from other languages.&lt;br /&gt;
&lt;br /&gt;
The typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl.  For current Ubuntu installs, the perl version is 5.10, so the modules are in ''/usr/lib/perl/5.10/''.  You can define other locations by setting the ''PERL5LIB'' environmental variable. &lt;br /&gt;
&lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
A module is contained in ''.pm'' files located in one of the module directories (either the default or through PERL5LIB).  They are accessed in a perl script by including them via the ''use'' command.  Most modules are grouped in to common packages. For example, there is a ''DBI'' package that contains many database interface modules.  One of those modules is the ''mysql'' module.  To include that module (assuming you have it installed on your system) in your script, you would add this line:&lt;br /&gt;
&lt;br /&gt;
 use DBI::mysql;&lt;br /&gt;
&lt;br /&gt;
If a module you want is not installed on your system, you need to install it either manually or through the automated ''cpan'' command.  The later approach takes a bit of set up, but it makes installing new modules much simpler.&lt;br /&gt;
&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
Search the CPAN website for the module you want, then download the module.  Untar it (or unzip it) and change to the directory containing the source code.  You will notice that there is a file named ''Makefile.PL''. It's a perl script that generates a makefile for your perl installation.  Run this:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile you need to build the module.  If you want to install the module to a non-default module location, set the PREFIX environment variable:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile, you can build the module, test it, and install it. Most Perl modules require testing to ensure they are functioning correctly on your system, so doing the tests is part of the build process.  If a critical component of the test fails, you may have to resolve any problems before moving on.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself.  In some cases, the dependencies can easily spiral out of control so that it becomes very difficult to install everything manually, which is why the CPAN approach is preferred. &lt;br /&gt;
&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that comes with the perl package that uses the CPAN perl module. It is a tool that installs new modules with minimum user interaction and that auto installs prerequisites.  To install new modules system-wide, you'll need to run ''cpan'' as root.  So, run:&lt;br /&gt;
 &lt;br /&gt;
 sudo cpan&lt;br /&gt;
&lt;br /&gt;
If it is the first time you are running ''cpan'', you will need to answer some configuration questions.  When prompted, you should select the automated configuration to get things going without any extra hassle.  You will then get to the ''cpan'' prompt, where you can run commands to search for and install new modules.&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The first thing should probably install is ''Bundle::CPAN'' which installs a bunch of packages that make cpan easier to use.  To do that, at the ''cpan&amp;gt;'' prompt, run:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' install Bundle::CPAN&lt;br /&gt;
&lt;br /&gt;
Note that as it installs, it will probably ask if it's ok to install additional packages and such along the way.  It's generally ok to accept whatever it asks when you are prompted.  The ''help'' command shows you all the available commands and options. You can search for a module with the ''i'' command:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
You can install a module with the ''install'' command:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' install MODULE&lt;br /&gt;
&lt;br /&gt;
''install'' downloads the module, compiles and tests it, and finally installs it if everything went well. The downloaded modules are stored under your ''~/.cpan '' directory (unless you specified another one during configuration), in case you need to look at source code or anything related to the module.  Although ''cpan'' will download any perl modules that are needed by the module you actually want to install, it might fail if any of the modules rely on non-perl resources elsewhere on the system (e.g., graphics libraries).  If that happens, you can go to the build directory, ''~/.cpan/build/MODULE'', and try to fix the problem.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect to a mysql server, you need to have the DBI module and DBD::mysql installed on your system.  The DBI is a generic database interface that is relatively easy to use, and a good overall description of the DBI is [http://www.perl.com/pub/a/1999/10/DBI.html here].&lt;br /&gt;
&lt;br /&gt;
Once you have the correct modules installed, you can connect to a mysql server with the function:&lt;br /&gt;
&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
&lt;br /&gt;
The database description contains the type of the database (so that the DBI knows how to connect), the name of the database and the host name.  It returns a handle that you will use to execute SQL statements on that database.  Here is an example to connect the grades databases on the localhost:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 use DBI;&lt;br /&gt;
 $dbh=DBI-&amp;gt;connect(&amp;quot;DBI:mysql:database=grades&amp;quot;,'dbuser','dbpass') or die &amp;quot;error connecting to database&amp;quot;;  #note that $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established, you can send SQL queries to the database server. First you need to prepare your SQL statement with the ''prepare'' function on the database handle:&lt;br /&gt;
&lt;br /&gt;
 $stmth = $dbh-&amp;gt;prepare(&amp;quot;SQL STATEMENT&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' returns a statement handle that you will use when executing or working with that statement.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Aladdin','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about quotation marks. It's usually simpler to use the ''qq'' function (which is just a generalized quotation mechanism):&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Aladdin','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with the ''execute'' function:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' also lets you bind variables so that you don't have to repeat them.  For example, if you want to have a generic way to insert new values, you can prepare your statement like this:&lt;br /&gt;
&lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Then, to execute the statement, you supply the values to fill in for each ''?'':&lt;br /&gt;
&lt;br /&gt;
 $inserth-&amp;gt;execute('Aladdin','Arabian Nights','Adventurer') or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
When executing statements that return data, the results are also accessed through the statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results.  This function returns an array, and you can access the columns in each sql row as members of the perl array. As ''fetchrow_array'' returns only one row, you call it continuously until it stops returning rows.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sql=qq(select name,jobtitle from employee);&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
 $sth-&amp;gt;execute() or die &amp;quot;problem executing statement&amp;quot;;&lt;br /&gt;
 while(@row=$sth-&amp;gt;fetchrow_array)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$row[0] $row[1] \n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, have some attributes and finish with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements.&lt;br /&gt;
&lt;br /&gt;
For example, consider the following HTML page&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code can be represented in a tree format&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hiararchical relation. For example, &amp;lt;html&amp;gt; node has two direct children, &amp;lt;head&amp;gt; and &amp;lt;body&amp;gt; and these siblings have their own children. Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree. You can find more about HTML::Trees at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod]].&lt;br /&gt;
In order to use ''HTML::Tree''s, you have to use ''HTML::TreeBuilder'' module.&lt;br /&gt;
 use ''HTML::TreeBuilder'';&lt;br /&gt;
 $tree=TML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
will create a new tree for you. You can then use this tree parse an html file. There are two ways to parse HTML, either directly parse a file with ''parse_file'' or parse a string with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can dump the contents of an HTML::Tree by calling ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before doing that you need to empty the existing parse data stored in the tree with ''delete'' function, otherwise, you may run out of memory.&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
Lets say we are interested in capturing headlines for  science news at  [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded this web page to ''bbc.html'', you can build a tree on it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information. Function ''look_down'' on an HTML::Tree or HTML::Element will return any child satisfying some criteria&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, following code will return all  HTML::Elements that are actually  hyperlinks in the page  and put it to an array.&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
left hand side of the call could be a variable instead of array,&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
In this case, instead of returning all links, it returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of look_down function comes from ability to specify further criteria in the following format:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has its SOMEATTRIBUTE has ITVALUE and the function specified in look_down returns 1. Usually that function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contains URLs to cse330 website.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 $_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;)=~m/www.cse330.org/&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing ''href'' attribute and using a regular expression to match it to ''www.cse330.org''.&lt;br /&gt;
&lt;br /&gt;
Now, as we said we are interested in capturing the BBC news headlines, we need to look at bbc.html to see what are the characteristics of the HTML::Elements containing the headlines.&lt;br /&gt;
&lt;br /&gt;
For example, in the current website, the main headline has the following lines (headline followed by a description)&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
....&lt;br /&gt;
            &lt;br /&gt;
		&lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence if we look down on the hyperlinks that have  class types ''tsh'' and ''shl'', we will get the hyperlinks that contain the headlines.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines but not the summaries. But no worries, if you look at the BBC html code, the headlines are inside a &amp;lt;div&amp;gt; element, and the summaries are in the next &amp;lt;div&amp;gt;. So we can reach those summaries by first moving to parent of the hyperlink and reaching the next sibling of that parent.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
  print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you should be careful about these kind of operations, if, for example parent was undefined, the above code was going to be interrupted during run time. A better way is  first to check if $links[$i]-&amp;gt;parent was defined and then check if parent's right sibling was defined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, you can use LWP::UserAgent to actually make a request to the website, get the contents and pass those contents to the tree, instead of downloading the a website and calling the parser  on that file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || #that are tsh  class&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href')=~m/science/&lt;br /&gt;
             )             ||# or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/ #or their class name ends with hl&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
 if( !($links[$i]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;)) {print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;}&lt;br /&gt;
  else {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some other examples can be found at Perl documentation for Tree Scanning at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod]&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=103</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=103"/>
		<updated>2009-10-26T02:24:43Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Automated Module Installation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language with syntax similar to bash and PHP.  It's a high level, general purpose language that is used primarily to automate repetitive tasks, and to parse and process text.  Indeed, text processing was the original intended use for the language, although it has grown to be an extremely flexible language for many different types of processing.&lt;br /&gt;
&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways.  The programs, or scripts, themselves are plain text files, usually ending in '.pl' to indicate that it is a Perl script.  Given a Perl script file, the most common ways to execute the script are shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pass the script as input to the 'perl' executable: &amp;lt;pre&amp;gt;perl -w SCRIPT&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Execute the script directly by adding the following line to the top of the script:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, change the script to be executable, and finally execute the script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x SCRIPT&lt;br /&gt;
./SCRIPT &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While flexibility is one of the major strengths of Perl, the flexibility in the language itself can often lead to unintended consequences.  To overcome this, it is good practice to explicitly enable warnings and strict behavior for you scripts.  The ''-w'' above when running perl gets you part of the way there, and although it's not necessary to supply the ''-w'' it is nearly always the right thing to do.  You should also add the line:&lt;br /&gt;
&lt;br /&gt;
 use strict;&lt;br /&gt;
&lt;br /&gt;
to all of your scripts, near the top, to enable strict error checking.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Standard variables start with a dollar sign, ''$'', at the beginning of their names.  Unlike shell scripts, the ''$'' is always used, regardless of whether the variable is being set or accessed.  As with other scripting languages, you don't specify the type of the variable; it will be used treated as a integer, floating point number, string, etc, depending on how it is used in your script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Comments start with the number/sharp/hash sign, ''#'', and extend until the end of the line.  All non-comment statements end with a semicolon, '';''.  You can print  variables with the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
Variables can be used inside strings, and the value of that variable will be included in the string. (Note that you can also use the ''printf'' function when you need specific formatting of the output).&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string:&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global by default, so you need to specify locality if you want local variables. This can be done with the ''local'' and ''my'' keywords. Generally, ''my'' is safer and faster, and is therefore used most often.  If a variable is defined by ''my'' in a block, it will be local to the block.  If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $i=0;&lt;br /&gt;
 while($i&amp;lt;5)&lt;br /&gt;
 {&lt;br /&gt;
   my $squared = $i*$i;&lt;br /&gt;
   $cubed = $i*$i*$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print $squared;&lt;br /&gt;
 print $cubed;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above program segment, ''$squared'' is defined with ''my'' inside the while loop, so it is only defined inside that loop.  ''$cubed'' is not given any special scope, so it is a global variable.  Hence, after the loop, ''$squared'' will be undefined, but ''$cubed'' with have the value 64 (the last value it was assigned in the loop).&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
Arrays are represented with two different symbols, depending on whether you are referring to elements of the array, or the entire array.  Array variable names start with ''@'' when declaring them or when you are referring to the array itself.  The normal ''$'' is used when you want to access individual elements of the array.  Declaring arrays is fairly easy.  You can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
 $student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
 $student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
 $student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, lists strings or variables enclosed in parentheses, ''(value1,value2,...,valuen )'', are interpreted as arrays. You can access the contents with an array index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
You can also print the whole array:&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
The above will concatenate all the elements together without a delimiter.  Alternatively, you can print the array inside of a quotes&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print each elements separated by a space.&lt;br /&gt;
&lt;br /&gt;
The current size of an array can be accessed with the ''scalar()'' function:&lt;br /&gt;
&lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can also use special syntax to access the index of the last element in an array like so:&lt;br /&gt;
&lt;br /&gt;
 $last_index = $#student;&lt;br /&gt;
&lt;br /&gt;
Note that you start the array variable with ''$#'' instead of ''@'' or just ''$''.  In the example above, ''scalar(@student)'' is 4, and ''$#student'' is 3.&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
&lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be numbers or a characters:&lt;br /&gt;
&lt;br /&gt;
 @newarray = (1 .. 20);&lt;br /&gt;
 @newchars = ('l' .. 't');&lt;br /&gt;
&lt;br /&gt;
There is also support for adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''push()'' and ''unshift()'' take an array and a variable as arguments:&lt;br /&gt;
&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
VARIABLE can actually be a single variable or another array:&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student, @newstudents;&lt;br /&gt;
 push @student, (&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''pop()'' and ''shift()'' take only the array as an argument:&lt;br /&gt;
&lt;br /&gt;
 $laststudent = pop(@student);&lt;br /&gt;
&lt;br /&gt;
You can use the ''slice'' operation to retrieve portions of an array.&lt;br /&gt;
&lt;br /&gt;
 @newarray = @student[1..3]; # elements 1-3&lt;br /&gt;
 @newarray = @student[1..3,5,7,9..15]; # elements 1-3,5,7,9-15&lt;br /&gt;
&lt;br /&gt;
The ''splice()'' function lets you replace arbitrary elements within an array.  Here is the general syntax:&lt;br /&gt;
&lt;br /&gt;
 splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY);&lt;br /&gt;
  &lt;br /&gt;
Here is an example that replaces element 3 with ''Jerry'' and elements 4 with ''Lena'':&lt;br /&gt;
&lt;br /&gt;
 splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
The ''split()'' operation creates an array from a string. It takes a delimiter and a string as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The above will produce an array with 5 elements (''test'',''array'',''1'',''2'',''3'').&lt;br /&gt;
&lt;br /&gt;
The ''join()'' function does the reverse, i.e., it creates a string from the elements of an array. It takes a delimiter and an array as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
The above will form one string with every element from ''@student'' separated by a comma.&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays, which are similar to associate arrays. Whereas arrays are represented with the ''@'', hash arrays are represented by the ''%''symbol.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Leela&amp;quot;, 25,&lt;br /&gt;
         &amp;quot;Fry&amp;quot;, 28,&lt;br /&gt;
         &amp;quot;Bender&amp;quot;, 4,&lt;br /&gt;
         &amp;quot;Lord Nibbler&amp;quot;, &amp;quot;Unknown&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Elements within a hash array accessed by prefacing the hash array name with ''$'' and then using curly braces for the index.  For example:&lt;br /&gt;
&lt;br /&gt;
 $ages{&amp;quot;Leela&amp;quot;};	# Returns 25&lt;br /&gt;
 $ages{&amp;quot;Fry&amp;quot;};		# Returns 28&lt;br /&gt;
 $ages{&amp;quot;Bender&amp;quot;};	# Returns 4&lt;br /&gt;
 $ages{&amp;quot;Lord Nibbler&amp;quot;};	# Returns &amp;quot;Unknown&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
&lt;br /&gt;
''if'' statements work as they do in most other languages.  The general format is&lt;br /&gt;
&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #some statements&lt;br /&gt;
 }&lt;br /&gt;
 elsif ( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #whatever&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
   #other stuff&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
The standard comparison operators work as usual, including ''=='', ''&amp;lt;'', ''&amp;gt;'', etc.  The only thing to be aware of is that you should use ''eq'' and ''neq'' to compare strings instead of ''=='' and ''!=''.  &lt;br /&gt;
&lt;br /&gt;
 if($string eq &amp;quot;some string&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;strings are equal\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 elsif($int == 5)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;integer is five\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
&lt;br /&gt;
There are three main types of loops.  ''for'' and ''while'' loops work similarly to other languages.  Here are the general forms:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while(condition)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here are simple examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $sum=0;&lt;br /&gt;
 $i=1;&lt;br /&gt;
 while($i&amp;lt;=10)&lt;br /&gt;
 {&lt;br /&gt;
   $sum=$sum+$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print &amp;quot;sum is $sum\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''next'' and ''last'' are special commands that will jump to the next loop iteration, or to the end of the loop, respectively.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   if($j == 2) { next; }&lt;br /&gt;
   if($j == 4) { last; }&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
 j is 0&lt;br /&gt;
 j is 1&lt;br /&gt;
 j is 3&lt;br /&gt;
&lt;br /&gt;
Finally, there is the ''foreach'' loop.  ''foreach'' loops are most useful when iterating through arrays or hashs.  Here is the general form:&lt;br /&gt;
&lt;br /&gt;
 foreach $iterator (@array)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, to print out all the values in an array, you could do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @names = ('Bugs', 'Daffy', 'Marvin');&lt;br /&gt;
 foreach $name (@names)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$name\n&amp;quot;;&lt;br /&gt;
 } &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl functions are identified with the keyword ''sub'' followed by the function name. Arguments passed to functions are stored in a special array ''@_''.  You can either access all of the arguments by assigning them to local variables via ''@_'' or you can use the ''shift'' function.  Here is the general form for a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my $arg1 = shift;&lt;br /&gt;
   my $arg2 = shift;&lt;br /&gt;
   ...&lt;br /&gt;
   my $argn = shift;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is a simple function to add two numbers and a call to that function (note that functions can be defined above or below where they are called):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add&lt;br /&gt;
 {&lt;br /&gt;
   my ($x,$y) = @_;&lt;br /&gt;
   return ($x+$y);&lt;br /&gt;
 }  &lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;sum of 2+5=&amp;quot; . add(2,5) . &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are simply Perl libraries that provide new or enhanced functionality.  Although some modules are distributed as part of the core Perl distribution, most of them are written and maintained by other Perl users or developers who needed some specific functionality that was missing.  The [http://www.cpan.org CPAN] repository is the portal for finding and getting modules.  CPAN also contains manuals for the modules, most of which contain examples.  Generally speaking, the modules are all perl programs, although sometimes they require libraries from other languages.&lt;br /&gt;
&lt;br /&gt;
The typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl.  For current Ubuntu installs, the perl version is 5.10, so the modules are in ''/usr/lib/perl/5.10/''.  You can define other locations by setting the ''PERL5LIB'' environmental variable. &lt;br /&gt;
&lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
A module is contained in ''.pm'' files located in one of the module directories (either the default or through PERL5LIB).  They are accessed in a perl script by including them via the ''use'' command.  Most modules are grouped in to common packages. For example, there is a ''DBI'' package that contains many database interface modules.  One of those modules is the ''mysql'' module.  To include that module (assuming you have it installed on your system) in your script, you would add this line:&lt;br /&gt;
&lt;br /&gt;
 use DBI::mysql;&lt;br /&gt;
&lt;br /&gt;
If a module you want is not installed on your system, you need to install it either manually or through the automated ''cpan'' command.  The later approach takes a bit of set up, but it makes installing new modules much simpler.&lt;br /&gt;
&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
Search the CPAN website for the module you want, then download the module.  Untar it (or unzip it) and change to the directory containing the source code.  You will notice that there is a file named ''Makefile.PL''. It's a perl script that generates a makefile for your perl installation.  Run this:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile you need to build the module.  If you want to install the module to a non-default module location, set the PREFIX environment variable:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile, you can build the module, test it, and install it. Most Perl modules require testing to ensure they are functioning correctly on your system, so doing the tests is part of the build process.  If a critical component of the test fails, you may have to resolve any problems before moving on.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself.  In some cases, the dependencies can easily spiral out of control so that it becomes very difficult to install everything manually, which is why the CPAN approach is preferred. &lt;br /&gt;
&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that comes with the perl package that uses the CPAN perl module. It is a tool that installs new modules with minimum user interaction and that auto installs prerequisites.  To install new modules system-wide, you'll need to run ''cpan'' as root.  So, run:&lt;br /&gt;
 &lt;br /&gt;
 sudo cpan&lt;br /&gt;
&lt;br /&gt;
If it is the first time you are running ''cpan'', you will need to answer some configuration questions.  When prompted, you should select the automated configuration to get things going without any extra hassle.  You will then get to the ''cpan'' prompt, where you can run commands to search for and install new modules.&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The first thing should probably install is ''Bundle::CPAN'' which installs a bunch of packages that make cpan easier to use.  To do that, at the ''cpan&amp;gt;'' prompt, run:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' install Bundle::CPAN&lt;br /&gt;
&lt;br /&gt;
Note that as it installs, it will probably ask if it's ok to install additional packages and such along the way.  It's generally ok to accept whatever it asks when you are prompted.  The ''help'' command shows you all the available commands and options. You can search for a module with the ''i'' command:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
You can install a module with the ''install'' command:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' install MODULE&lt;br /&gt;
&lt;br /&gt;
''install'' downloads the module, compiles and tests it, and finally installs it if everything went well. The downloaded modules are stored under your ''~/.cpan '' directory (unless you specified another one during configuration), in case you need to look at source code or anything related to the module.  Although ''cpan'' will download any perl modules that are needed by the module you actually want to install, it might fail if any of the modules rely on non-perl resources elsewhere on the system (e.g., graphics libraries).  If that happens, you can go to the build directory, ''~/.cpan/build/MODULE'', and try to fix the problem.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect a MySQL you need to have DBI module and DBI::mysql installed in your system. Following guide has a good description of DBI interface in general [http://www.perl.com/pub/a/1999/10/DBI.html]&lt;br /&gt;
&lt;br /&gt;
Then you can connect a mysql database with the command&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
The databse description contains the type of the database (so that DBI knows how to connect), the name of the database and the host name. It returns a handle that you will use for this connection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;; #note $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established you can send SQL queries to your database server. First you need to prepare your SQL with ''prepare'' command on the database handle.&lt;br /&gt;
&lt;br /&gt;
$dbh-&amp;gt;prepare(&amp;quot;TYPE YOUR SQL HERE&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually returns a statement handle ($sth in our example below) that you will use for the database operations.&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Alaaddin'','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about the your quotation marks. An easier way is to use perl qq function (which is  generalize quotation)&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Alaaddin'','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with execute command:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() ||die &amp;quot;Can't execute sql&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually helps you bind variables so that you don't have to repeat them. For example, if you want to have a generic way to insert new values, you can prepare your statement &lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
and during execution specify the arguments represented with ''?''&lt;br /&gt;
 $inserth-&amp;gt;execute('Alaaddin'','Arabian Nights','Adventurer')|| die &amp;quot;Can't execute SQL&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you are querying your database, the query results are also accessed through your statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results. This function returns an array, so you can access any attribute from this array. As ''fetchrow_array'' returns only one row, you can call it continuously until it fails to return a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$sql=&amp;quot;insert into employee values ('aladdin','Arabian Nights','Adventurer')&amp;quot;; # sql for inserting&lt;br /&gt;
$sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
$sth-&amp;gt;finish();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, have some attributes and finish with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements.&lt;br /&gt;
&lt;br /&gt;
For example, consider the following HTML page&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code can be represented in a tree format&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hiararchical relation. For example, &amp;lt;html&amp;gt; node has two direct children, &amp;lt;head&amp;gt; and &amp;lt;body&amp;gt; and these siblings have their own children. Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree. You can find more about HTML::Trees at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod]].&lt;br /&gt;
In order to use ''HTML::Tree''s, you have to use ''HTML::TreeBuilder'' module.&lt;br /&gt;
 use ''HTML::TreeBuilder'';&lt;br /&gt;
 $tree=TML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
will create a new tree for you. You can then use this tree parse an html file. There are two ways to parse HTML, either directly parse a file with ''parse_file'' or parse a string with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can dump the contents of an HTML::Tree by calling ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before doing that you need to empty the existing parse data stored in the tree with ''delete'' function, otherwise, you may run out of memory.&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
Lets say we are interested in capturing headlines for  science news at  [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded this web page to ''bbc.html'', you can build a tree on it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information. Function ''look_down'' on an HTML::Tree or HTML::Element will return any child satisfying some criteria&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, following code will return all  HTML::Elements that are actually  hyperlinks in the page  and put it to an array.&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
left hand side of the call could be a variable instead of array,&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
In this case, instead of returning all links, it returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of look_down function comes from ability to specify further criteria in the following format:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has its SOMEATTRIBUTE has ITVALUE and the function specified in look_down returns 1. Usually that function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contains URLs to cse330 website.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 $_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;)=~m/www.cse330.org/&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing ''href'' attribute and using a regular expression to match it to ''www.cse330.org''.&lt;br /&gt;
&lt;br /&gt;
Now, as we said we are interested in capturing the BBC news headlines, we need to look at bbc.html to see what are the characteristics of the HTML::Elements containing the headlines.&lt;br /&gt;
&lt;br /&gt;
For example, in the current website, the main headline has the following lines (headline followed by a description)&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
....&lt;br /&gt;
            &lt;br /&gt;
		&lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence if we look down on the hyperlinks that have  class types ''tsh'' and ''shl'', we will get the hyperlinks that contain the headlines.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines but not the summaries. But no worries, if you look at the BBC html code, the headlines are inside a &amp;lt;div&amp;gt; element, and the summaries are in the next &amp;lt;div&amp;gt;. So we can reach those summaries by first moving to parent of the hyperlink and reaching the next sibling of that parent.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
  print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you should be careful about these kind of operations, if, for example parent was undefined, the above code was going to be interrupted during run time. A better way is  first to check if $links[$i]-&amp;gt;parent was defined and then check if parent's right sibling was defined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, you can use LWP::UserAgent to actually make a request to the website, get the contents and pass those contents to the tree, instead of downloading the a website and calling the parser  on that file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || #that are tsh  class&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href')=~m/science/&lt;br /&gt;
             )             ||# or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/ #or their class name ends with hl&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
 if( !($links[$i]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;)) {print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;}&lt;br /&gt;
  else {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some other examples can be found at Perl documentation for Tree Scanning at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod]&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=102</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=102"/>
		<updated>2009-10-26T02:11:51Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Perl Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language with syntax similar to bash and PHP.  It's a high level, general purpose language that is used primarily to automate repetitive tasks, and to parse and process text.  Indeed, text processing was the original intended use for the language, although it has grown to be an extremely flexible language for many different types of processing.&lt;br /&gt;
&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways.  The programs, or scripts, themselves are plain text files, usually ending in '.pl' to indicate that it is a Perl script.  Given a Perl script file, the most common ways to execute the script are shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pass the script as input to the 'perl' executable: &amp;lt;pre&amp;gt;perl -w SCRIPT&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Execute the script directly by adding the following line to the top of the script:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, change the script to be executable, and finally execute the script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x SCRIPT&lt;br /&gt;
./SCRIPT &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While flexibility is one of the major strengths of Perl, the flexibility in the language itself can often lead to unintended consequences.  To overcome this, it is good practice to explicitly enable warnings and strict behavior for you scripts.  The ''-w'' above when running perl gets you part of the way there, and although it's not necessary to supply the ''-w'' it is nearly always the right thing to do.  You should also add the line:&lt;br /&gt;
&lt;br /&gt;
 use strict;&lt;br /&gt;
&lt;br /&gt;
to all of your scripts, near the top, to enable strict error checking.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Standard variables start with a dollar sign, ''$'', at the beginning of their names.  Unlike shell scripts, the ''$'' is always used, regardless of whether the variable is being set or accessed.  As with other scripting languages, you don't specify the type of the variable; it will be used treated as a integer, floating point number, string, etc, depending on how it is used in your script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Comments start with the number/sharp/hash sign, ''#'', and extend until the end of the line.  All non-comment statements end with a semicolon, '';''.  You can print  variables with the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
Variables can be used inside strings, and the value of that variable will be included in the string. (Note that you can also use the ''printf'' function when you need specific formatting of the output).&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string:&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global by default, so you need to specify locality if you want local variables. This can be done with the ''local'' and ''my'' keywords. Generally, ''my'' is safer and faster, and is therefore used most often.  If a variable is defined by ''my'' in a block, it will be local to the block.  If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $i=0;&lt;br /&gt;
 while($i&amp;lt;5)&lt;br /&gt;
 {&lt;br /&gt;
   my $squared = $i*$i;&lt;br /&gt;
   $cubed = $i*$i*$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print $squared;&lt;br /&gt;
 print $cubed;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above program segment, ''$squared'' is defined with ''my'' inside the while loop, so it is only defined inside that loop.  ''$cubed'' is not given any special scope, so it is a global variable.  Hence, after the loop, ''$squared'' will be undefined, but ''$cubed'' with have the value 64 (the last value it was assigned in the loop).&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
Arrays are represented with two different symbols, depending on whether you are referring to elements of the array, or the entire array.  Array variable names start with ''@'' when declaring them or when you are referring to the array itself.  The normal ''$'' is used when you want to access individual elements of the array.  Declaring arrays is fairly easy.  You can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
 $student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
 $student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
 $student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, lists strings or variables enclosed in parentheses, ''(value1,value2,...,valuen )'', are interpreted as arrays. You can access the contents with an array index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
You can also print the whole array:&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
The above will concatenate all the elements together without a delimiter.  Alternatively, you can print the array inside of a quotes&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print each elements separated by a space.&lt;br /&gt;
&lt;br /&gt;
The current size of an array can be accessed with the ''scalar()'' function:&lt;br /&gt;
&lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can also use special syntax to access the index of the last element in an array like so:&lt;br /&gt;
&lt;br /&gt;
 $last_index = $#student;&lt;br /&gt;
&lt;br /&gt;
Note that you start the array variable with ''$#'' instead of ''@'' or just ''$''.  In the example above, ''scalar(@student)'' is 4, and ''$#student'' is 3.&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
&lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be numbers or a characters:&lt;br /&gt;
&lt;br /&gt;
 @newarray = (1 .. 20);&lt;br /&gt;
 @newchars = ('l' .. 't');&lt;br /&gt;
&lt;br /&gt;
There is also support for adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''push()'' and ''unshift()'' take an array and a variable as arguments:&lt;br /&gt;
&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
VARIABLE can actually be a single variable or another array:&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student, @newstudents;&lt;br /&gt;
 push @student, (&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''pop()'' and ''shift()'' take only the array as an argument:&lt;br /&gt;
&lt;br /&gt;
 $laststudent = pop(@student);&lt;br /&gt;
&lt;br /&gt;
You can use the ''slice'' operation to retrieve portions of an array.&lt;br /&gt;
&lt;br /&gt;
 @newarray = @student[1..3]; # elements 1-3&lt;br /&gt;
 @newarray = @student[1..3,5,7,9..15]; # elements 1-3,5,7,9-15&lt;br /&gt;
&lt;br /&gt;
The ''splice()'' function lets you replace arbitrary elements within an array.  Here is the general syntax:&lt;br /&gt;
&lt;br /&gt;
 splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY);&lt;br /&gt;
  &lt;br /&gt;
Here is an example that replaces element 3 with ''Jerry'' and elements 4 with ''Lena'':&lt;br /&gt;
&lt;br /&gt;
 splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
The ''split()'' operation creates an array from a string. It takes a delimiter and a string as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The above will produce an array with 5 elements (''test'',''array'',''1'',''2'',''3'').&lt;br /&gt;
&lt;br /&gt;
The ''join()'' function does the reverse, i.e., it creates a string from the elements of an array. It takes a delimiter and an array as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
The above will form one string with every element from ''@student'' separated by a comma.&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays, which are similar to associate arrays. Whereas arrays are represented with the ''@'', hash arrays are represented by the ''%''symbol.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Leela&amp;quot;, 25,&lt;br /&gt;
         &amp;quot;Fry&amp;quot;, 28,&lt;br /&gt;
         &amp;quot;Bender&amp;quot;, 4,&lt;br /&gt;
         &amp;quot;Lord Nibbler&amp;quot;, &amp;quot;Unknown&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Elements within a hash array accessed by prefacing the hash array name with ''$'' and then using curly braces for the index.  For example:&lt;br /&gt;
&lt;br /&gt;
 $ages{&amp;quot;Leela&amp;quot;};	# Returns 25&lt;br /&gt;
 $ages{&amp;quot;Fry&amp;quot;};		# Returns 28&lt;br /&gt;
 $ages{&amp;quot;Bender&amp;quot;};	# Returns 4&lt;br /&gt;
 $ages{&amp;quot;Lord Nibbler&amp;quot;};	# Returns &amp;quot;Unknown&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
&lt;br /&gt;
''if'' statements work as they do in most other languages.  The general format is&lt;br /&gt;
&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #some statements&lt;br /&gt;
 }&lt;br /&gt;
 elsif ( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #whatever&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
   #other stuff&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
The standard comparison operators work as usual, including ''=='', ''&amp;lt;'', ''&amp;gt;'', etc.  The only thing to be aware of is that you should use ''eq'' and ''neq'' to compare strings instead of ''=='' and ''!=''.  &lt;br /&gt;
&lt;br /&gt;
 if($string eq &amp;quot;some string&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;strings are equal\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 elsif($int == 5)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;integer is five\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
&lt;br /&gt;
There are three main types of loops.  ''for'' and ''while'' loops work similarly to other languages.  Here are the general forms:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while(condition)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here are simple examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $sum=0;&lt;br /&gt;
 $i=1;&lt;br /&gt;
 while($i&amp;lt;=10)&lt;br /&gt;
 {&lt;br /&gt;
   $sum=$sum+$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print &amp;quot;sum is $sum\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''next'' and ''last'' are special commands that will jump to the next loop iteration, or to the end of the loop, respectively.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   if($j == 2) { next; }&lt;br /&gt;
   if($j == 4) { last; }&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
 j is 0&lt;br /&gt;
 j is 1&lt;br /&gt;
 j is 3&lt;br /&gt;
&lt;br /&gt;
Finally, there is the ''foreach'' loop.  ''foreach'' loops are most useful when iterating through arrays or hashs.  Here is the general form:&lt;br /&gt;
&lt;br /&gt;
 foreach $iterator (@array)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, to print out all the values in an array, you could do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @names = ('Bugs', 'Daffy', 'Marvin');&lt;br /&gt;
 foreach $name (@names)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$name\n&amp;quot;;&lt;br /&gt;
 } &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl functions are identified with the keyword ''sub'' followed by the function name. Arguments passed to functions are stored in a special array ''@_''.  You can either access all of the arguments by assigning them to local variables via ''@_'' or you can use the ''shift'' function.  Here is the general form for a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my $arg1 = shift;&lt;br /&gt;
   my $arg2 = shift;&lt;br /&gt;
   ...&lt;br /&gt;
   my $argn = shift;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is a simple function to add two numbers and a call to that function (note that functions can be defined above or below where they are called):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add&lt;br /&gt;
 {&lt;br /&gt;
   my ($x,$y) = @_;&lt;br /&gt;
   return ($x+$y);&lt;br /&gt;
 }  &lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;sum of 2+5=&amp;quot; . add(2,5) . &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are simply Perl libraries that provide new or enhanced functionality.  Although some modules are distributed as part of the core Perl distribution, most of them are written and maintained by other Perl users or developers who needed some specific functionality that was missing.  The [http://www.cpan.org CPAN] repository is the portal for finding and getting modules.  CPAN also contains manuals for the modules, most of which contain examples.  Generally speaking, the modules are all perl programs, although sometimes they require libraries from other languages.&lt;br /&gt;
&lt;br /&gt;
The typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl.  For current Ubuntu installs, the perl version is 5.10, so the modules are in ''/usr/lib/perl/5.10/''.  You can define other locations by setting the ''PERL5LIB'' environmental variable. &lt;br /&gt;
&lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
A module is contained in ''.pm'' files located in one of the module directories (either the default or through PERL5LIB).  They are accessed in a perl script by including them via the ''use'' command.  Most modules are grouped in to common packages. For example, there is a ''DBI'' package that contains many database interface modules.  One of those modules is the ''mysql'' module.  To include that module (assuming you have it installed on your system) in your script, you would add this line:&lt;br /&gt;
&lt;br /&gt;
 use DBI::mysql;&lt;br /&gt;
&lt;br /&gt;
If a module you want is not installed on your system, you need to install it either manually or through the automated ''cpan'' command.  The later approach takes a bit of set up, but it makes installing new modules much simpler.&lt;br /&gt;
&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
Search the CPAN website for the module you want, then download the module.  Untar it (or unzip it) and change to the directory containing the source code.  You will notice that there is a file named ''Makefile.PL''. It's a perl script that generates a makefile for your perl installation.  Run this:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile you need to build the module.  If you want to install the module to a non-default module location, set the PREFIX environment variable:&lt;br /&gt;
&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile, you can build the module, test it, and install it. Most Perl modules require testing to ensure they are functioning correctly on your system, so doing the tests is part of the build process.  If a critical component of the test fails, you may have to resolve any problems before moving on.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself.  In some cases, the dependencies can easily spiral out of control so that it becomes very difficult to install everything manually, which is why the CPAN approach is preferred. &lt;br /&gt;
&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that comes with the perl package that uses the CPAN perl module. It is a tool that installs new modules with minimum user interaction and that auto installs prerequisites.  To install new modules system-wide, you'll need to run ''cpan'' as root.  So, run:&lt;br /&gt;
 &lt;br /&gt;
 sudo cpan&lt;br /&gt;
&lt;br /&gt;
If it is the first time you are running ''cpan'', you will need to answer some configuration questions.  When prompted, you should select the automated configuration to get things going without any extra hassle.  You will then get to the ''cpan'' prompt, where you can run commands to search for and install new modules.&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The first thing should probably install is ''Bundle::CPAN'' which installs a bunch of packages that make cpan easier to use.  To do that, at the ''cpan&amp;gt;'' prompt, run:&lt;br /&gt;
&lt;br /&gt;
 '''cpan&amp;gt;''' install Bundle::CPAN&lt;br /&gt;
&lt;br /&gt;
Note that as it installs, it will probably ask if it's ok to install additional packages and such along the way.  It's generally ok to accept whatever it asks when you are prompted.  The ''help'' command shows you all the available commands and options. You can search for a module with the ''i'' command:&lt;br /&gt;
&lt;br /&gt;
 ''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
You can install a module with the ''install'' command:&lt;br /&gt;
&lt;br /&gt;
 ''cpan&amp;gt;''' install MODULE&lt;br /&gt;
&lt;br /&gt;
''install'' downloads the module, compiles and tests it, and finally installs it if everything went well. The downloaded modules are stored under your ''~/.cpan '' directory (unless you specified another one during configuration), in case you need to look at source code or anything related to the module.  Although ''cpan'' will download any perl modules that are needed by the module you actually want to install, it might fail if any of the modules rely on non-perl resources elsewhere on the system (e.g., graphics libraries).  If that happens, you can go to the build directory, ''~/.cpan/build/MODULE'', and try to fix the problem.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect a MySQL you need to have DBI module and DBI::mysql installed in your system. Following guide has a good description of DBI interface in general [http://www.perl.com/pub/a/1999/10/DBI.html]&lt;br /&gt;
&lt;br /&gt;
Then you can connect a mysql database with the command&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
The databse description contains the type of the database (so that DBI knows how to connect), the name of the database and the host name. It returns a handle that you will use for this connection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;; #note $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established you can send SQL queries to your database server. First you need to prepare your SQL with ''prepare'' command on the database handle.&lt;br /&gt;
&lt;br /&gt;
$dbh-&amp;gt;prepare(&amp;quot;TYPE YOUR SQL HERE&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually returns a statement handle ($sth in our example below) that you will use for the database operations.&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Alaaddin'','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about the your quotation marks. An easier way is to use perl qq function (which is  generalize quotation)&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Alaaddin'','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with execute command:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() ||die &amp;quot;Can't execute sql&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually helps you bind variables so that you don't have to repeat them. For example, if you want to have a generic way to insert new values, you can prepare your statement &lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
and during execution specify the arguments represented with ''?''&lt;br /&gt;
 $inserth-&amp;gt;execute('Alaaddin'','Arabian Nights','Adventurer')|| die &amp;quot;Can't execute SQL&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you are querying your database, the query results are also accessed through your statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results. This function returns an array, so you can access any attribute from this array. As ''fetchrow_array'' returns only one row, you can call it continuously until it fails to return a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$sql=&amp;quot;insert into employee values ('aladdin','Arabian Nights','Adventurer')&amp;quot;; # sql for inserting&lt;br /&gt;
$sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
$sth-&amp;gt;finish();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, have some attributes and finish with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements.&lt;br /&gt;
&lt;br /&gt;
For example, consider the following HTML page&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code can be represented in a tree format&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hiararchical relation. For example, &amp;lt;html&amp;gt; node has two direct children, &amp;lt;head&amp;gt; and &amp;lt;body&amp;gt; and these siblings have their own children. Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree. You can find more about HTML::Trees at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod]].&lt;br /&gt;
In order to use ''HTML::Tree''s, you have to use ''HTML::TreeBuilder'' module.&lt;br /&gt;
 use ''HTML::TreeBuilder'';&lt;br /&gt;
 $tree=TML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
will create a new tree for you. You can then use this tree parse an html file. There are two ways to parse HTML, either directly parse a file with ''parse_file'' or parse a string with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can dump the contents of an HTML::Tree by calling ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before doing that you need to empty the existing parse data stored in the tree with ''delete'' function, otherwise, you may run out of memory.&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
Lets say we are interested in capturing headlines for  science news at  [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded this web page to ''bbc.html'', you can build a tree on it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information. Function ''look_down'' on an HTML::Tree or HTML::Element will return any child satisfying some criteria&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, following code will return all  HTML::Elements that are actually  hyperlinks in the page  and put it to an array.&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
left hand side of the call could be a variable instead of array,&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
In this case, instead of returning all links, it returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of look_down function comes from ability to specify further criteria in the following format:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has its SOMEATTRIBUTE has ITVALUE and the function specified in look_down returns 1. Usually that function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contains URLs to cse330 website.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 $_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;)=~m/www.cse330.org/&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing ''href'' attribute and using a regular expression to match it to ''www.cse330.org''.&lt;br /&gt;
&lt;br /&gt;
Now, as we said we are interested in capturing the BBC news headlines, we need to look at bbc.html to see what are the characteristics of the HTML::Elements containing the headlines.&lt;br /&gt;
&lt;br /&gt;
For example, in the current website, the main headline has the following lines (headline followed by a description)&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
....&lt;br /&gt;
            &lt;br /&gt;
		&lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence if we look down on the hyperlinks that have  class types ''tsh'' and ''shl'', we will get the hyperlinks that contain the headlines.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines but not the summaries. But no worries, if you look at the BBC html code, the headlines are inside a &amp;lt;div&amp;gt; element, and the summaries are in the next &amp;lt;div&amp;gt;. So we can reach those summaries by first moving to parent of the hyperlink and reaching the next sibling of that parent.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
  print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you should be careful about these kind of operations, if, for example parent was undefined, the above code was going to be interrupted during run time. A better way is  first to check if $links[$i]-&amp;gt;parent was defined and then check if parent's right sibling was defined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, you can use LWP::UserAgent to actually make a request to the website, get the contents and pass those contents to the tree, instead of downloading the a website and calling the parser  on that file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || #that are tsh  class&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href')=~m/science/&lt;br /&gt;
             )             ||# or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/ #or their class name ends with hl&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
 if( !($links[$i]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;)) {print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;}&lt;br /&gt;
  else {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some other examples can be found at Perl documentation for Tree Scanning at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod]&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=101</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=101"/>
		<updated>2009-10-26T00:50:03Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language with syntax similar to bash and PHP.  It's a high level, general purpose language that is used primarily to automate repetitive tasks, and to parse and process text.  Indeed, text processing was the original intended use for the language, although it has grown to be an extremely flexible language for many different types of processing.&lt;br /&gt;
&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways.  The programs, or scripts, themselves are plain text files, usually ending in '.pl' to indicate that it is a Perl script.  Given a Perl script file, the most common ways to execute the script are shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pass the script as input to the 'perl' executable: &amp;lt;pre&amp;gt;perl -w SCRIPT&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Execute the script directly by adding the following line to the top of the script:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, change the script to be executable, and finally execute the script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x SCRIPT&lt;br /&gt;
./SCRIPT &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While flexibility is one of the major strengths of Perl, the flexibility in the language itself can often lead to unintended consequences.  To overcome this, it is good practice to explicitly enable warnings and strict behavior for you scripts.  The ''-w'' above when running perl gets you part of the way there, and although it's not necessary to supply the ''-w'' it is nearly always the right thing to do.  You should also add the line:&lt;br /&gt;
&lt;br /&gt;
 use strict;&lt;br /&gt;
&lt;br /&gt;
to all of your scripts, near the top, to enable strict error checking.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Standard variables start with a dollar sign, ''$'', at the beginning of their names.  Unlike shell scripts, the ''$'' is always used, regardless of whether the variable is being set or accessed.  As with other scripting languages, you don't specify the type of the variable; it will be used treated as a integer, floating point number, string, etc, depending on how it is used in your script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Comments start with the number/sharp/hash sign, ''#'', and extend until the end of the line.  All non-comment statements end with a semicolon, '';''.  You can print  variables with the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
Variables can be used inside strings, and the value of that variable will be included in the string. (Note that you can also use the ''printf'' function when you need specific formatting of the output).&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string:&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global by default, so you need to specify locality if you want local variables. This can be done with the ''local'' and ''my'' keywords. Generally, ''my'' is safer and faster, and is therefore used most often.  If a variable is defined by ''my'' in a block, it will be local to the block.  If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $i=0;&lt;br /&gt;
 while($i&amp;lt;5)&lt;br /&gt;
 {&lt;br /&gt;
   my $squared = $i*$i;&lt;br /&gt;
   $cubed = $i*$i*$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print $squared;&lt;br /&gt;
 print $cubed;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above program segment, ''$squared'' is defined with ''my'' inside the while loop, so it is only defined inside that loop.  ''$cubed'' is not given any special scope, so it is a global variable.  Hence, after the loop, ''$squared'' will be undefined, but ''$cubed'' with have the value 64 (the last value it was assigned in the loop).&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
Arrays are represented with two different symbols, depending on whether you are referring to elements of the array, or the entire array.  Array variable names start with ''@'' when declaring them or when you are referring to the array itself.  The normal ''$'' is used when you want to access individual elements of the array.  Declaring arrays is fairly easy.  You can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
 $student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
 $student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
 $student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, lists strings or variables enclosed in parentheses, ''(value1,value2,...,valuen )'', are interpreted as arrays. You can access the contents with an array index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
You can also print the whole array:&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
The above will concatenate all the elements together without a delimiter.  Alternatively, you can print the array inside of a quotes&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print each elements separated by a space.&lt;br /&gt;
&lt;br /&gt;
The current size of an array can be accessed with the ''scalar()'' function:&lt;br /&gt;
&lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can also use special syntax to access the index of the last element in an array like so:&lt;br /&gt;
&lt;br /&gt;
 $last_index = $#student;&lt;br /&gt;
&lt;br /&gt;
Note that you start the array variable with ''$#'' instead of ''@'' or just ''$''.  In the example above, ''scalar(@student)'' is 4, and ''$#student'' is 3.&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
&lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be numbers or a characters:&lt;br /&gt;
&lt;br /&gt;
 @newarray = (1 .. 20);&lt;br /&gt;
 @newchars = ('l' .. 't');&lt;br /&gt;
&lt;br /&gt;
There is also support for adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''push()'' and ''unshift()'' take an array and a variable as arguments:&lt;br /&gt;
&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
VARIABLE can actually be a single variable or another array:&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student, @newstudents;&lt;br /&gt;
 push @student, (&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''pop()'' and ''shift()'' take only the array as an argument:&lt;br /&gt;
&lt;br /&gt;
 $laststudent = pop(@student);&lt;br /&gt;
&lt;br /&gt;
You can use the ''slice'' operation to retrieve portions of an array.&lt;br /&gt;
&lt;br /&gt;
 @newarray = @student[1..3]; # elements 1-3&lt;br /&gt;
 @newarray = @student[1..3,5,7,9..15]; # elements 1-3,5,7,9-15&lt;br /&gt;
&lt;br /&gt;
The ''splice()'' function lets you replace arbitrary elements within an array.  Here is the general syntax:&lt;br /&gt;
&lt;br /&gt;
 splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY);&lt;br /&gt;
  &lt;br /&gt;
Here is an example that replaces element 3 with ''Jerry'' and elements 4 with ''Lena'':&lt;br /&gt;
&lt;br /&gt;
 splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
The ''split()'' operation creates an array from a string. It takes a delimiter and a string as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The above will produce an array with 5 elements (''test'',''array'',''1'',''2'',''3'').&lt;br /&gt;
&lt;br /&gt;
The ''join()'' function does the reverse, i.e., it creates a string from the elements of an array. It takes a delimiter and an array as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
The above will form one string with every element from ''@student'' separated by a comma.&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays, which are similar to associate arrays. Whereas arrays are represented with the ''@'', hash arrays are represented by the ''%''symbol.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Leela&amp;quot;, 25,&lt;br /&gt;
         &amp;quot;Fry&amp;quot;, 28,&lt;br /&gt;
         &amp;quot;Bender&amp;quot;, 4,&lt;br /&gt;
         &amp;quot;Lord Nibbler&amp;quot;, &amp;quot;Unknown&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Elements within a hash array accessed by prefacing the hash array name with ''$'' and then using curly braces for the index.  For example:&lt;br /&gt;
&lt;br /&gt;
 $ages{&amp;quot;Leela&amp;quot;};	# Returns 25&lt;br /&gt;
 $ages{&amp;quot;Fry&amp;quot;};		# Returns 28&lt;br /&gt;
 $ages{&amp;quot;Bender&amp;quot;};	# Returns 4&lt;br /&gt;
 $ages{&amp;quot;Lord Nibbler&amp;quot;};	# Returns &amp;quot;Unknown&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
&lt;br /&gt;
''if'' statements work as they do in most other languages.  The general format is&lt;br /&gt;
&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #some statements&lt;br /&gt;
 }&lt;br /&gt;
 elsif ( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #whatever&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
   #other stuff&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
The standard comparison operators work as usual, including ''=='', ''&amp;lt;'', ''&amp;gt;'', etc.  The only thing to be aware of is that you should use ''eq'' and ''neq'' to compare strings instead of ''=='' and ''!=''.  &lt;br /&gt;
&lt;br /&gt;
 if($string eq &amp;quot;some string&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;strings are equal\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 elsif($int == 5)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;integer is five\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
&lt;br /&gt;
There are three main types of loops.  ''for'' and ''while'' loops work similarly to other languages.  Here are the general forms:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while(condition)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here are simple examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $sum=0;&lt;br /&gt;
 $i=1;&lt;br /&gt;
 while($i&amp;lt;=10)&lt;br /&gt;
 {&lt;br /&gt;
   $sum=$sum+$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print &amp;quot;sum is $sum\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''next'' and ''last'' are special commands that will jump to the next loop iteration, or to the end of the loop, respectively.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   if($j == 2) { next; }&lt;br /&gt;
   if($j == 4) { last; }&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
 j is 0&lt;br /&gt;
 j is 1&lt;br /&gt;
 j is 3&lt;br /&gt;
&lt;br /&gt;
Finally, there is the ''foreach'' loop.  ''foreach'' loops are most useful when iterating through arrays or hashs.  Here is the general form:&lt;br /&gt;
&lt;br /&gt;
 foreach $iterator (@array)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, to print out all the values in an array, you could do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @names = ('Bugs', 'Daffy', 'Marvin');&lt;br /&gt;
 foreach $name (@names)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$name\n&amp;quot;;&lt;br /&gt;
 } &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl functions are identified with the keyword ''sub'' followed by the function name. Arguments passed to functions are stored in a special array ''@_''.  You can either access all of the arguments by assigning them to local variables via ''@_'' or you can use the ''shift'' function.  Here is the general form for a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub functionname&lt;br /&gt;
 {&lt;br /&gt;
   my $arg1 = shift;&lt;br /&gt;
   my $arg2 = shift;&lt;br /&gt;
   ...&lt;br /&gt;
   my $argn = shift;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is a simple function to add two numbers and a call to that function (note that functions can be defined above or below where they are called):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add&lt;br /&gt;
 {&lt;br /&gt;
   my ($x,$y) = @_;&lt;br /&gt;
   return ($x+$y);&lt;br /&gt;
 }  &lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;sum of 2+5=&amp;quot; . add(2,5) . &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are libraries that other people developed for several useful task. [http://www.cpan.org] is the portal that contains most of the modules. It also has manuals for each module (most of which contain examples). While most of the modules are perl programs themselves, some may require compilation of libraries written using other languages. &lt;br /&gt;
&lt;br /&gt;
The  typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl. You can define other locations by setting ''PERL5LIB'' environmental variable. &lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
 A module is described in ''.pm'' file located in one of the module directories (either default or through PERL5LIB)  and accessible in a perl script through ''use'' command&lt;br /&gt;
 use PERL-MODULE&lt;br /&gt;
&lt;br /&gt;
If your modules is not installed in the system, you need to install it either by manually or through automated ''cpan'' command.&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
After you downloaded your module from CPAN, untar it (or unzip) and change to the directory containing the source code. You will notice that there is a Makefile.PL. That is a perl script that generates a makefile for your perl installation. Run this&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile. If you want to install it to a non-default module location, set PREFIX variable&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile ready, you can make  module, test it and install it. Most Perl modules requires a test to verify that it is working. If a critical component of the test fails, you may have to force it to install.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself. Some cases, such dependencies could branch easily so it may be hard to install everything  manually. Perl provides a tool that makes module installation easier as we will see next.&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that uses perl module Cpan. It is a tool that installs a module with minimum user interaction and auto-installs prerequisites. &lt;br /&gt;
 cpan&lt;br /&gt;
will start it. If it is the first time you are running ''cpan'', it will ask for configuration options. You can select the automated configuration and it will fill out the options for you. Alternatively, you can go with manual selection and specify each option by hand. The last option asks for one or more CPAN mirror locations. Make sure you have selected ''North America-&amp;gt;United States'' and select a nearby mirror. I usually go with ''perl.com''  as my main mirror and add a few in case ''perl.com'' is down. Don't worry, if you are not happy with your configuration, you can modify it  later.&lt;br /&gt;
&lt;br /&gt;
Once you are in ''cpan'' prompt, you can  interact with it&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
help command gives you the available options. You can search a module with ''i'' command&lt;br /&gt;
 ''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
and you can install a module with ''install'' command&lt;br /&gt;
 ''cpan&amp;gt;''' install /MODULENAME/&lt;br /&gt;
&lt;br /&gt;
''install'' command will download the module, compile and test it and finally install it if everything goes well. The downloaded programs are stored under ''~/.cpan '' directory (unless you specified another one during configuration). Although ''cpan'' will download the other prerequistid perl modules, if you have missing library in your system (such as graphics) that your module requires, cpan installation may fail. If  it happens, you can go to the build directory ''~/.cpan/build/YOUR-MODULE-NAME'' and try to fix the problem. ''look'' command will also take you to the compilation directory. You can leave ''cpan'' with ''q'' command.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect a MySQL you need to have DBI module and DBI::mysql installed in your system. Following guide has a good description of DBI interface in general [http://www.perl.com/pub/a/1999/10/DBI.html]&lt;br /&gt;
&lt;br /&gt;
Then you can connect a mysql database with the command&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
The databse description contains the type of the database (so that DBI knows how to connect), the name of the database and the host name. It returns a handle that you will use for this connection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;; #note $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established you can send SQL queries to your database server. First you need to prepare your SQL with ''prepare'' command on the database handle.&lt;br /&gt;
&lt;br /&gt;
$dbh-&amp;gt;prepare(&amp;quot;TYPE YOUR SQL HERE&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually returns a statement handle ($sth in our example below) that you will use for the database operations.&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Alaaddin'','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about the your quotation marks. An easier way is to use perl qq function (which is  generalize quotation)&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Alaaddin'','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with execute command:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() ||die &amp;quot;Can't execute sql&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually helps you bind variables so that you don't have to repeat them. For example, if you want to have a generic way to insert new values, you can prepare your statement &lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
and during execution specify the arguments represented with ''?''&lt;br /&gt;
 $inserth-&amp;gt;execute('Alaaddin'','Arabian Nights','Adventurer')|| die &amp;quot;Can't execute SQL&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you are querying your database, the query results are also accessed through your statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results. This function returns an array, so you can access any attribute from this array. As ''fetchrow_array'' returns only one row, you can call it continuously until it fails to return a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$sql=&amp;quot;insert into employee values ('aladdin','Arabian Nights','Adventurer')&amp;quot;; # sql for inserting&lt;br /&gt;
$sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
$sth-&amp;gt;finish();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, have some attributes and finish with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements.&lt;br /&gt;
&lt;br /&gt;
For example, consider the following HTML page&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code can be represented in a tree format&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hiararchical relation. For example, &amp;lt;html&amp;gt; node has two direct children, &amp;lt;head&amp;gt; and &amp;lt;body&amp;gt; and these siblings have their own children. Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree. You can find more about HTML::Trees at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod]].&lt;br /&gt;
In order to use ''HTML::Tree''s, you have to use ''HTML::TreeBuilder'' module.&lt;br /&gt;
 use ''HTML::TreeBuilder'';&lt;br /&gt;
 $tree=TML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
will create a new tree for you. You can then use this tree parse an html file. There are two ways to parse HTML, either directly parse a file with ''parse_file'' or parse a string with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can dump the contents of an HTML::Tree by calling ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before doing that you need to empty the existing parse data stored in the tree with ''delete'' function, otherwise, you may run out of memory.&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
Lets say we are interested in capturing headlines for  science news at  [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded this web page to ''bbc.html'', you can build a tree on it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information. Function ''look_down'' on an HTML::Tree or HTML::Element will return any child satisfying some criteria&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, following code will return all  HTML::Elements that are actually  hyperlinks in the page  and put it to an array.&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
left hand side of the call could be a variable instead of array,&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
In this case, instead of returning all links, it returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of look_down function comes from ability to specify further criteria in the following format:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has its SOMEATTRIBUTE has ITVALUE and the function specified in look_down returns 1. Usually that function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contains URLs to cse330 website.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 $_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;)=~m/www.cse330.org/&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing ''href'' attribute and using a regular expression to match it to ''www.cse330.org''.&lt;br /&gt;
&lt;br /&gt;
Now, as we said we are interested in capturing the BBC news headlines, we need to look at bbc.html to see what are the characteristics of the HTML::Elements containing the headlines.&lt;br /&gt;
&lt;br /&gt;
For example, in the current website, the main headline has the following lines (headline followed by a description)&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
....&lt;br /&gt;
            &lt;br /&gt;
		&lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence if we look down on the hyperlinks that have  class types ''tsh'' and ''shl'', we will get the hyperlinks that contain the headlines.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines but not the summaries. But no worries, if you look at the BBC html code, the headlines are inside a &amp;lt;div&amp;gt; element, and the summaries are in the next &amp;lt;div&amp;gt;. So we can reach those summaries by first moving to parent of the hyperlink and reaching the next sibling of that parent.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
  print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you should be careful about these kind of operations, if, for example parent was undefined, the above code was going to be interrupted during run time. A better way is  first to check if $links[$i]-&amp;gt;parent was defined and then check if parent's right sibling was defined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, you can use LWP::UserAgent to actually make a request to the website, get the contents and pass those contents to the tree, instead of downloading the a website and calling the parser  on that file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || #that are tsh  class&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href')=~m/science/&lt;br /&gt;
             )             ||# or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/ #or their class name ends with hl&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
 if( !($links[$i]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;)) {print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;}&lt;br /&gt;
  else {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some other examples can be found at Perl documentation for Tree Scanning at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod]&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=100</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=100"/>
		<updated>2009-10-26T00:39:00Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Loops */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language with syntax similar to bash and PHP.  It's a high level, general purpose language that is used primarily to automate repetitive tasks, and to parse and process text.  Indeed, text processing was the original intended use for the language, although it has grown to be an extremely flexible language for many different types of processing.&lt;br /&gt;
&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways.  The programs, or scripts, themselves are plain text files, usually ending in '.pl' to indicate that it is a Perl script.  Given a Perl script file, the most common ways to execute the script are shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pass the script as input to the 'perl' executable: &amp;lt;pre&amp;gt;perl -w SCRIPT&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Execute the script directly by adding the following line to the top of the script:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, change the script to be executable, and finally execute the script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x SCRIPT&lt;br /&gt;
./SCRIPT &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While flexibility is one of the major strengths of Perl, the flexibility in the language itself can often lead to unintended consequences.  To overcome this, it is good practice to explicitly enable warnings and strict behavior for you scripts.  The ''-w'' above when running perl gets you part of the way there, and although it's not necessary to supply the ''-w'' it is nearly always the right thing to do.  You should also add the line:&lt;br /&gt;
&lt;br /&gt;
 use strict;&lt;br /&gt;
&lt;br /&gt;
to all of your scripts, near the top, to enable strict error checking.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Standard variables start with a dollar sign, ''$'', at the beginning of their names.  Unlike shell scripts, the ''$'' is always used, regardless of whether the variable is being set or accessed.  As with other scripting languages, you don't specify the type of the variable; it will be used treated as a integer, floating point number, string, etc, depending on how it is used in your script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Comments start with the number/sharp/hash sign, ''#'', and extend until the end of the line.  All non-comment statements end with a semicolon, '';''.  You can print  variables with the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
Variables can be used inside strings, and the value of that variable will be included in the string. (Note that you can also use the ''printf'' function when you need specific formatting of the output).&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string:&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global by default, so you need to specify locality if you want local variables. This can be done with the ''local'' and ''my'' keywords. Generally, ''my'' is safer and faster, and is therefore used most often.  If a variable is defined by ''my'' in a block, it will be local to the block.  If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $i=0;&lt;br /&gt;
 while($i&amp;lt;5)&lt;br /&gt;
 {&lt;br /&gt;
   my $squared = $i*$i;&lt;br /&gt;
   $cubed = $i*$i*$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print $squared;&lt;br /&gt;
 print $cubed;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above program segment, ''$squared'' is defined with ''my'' inside the while loop, so it is only defined inside that loop.  ''$cubed'' is not given any special scope, so it is a global variable.  Hence, after the loop, ''$squared'' will be undefined, but ''$cubed'' with have the value 64 (the last value it was assigned in the loop).&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
Arrays are represented with two different symbols, depending on whether you are referring to elements of the array, or the entire array.  Array variable names start with ''@'' when declaring them or when you are referring to the array itself.  The normal ''$'' is used when you want to access individual elements of the array.  Declaring arrays is fairly easy.  You can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
 $student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
 $student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
 $student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, lists strings or variables enclosed in parentheses, ''(value1,value2,...,valuen )'', are interpreted as arrays. You can access the contents with an array index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
You can also print the whole array:&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
The above will concatenate all the elements together without a delimiter.  Alternatively, you can print the array inside of a quotes&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print each elements separated by a space.&lt;br /&gt;
&lt;br /&gt;
The current size of an array can be accessed with the ''scalar()'' function:&lt;br /&gt;
&lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can also use special syntax to access the index of the last element in an array like so:&lt;br /&gt;
&lt;br /&gt;
 $last_index = $#student;&lt;br /&gt;
&lt;br /&gt;
Note that you start the array variable with ''$#'' instead of ''@'' or just ''$''.  In the example above, ''scalar(@student)'' is 4, and ''$#student'' is 3.&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
&lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be numbers or a characters:&lt;br /&gt;
&lt;br /&gt;
 @newarray = (1 .. 20);&lt;br /&gt;
 @newchars = ('l' .. 't');&lt;br /&gt;
&lt;br /&gt;
There is also support for adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''push()'' and ''unshift()'' take an array and a variable as arguments:&lt;br /&gt;
&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
VARIABLE can actually be a single variable or another array:&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student, @newstudents;&lt;br /&gt;
 push @student, (&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''pop()'' and ''shift()'' take only the array as an argument:&lt;br /&gt;
&lt;br /&gt;
 $laststudent = pop(@student);&lt;br /&gt;
&lt;br /&gt;
You can use the ''slice'' operation to retrieve portions of an array.&lt;br /&gt;
&lt;br /&gt;
 @newarray = @student[1..3]; # elements 1-3&lt;br /&gt;
 @newarray = @student[1..3,5,7,9..15]; # elements 1-3,5,7,9-15&lt;br /&gt;
&lt;br /&gt;
The ''splice()'' function lets you replace arbitrary elements within an array.  Here is the general syntax:&lt;br /&gt;
&lt;br /&gt;
 splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY);&lt;br /&gt;
  &lt;br /&gt;
Here is an example that replaces element 3 with ''Jerry'' and elements 4 with ''Lena'':&lt;br /&gt;
&lt;br /&gt;
 splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
The ''split()'' operation creates an array from a string. It takes a delimiter and a string as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The above will produce an array with 5 elements (''test'',''array'',''1'',''2'',''3'').&lt;br /&gt;
&lt;br /&gt;
The ''join()'' function does the reverse, i.e., it creates a string from the elements of an array. It takes a delimiter and an array as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
The above will form one string with every element from ''@student'' separated by a comma.&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays, which are similar to associate arrays. Whereas arrays are represented with the ''@'', hash arrays are represented by the ''%''symbol.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Leela&amp;quot;, 25,&lt;br /&gt;
         &amp;quot;Fry&amp;quot;, 28,&lt;br /&gt;
         &amp;quot;Bender&amp;quot;, 4,&lt;br /&gt;
         &amp;quot;Lord Nibbler&amp;quot;, &amp;quot;Unknown&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Elements within a hash array accessed by prefacing the hash array name with ''$'' and then using curly braces for the index.  For example:&lt;br /&gt;
&lt;br /&gt;
 $ages{&amp;quot;Leela&amp;quot;};	# Returns 25&lt;br /&gt;
 $ages{&amp;quot;Fry&amp;quot;};		# Returns 28&lt;br /&gt;
 $ages{&amp;quot;Bender&amp;quot;};	# Returns 4&lt;br /&gt;
 $ages{&amp;quot;Lord Nibbler&amp;quot;};	# Returns &amp;quot;Unknown&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
&lt;br /&gt;
''if'' statements work as they do in most other languages.  The general format is&lt;br /&gt;
&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #some statements&lt;br /&gt;
 }&lt;br /&gt;
 elsif ( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #whatever&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
   #other stuff&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
The standard comparison operators work as usual, including ''=='', ''&amp;lt;'', ''&amp;gt;'', etc.  The only thing to be aware of is that you should use ''eq'' and ''neq'' to compare strings instead of ''=='' and ''!=''.  &lt;br /&gt;
&lt;br /&gt;
 if($string eq &amp;quot;some string&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;strings are equal\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 elsif($int == 5)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;integer is five\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
&lt;br /&gt;
There are three main types of loops.  ''for'' and ''while'' loops work similarly to other languages.  Here are the general forms:&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while(condition)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here are simple examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $sum=0;&lt;br /&gt;
 $i=1;&lt;br /&gt;
 while($i&amp;lt;=10)&lt;br /&gt;
 {&lt;br /&gt;
   $sum=$sum+$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print &amp;quot;sum is $sum\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''next'' and ''last'' are special commands that will jump to the next loop iteration, or to the end of the loop, respectively.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 for($j=0; $j&amp;lt;5; ++$j)&lt;br /&gt;
 {&lt;br /&gt;
   if($j == 2) { next; }&lt;br /&gt;
   if($j == 4) { last; }&lt;br /&gt;
   print &amp;quot;j is $j\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code will output:&lt;br /&gt;
&lt;br /&gt;
 j is 0&lt;br /&gt;
 j is 1&lt;br /&gt;
 j is 3&lt;br /&gt;
&lt;br /&gt;
Finally, there is the ''foreach'' loop.  ''foreach'' loops are most useful when iterating through arrays or hashs.  Here is the general form:&lt;br /&gt;
&lt;br /&gt;
 foreach $iterator (@array)&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, to print out all the values in an array, you could do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @names = ('Bugs', 'Daffy', 'Marvin');&lt;br /&gt;
 foreach $name (@names)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;$name\n&amp;quot;;&lt;br /&gt;
 } &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl  functions are identified with the keyword ''sub'' without any arguments. The arguments are stored in a special array ''@_''followed by a function name and argument list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
sub functionname {&lt;br /&gt;
&lt;br /&gt;
  my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add {&lt;br /&gt;
     my ($x,$y)=@_&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
 print  &amp;quot;Sum of 2+5=&amp;quot;,add(2,5);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are libraries that other people developed for several useful task. [http://www.cpan.org] is the portal that contains most of the modules. It also has manuals for each module (most of which contain examples). While most of the modules are perl programs themselves, some may require compilation of libraries written using other languages. &lt;br /&gt;
&lt;br /&gt;
The  typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl. You can define other locations by setting ''PERL5LIB'' environmental variable. &lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
 A module is described in ''.pm'' file located in one of the module directories (either default or through PERL5LIB)  and accessible in a perl script through ''use'' command&lt;br /&gt;
 use PERL-MODULE&lt;br /&gt;
&lt;br /&gt;
If your modules is not installed in the system, you need to install it either by manually or through automated ''cpan'' command.&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
After you downloaded your module from CPAN, untar it (or unzip) and change to the directory containing the source code. You will notice that there is a Makefile.PL. That is a perl script that generates a makefile for your perl installation. Run this&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile. If you want to install it to a non-default module location, set PREFIX variable&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile ready, you can make  module, test it and install it. Most Perl modules requires a test to verify that it is working. If a critical component of the test fails, you may have to force it to install.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself. Some cases, such dependencies could branch easily so it may be hard to install everything  manually. Perl provides a tool that makes module installation easier as we will see next.&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that uses perl module Cpan. It is a tool that installs a module with minimum user interaction and auto-installs prerequisites. &lt;br /&gt;
 cpan&lt;br /&gt;
will start it. If it is the first time you are running ''cpan'', it will ask for configuration options. You can select the automated configuration and it will fill out the options for you. Alternatively, you can go with manual selection and specify each option by hand. The last option asks for one or more CPAN mirror locations. Make sure you have selected ''North America-&amp;gt;United States'' and select a nearby mirror. I usually go with ''perl.com''  as my main mirror and add a few in case ''perl.com'' is down. Don't worry, if you are not happy with your configuration, you can modify it  later.&lt;br /&gt;
&lt;br /&gt;
Once you are in ''cpan'' prompt, you can  interact with it&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
help command gives you the available options. You can search a module with ''i'' command&lt;br /&gt;
 ''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
and you can install a module with ''install'' command&lt;br /&gt;
 ''cpan&amp;gt;''' install /MODULENAME/&lt;br /&gt;
&lt;br /&gt;
''install'' command will download the module, compile and test it and finally install it if everything goes well. The downloaded programs are stored under ''~/.cpan '' directory (unless you specified another one during configuration). Although ''cpan'' will download the other prerequistid perl modules, if you have missing library in your system (such as graphics) that your module requires, cpan installation may fail. If  it happens, you can go to the build directory ''~/.cpan/build/YOUR-MODULE-NAME'' and try to fix the problem. ''look'' command will also take you to the compilation directory. You can leave ''cpan'' with ''q'' command.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect a MySQL you need to have DBI module and DBI::mysql installed in your system. Following guide has a good description of DBI interface in general [http://www.perl.com/pub/a/1999/10/DBI.html]&lt;br /&gt;
&lt;br /&gt;
Then you can connect a mysql database with the command&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
The databse description contains the type of the database (so that DBI knows how to connect), the name of the database and the host name. It returns a handle that you will use for this connection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;; #note $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established you can send SQL queries to your database server. First you need to prepare your SQL with ''prepare'' command on the database handle.&lt;br /&gt;
&lt;br /&gt;
$dbh-&amp;gt;prepare(&amp;quot;TYPE YOUR SQL HERE&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually returns a statement handle ($sth in our example below) that you will use for the database operations.&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Alaaddin'','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about the your quotation marks. An easier way is to use perl qq function (which is  generalize quotation)&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Alaaddin'','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with execute command:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() ||die &amp;quot;Can't execute sql&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually helps you bind variables so that you don't have to repeat them. For example, if you want to have a generic way to insert new values, you can prepare your statement &lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
and during execution specify the arguments represented with ''?''&lt;br /&gt;
 $inserth-&amp;gt;execute('Alaaddin'','Arabian Nights','Adventurer')|| die &amp;quot;Can't execute SQL&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you are querying your database, the query results are also accessed through your statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results. This function returns an array, so you can access any attribute from this array. As ''fetchrow_array'' returns only one row, you can call it continuously until it fails to return a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$sql=&amp;quot;insert into employee values ('aladdin','Arabian Nights','Adventurer')&amp;quot;; # sql for inserting&lt;br /&gt;
$sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
$sth-&amp;gt;finish();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, have some attributes and finish with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements.&lt;br /&gt;
&lt;br /&gt;
For example, consider the following HTML page&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code can be represented in a tree format&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hiararchical relation. For example, &amp;lt;html&amp;gt; node has two direct children, &amp;lt;head&amp;gt; and &amp;lt;body&amp;gt; and these siblings have their own children. Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree. You can find more about HTML::Trees at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod]].&lt;br /&gt;
In order to use ''HTML::Tree''s, you have to use ''HTML::TreeBuilder'' module.&lt;br /&gt;
 use ''HTML::TreeBuilder'';&lt;br /&gt;
 $tree=TML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
will create a new tree for you. You can then use this tree parse an html file. There are two ways to parse HTML, either directly parse a file with ''parse_file'' or parse a string with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can dump the contents of an HTML::Tree by calling ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before doing that you need to empty the existing parse data stored in the tree with ''delete'' function, otherwise, you may run out of memory.&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
Lets say we are interested in capturing headlines for  science news at  [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded this web page to ''bbc.html'', you can build a tree on it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information. Function ''look_down'' on an HTML::Tree or HTML::Element will return any child satisfying some criteria&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, following code will return all  HTML::Elements that are actually  hyperlinks in the page  and put it to an array.&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
left hand side of the call could be a variable instead of array,&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
In this case, instead of returning all links, it returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of look_down function comes from ability to specify further criteria in the following format:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has its SOMEATTRIBUTE has ITVALUE and the function specified in look_down returns 1. Usually that function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contains URLs to cse330 website.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 $_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;)=~m/www.cse330.org/&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing ''href'' attribute and using a regular expression to match it to ''www.cse330.org''.&lt;br /&gt;
&lt;br /&gt;
Now, as we said we are interested in capturing the BBC news headlines, we need to look at bbc.html to see what are the characteristics of the HTML::Elements containing the headlines.&lt;br /&gt;
&lt;br /&gt;
For example, in the current website, the main headline has the following lines (headline followed by a description)&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
....&lt;br /&gt;
            &lt;br /&gt;
		&lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence if we look down on the hyperlinks that have  class types ''tsh'' and ''shl'', we will get the hyperlinks that contain the headlines.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines but not the summaries. But no worries, if you look at the BBC html code, the headlines are inside a &amp;lt;div&amp;gt; element, and the summaries are in the next &amp;lt;div&amp;gt;. So we can reach those summaries by first moving to parent of the hyperlink and reaching the next sibling of that parent.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
  print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you should be careful about these kind of operations, if, for example parent was undefined, the above code was going to be interrupted during run time. A better way is  first to check if $links[$i]-&amp;gt;parent was defined and then check if parent's right sibling was defined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, you can use LWP::UserAgent to actually make a request to the website, get the contents and pass those contents to the tree, instead of downloading the a website and calling the parser  on that file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || #that are tsh  class&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href')=~m/science/&lt;br /&gt;
             )             ||# or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/ #or their class name ends with hl&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
 if( !($links[$i]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;)) {print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;}&lt;br /&gt;
  else {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some other examples can be found at Perl documentation for Tree Scanning at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod]&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=99</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=99"/>
		<updated>2009-10-26T00:21:14Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Conditional Statements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language with syntax similar to bash and PHP.  It's a high level, general purpose language that is used primarily to automate repetitive tasks, and to parse and process text.  Indeed, text processing was the original intended use for the language, although it has grown to be an extremely flexible language for many different types of processing.&lt;br /&gt;
&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways.  The programs, or scripts, themselves are plain text files, usually ending in '.pl' to indicate that it is a Perl script.  Given a Perl script file, the most common ways to execute the script are shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pass the script as input to the 'perl' executable: &amp;lt;pre&amp;gt;perl -w SCRIPT&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Execute the script directly by adding the following line to the top of the script:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, change the script to be executable, and finally execute the script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x SCRIPT&lt;br /&gt;
./SCRIPT &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While flexibility is one of the major strengths of Perl, the flexibility in the language itself can often lead to unintended consequences.  To overcome this, it is good practice to explicitly enable warnings and strict behavior for you scripts.  The ''-w'' above when running perl gets you part of the way there, and although it's not necessary to supply the ''-w'' it is nearly always the right thing to do.  You should also add the line:&lt;br /&gt;
&lt;br /&gt;
 use strict;&lt;br /&gt;
&lt;br /&gt;
to all of your scripts, near the top, to enable strict error checking.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Standard variables start with a dollar sign, ''$'', at the beginning of their names.  Unlike shell scripts, the ''$'' is always used, regardless of whether the variable is being set or accessed.  As with other scripting languages, you don't specify the type of the variable; it will be used treated as a integer, floating point number, string, etc, depending on how it is used in your script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Comments start with the number/sharp/hash sign, ''#'', and extend until the end of the line.  All non-comment statements end with a semicolon, '';''.  You can print  variables with the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
Variables can be used inside strings, and the value of that variable will be included in the string. (Note that you can also use the ''printf'' function when you need specific formatting of the output).&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string:&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global by default, so you need to specify locality if you want local variables. This can be done with the ''local'' and ''my'' keywords. Generally, ''my'' is safer and faster, and is therefore used most often.  If a variable is defined by ''my'' in a block, it will be local to the block.  If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $i=0;&lt;br /&gt;
 while($i&amp;lt;5)&lt;br /&gt;
 {&lt;br /&gt;
   my $squared = $i*$i;&lt;br /&gt;
   $cubed = $i*$i*$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print $squared;&lt;br /&gt;
 print $cubed;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above program segment, ''$squared'' is defined with ''my'' inside the while loop, so it is only defined inside that loop.  ''$cubed'' is not given any special scope, so it is a global variable.  Hence, after the loop, ''$squared'' will be undefined, but ''$cubed'' with have the value 64 (the last value it was assigned in the loop).&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
Arrays are represented with two different symbols, depending on whether you are referring to elements of the array, or the entire array.  Array variable names start with ''@'' when declaring them or when you are referring to the array itself.  The normal ''$'' is used when you want to access individual elements of the array.  Declaring arrays is fairly easy.  You can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
 $student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
 $student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
 $student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, lists strings or variables enclosed in parentheses, ''(value1,value2,...,valuen )'', are interpreted as arrays. You can access the contents with an array index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
You can also print the whole array:&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
The above will concatenate all the elements together without a delimiter.  Alternatively, you can print the array inside of a quotes&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print each elements separated by a space.&lt;br /&gt;
&lt;br /&gt;
The current size of an array can be accessed with the ''scalar()'' function:&lt;br /&gt;
&lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can also use special syntax to access the index of the last element in an array like so:&lt;br /&gt;
&lt;br /&gt;
 $last_index = $#student;&lt;br /&gt;
&lt;br /&gt;
Note that you start the array variable with ''$#'' instead of ''@'' or just ''$''.  In the example above, ''scalar(@student)'' is 4, and ''$#student'' is 3.&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
&lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be numbers or a characters:&lt;br /&gt;
&lt;br /&gt;
 @newarray = (1 .. 20);&lt;br /&gt;
 @newchars = ('l' .. 't');&lt;br /&gt;
&lt;br /&gt;
There is also support for adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''push()'' and ''unshift()'' take an array and a variable as arguments:&lt;br /&gt;
&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
VARIABLE can actually be a single variable or another array:&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student, @newstudents;&lt;br /&gt;
 push @student, (&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''pop()'' and ''shift()'' take only the array as an argument:&lt;br /&gt;
&lt;br /&gt;
 $laststudent = pop(@student);&lt;br /&gt;
&lt;br /&gt;
You can use the ''slice'' operation to retrieve portions of an array.&lt;br /&gt;
&lt;br /&gt;
 @newarray = @student[1..3]; # elements 1-3&lt;br /&gt;
 @newarray = @student[1..3,5,7,9..15]; # elements 1-3,5,7,9-15&lt;br /&gt;
&lt;br /&gt;
The ''splice()'' function lets you replace arbitrary elements within an array.  Here is the general syntax:&lt;br /&gt;
&lt;br /&gt;
 splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY);&lt;br /&gt;
  &lt;br /&gt;
Here is an example that replaces element 3 with ''Jerry'' and elements 4 with ''Lena'':&lt;br /&gt;
&lt;br /&gt;
 splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
The ''split()'' operation creates an array from a string. It takes a delimiter and a string as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The above will produce an array with 5 elements (''test'',''array'',''1'',''2'',''3'').&lt;br /&gt;
&lt;br /&gt;
The ''join()'' function does the reverse, i.e., it creates a string from the elements of an array. It takes a delimiter and an array as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
The above will form one string with every element from ''@student'' separated by a comma.&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays, which are similar to associate arrays. Whereas arrays are represented with the ''@'', hash arrays are represented by the ''%''symbol.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Leela&amp;quot;, 25,&lt;br /&gt;
         &amp;quot;Fry&amp;quot;, 28,&lt;br /&gt;
         &amp;quot;Bender&amp;quot;, 4,&lt;br /&gt;
         &amp;quot;Lord Nibbler&amp;quot;, &amp;quot;Unknown&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Elements within a hash array accessed by prefacing the hash array name with ''$'' and then using curly braces for the index.  For example:&lt;br /&gt;
&lt;br /&gt;
 $ages{&amp;quot;Leela&amp;quot;};	# Returns 25&lt;br /&gt;
 $ages{&amp;quot;Fry&amp;quot;};		# Returns 28&lt;br /&gt;
 $ages{&amp;quot;Bender&amp;quot;};	# Returns 4&lt;br /&gt;
 $ages{&amp;quot;Lord Nibbler&amp;quot;};	# Returns &amp;quot;Unknown&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
&lt;br /&gt;
''if'' statements work as they do in most other languages.  The general format is&lt;br /&gt;
&lt;br /&gt;
 if ( CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #some statements&lt;br /&gt;
 }&lt;br /&gt;
 elsif ( ANOTHER_CONDITION )&lt;br /&gt;
 {&lt;br /&gt;
   #whatever&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
   #other stuff&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
The standard comparison operators work as usual, including ''=='', ''&amp;lt;'', ''&amp;gt;'', etc.  The only thing to be aware of is that you should use ''eq'' and ''neq'' to compare strings instead of ''=='' and ''!=''.  &lt;br /&gt;
&lt;br /&gt;
 if($string eq &amp;quot;some string&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;strings are equal\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 elsif($int == 5)&lt;br /&gt;
 {&lt;br /&gt;
   print &amp;quot;integer is five\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
You can use ''for'' or ''while'' loops. The formats are very similar to C.&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 For example, following code will print the sum of number 1 from 10.&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 print &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt the loop execution, or jump to the end of loop.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl  functions are identified with the keyword ''sub'' without any arguments. The arguments are stored in a special array ''@_''followed by a function name and argument list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
sub functionname {&lt;br /&gt;
&lt;br /&gt;
  my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add {&lt;br /&gt;
     my ($x,$y)=@_&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
 print  &amp;quot;Sum of 2+5=&amp;quot;,add(2,5);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are libraries that other people developed for several useful task. [http://www.cpan.org] is the portal that contains most of the modules. It also has manuals for each module (most of which contain examples). While most of the modules are perl programs themselves, some may require compilation of libraries written using other languages. &lt;br /&gt;
&lt;br /&gt;
The  typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl. You can define other locations by setting ''PERL5LIB'' environmental variable. &lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
 A module is described in ''.pm'' file located in one of the module directories (either default or through PERL5LIB)  and accessible in a perl script through ''use'' command&lt;br /&gt;
 use PERL-MODULE&lt;br /&gt;
&lt;br /&gt;
If your modules is not installed in the system, you need to install it either by manually or through automated ''cpan'' command.&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
After you downloaded your module from CPAN, untar it (or unzip) and change to the directory containing the source code. You will notice that there is a Makefile.PL. That is a perl script that generates a makefile for your perl installation. Run this&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile. If you want to install it to a non-default module location, set PREFIX variable&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile ready, you can make  module, test it and install it. Most Perl modules requires a test to verify that it is working. If a critical component of the test fails, you may have to force it to install.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself. Some cases, such dependencies could branch easily so it may be hard to install everything  manually. Perl provides a tool that makes module installation easier as we will see next.&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that uses perl module Cpan. It is a tool that installs a module with minimum user interaction and auto-installs prerequisites. &lt;br /&gt;
 cpan&lt;br /&gt;
will start it. If it is the first time you are running ''cpan'', it will ask for configuration options. You can select the automated configuration and it will fill out the options for you. Alternatively, you can go with manual selection and specify each option by hand. The last option asks for one or more CPAN mirror locations. Make sure you have selected ''North America-&amp;gt;United States'' and select a nearby mirror. I usually go with ''perl.com''  as my main mirror and add a few in case ''perl.com'' is down. Don't worry, if you are not happy with your configuration, you can modify it  later.&lt;br /&gt;
&lt;br /&gt;
Once you are in ''cpan'' prompt, you can  interact with it&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
help command gives you the available options. You can search a module with ''i'' command&lt;br /&gt;
 ''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
and you can install a module with ''install'' command&lt;br /&gt;
 ''cpan&amp;gt;''' install /MODULENAME/&lt;br /&gt;
&lt;br /&gt;
''install'' command will download the module, compile and test it and finally install it if everything goes well. The downloaded programs are stored under ''~/.cpan '' directory (unless you specified another one during configuration). Although ''cpan'' will download the other prerequistid perl modules, if you have missing library in your system (such as graphics) that your module requires, cpan installation may fail. If  it happens, you can go to the build directory ''~/.cpan/build/YOUR-MODULE-NAME'' and try to fix the problem. ''look'' command will also take you to the compilation directory. You can leave ''cpan'' with ''q'' command.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect a MySQL you need to have DBI module and DBI::mysql installed in your system. Following guide has a good description of DBI interface in general [http://www.perl.com/pub/a/1999/10/DBI.html]&lt;br /&gt;
&lt;br /&gt;
Then you can connect a mysql database with the command&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
The databse description contains the type of the database (so that DBI knows how to connect), the name of the database and the host name. It returns a handle that you will use for this connection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;; #note $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established you can send SQL queries to your database server. First you need to prepare your SQL with ''prepare'' command on the database handle.&lt;br /&gt;
&lt;br /&gt;
$dbh-&amp;gt;prepare(&amp;quot;TYPE YOUR SQL HERE&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually returns a statement handle ($sth in our example below) that you will use for the database operations.&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Alaaddin'','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about the your quotation marks. An easier way is to use perl qq function (which is  generalize quotation)&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Alaaddin'','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with execute command:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() ||die &amp;quot;Can't execute sql&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually helps you bind variables so that you don't have to repeat them. For example, if you want to have a generic way to insert new values, you can prepare your statement &lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
and during execution specify the arguments represented with ''?''&lt;br /&gt;
 $inserth-&amp;gt;execute('Alaaddin'','Arabian Nights','Adventurer')|| die &amp;quot;Can't execute SQL&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you are querying your database, the query results are also accessed through your statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results. This function returns an array, so you can access any attribute from this array. As ''fetchrow_array'' returns only one row, you can call it continuously until it fails to return a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$sql=&amp;quot;insert into employee values ('aladdin','Arabian Nights','Adventurer')&amp;quot;; # sql for inserting&lt;br /&gt;
$sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
$sth-&amp;gt;finish();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, have some attributes and finish with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements.&lt;br /&gt;
&lt;br /&gt;
For example, consider the following HTML page&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code can be represented in a tree format&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hiararchical relation. For example, &amp;lt;html&amp;gt; node has two direct children, &amp;lt;head&amp;gt; and &amp;lt;body&amp;gt; and these siblings have their own children. Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree. You can find more about HTML::Trees at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod]].&lt;br /&gt;
In order to use ''HTML::Tree''s, you have to use ''HTML::TreeBuilder'' module.&lt;br /&gt;
 use ''HTML::TreeBuilder'';&lt;br /&gt;
 $tree=TML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
will create a new tree for you. You can then use this tree parse an html file. There are two ways to parse HTML, either directly parse a file with ''parse_file'' or parse a string with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can dump the contents of an HTML::Tree by calling ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before doing that you need to empty the existing parse data stored in the tree with ''delete'' function, otherwise, you may run out of memory.&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
Lets say we are interested in capturing headlines for  science news at  [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded this web page to ''bbc.html'', you can build a tree on it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information. Function ''look_down'' on an HTML::Tree or HTML::Element will return any child satisfying some criteria&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, following code will return all  HTML::Elements that are actually  hyperlinks in the page  and put it to an array.&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
left hand side of the call could be a variable instead of array,&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
In this case, instead of returning all links, it returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of look_down function comes from ability to specify further criteria in the following format:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has its SOMEATTRIBUTE has ITVALUE and the function specified in look_down returns 1. Usually that function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contains URLs to cse330 website.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 $_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;)=~m/www.cse330.org/&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing ''href'' attribute and using a regular expression to match it to ''www.cse330.org''.&lt;br /&gt;
&lt;br /&gt;
Now, as we said we are interested in capturing the BBC news headlines, we need to look at bbc.html to see what are the characteristics of the HTML::Elements containing the headlines.&lt;br /&gt;
&lt;br /&gt;
For example, in the current website, the main headline has the following lines (headline followed by a description)&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
....&lt;br /&gt;
            &lt;br /&gt;
		&lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence if we look down on the hyperlinks that have  class types ''tsh'' and ''shl'', we will get the hyperlinks that contain the headlines.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines but not the summaries. But no worries, if you look at the BBC html code, the headlines are inside a &amp;lt;div&amp;gt; element, and the summaries are in the next &amp;lt;div&amp;gt;. So we can reach those summaries by first moving to parent of the hyperlink and reaching the next sibling of that parent.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
  print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you should be careful about these kind of operations, if, for example parent was undefined, the above code was going to be interrupted during run time. A better way is  first to check if $links[$i]-&amp;gt;parent was defined and then check if parent's right sibling was defined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, you can use LWP::UserAgent to actually make a request to the website, get the contents and pass those contents to the tree, instead of downloading the a website and calling the parser  on that file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || #that are tsh  class&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href')=~m/science/&lt;br /&gt;
             )             ||# or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/ #or their class name ends with hl&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
 if( !($links[$i]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;)) {print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;}&lt;br /&gt;
  else {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some other examples can be found at Perl documentation for Tree Scanning at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod]&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=98</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=98"/>
		<updated>2009-10-25T20:13:38Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Arrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language with syntax similar to bash and PHP.  It's a high level, general purpose language that is used primarily to automate repetitive tasks, and to parse and process text.  Indeed, text processing was the original intended use for the language, although it has grown to be an extremely flexible language for many different types of processing.&lt;br /&gt;
&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways.  The programs, or scripts, themselves are plain text files, usually ending in '.pl' to indicate that it is a Perl script.  Given a Perl script file, the most common ways to execute the script are shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pass the script as input to the 'perl' executable: &amp;lt;pre&amp;gt;perl -w SCRIPT&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Execute the script directly by adding the following line to the top of the script:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, change the script to be executable, and finally execute the script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x SCRIPT&lt;br /&gt;
./SCRIPT &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While flexibility is one of the major strengths of Perl, the flexibility in the language itself can often lead to unintended consequences.  To overcome this, it is good practice to explicitly enable warnings and strict behavior for you scripts.  The ''-w'' above when running perl gets you part of the way there, and although it's not necessary to supply the ''-w'' it is nearly always the right thing to do.  You should also add the line:&lt;br /&gt;
&lt;br /&gt;
 use strict;&lt;br /&gt;
&lt;br /&gt;
to all of your scripts, near the top, to enable strict error checking.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Standard variables start with a dollar sign, ''$'', at the beginning of their names.  Unlike shell scripts, the ''$'' is always used, regardless of whether the variable is being set or accessed.  As with other scripting languages, you don't specify the type of the variable; it will be used treated as a integer, floating point number, string, etc, depending on how it is used in your script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Comments start with the number/sharp/hash sign, ''#'', and extend until the end of the line.  All non-comment statements end with a semicolon, '';''.  You can print  variables with the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
Variables can be used inside strings, and the value of that variable will be included in the string. (Note that you can also use the ''printf'' function when you need specific formatting of the output).&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string:&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global by default, so you need to specify locality if you want local variables. This can be done with the ''local'' and ''my'' keywords. Generally, ''my'' is safer and faster, and is therefore used most often.  If a variable is defined by ''my'' in a block, it will be local to the block.  If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $i=0;&lt;br /&gt;
 while($i&amp;lt;5)&lt;br /&gt;
 {&lt;br /&gt;
   my $squared = $i*$i;&lt;br /&gt;
   $cubed = $i*$i*$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print $squared;&lt;br /&gt;
 print $cubed;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above program segment, ''$squared'' is defined with ''my'' inside the while loop, so it is only defined inside that loop.  ''$cubed'' is not given any special scope, so it is a global variable.  Hence, after the loop, ''$squared'' will be undefined, but ''$cubed'' with have the value 64 (the last value it was assigned in the loop).&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
Arrays are represented with two different symbols, depending on whether you are referring to elements of the array, or the entire array.  Array variable names start with ''@'' when declaring them or when you are referring to the array itself.  The normal ''$'' is used when you want to access individual elements of the array.  Declaring arrays is fairly easy.  You can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
 $student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
 $student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
 $student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, lists strings or variables enclosed in parentheses, ''(value1,value2,...,valuen )'', are interpreted as arrays. You can access the contents with an array index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
You can also print the whole array:&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
The above will concatenate all the elements together without a delimiter.  Alternatively, you can print the array inside of a quotes&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print each elements separated by a space.&lt;br /&gt;
&lt;br /&gt;
The current size of an array can be accessed with the ''scalar()'' function:&lt;br /&gt;
&lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can also use special syntax to access the index of the last element in an array like so:&lt;br /&gt;
&lt;br /&gt;
 $last_index = $#student;&lt;br /&gt;
&lt;br /&gt;
Note that you start the array variable with ''$#'' instead of ''@'' or just ''$''.  In the example above, ''scalar(@student)'' is 4, and ''$#student'' is 3.&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
&lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be numbers or a characters:&lt;br /&gt;
&lt;br /&gt;
 @newarray = (1 .. 20);&lt;br /&gt;
 @newchars = ('l' .. 't');&lt;br /&gt;
&lt;br /&gt;
There is also support for adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''push()'' and ''unshift()'' take an array and a variable as arguments:&lt;br /&gt;
&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
VARIABLE can actually be a single variable or another array:&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student, @newstudents;&lt;br /&gt;
 push @student, (&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''pop()'' and ''shift()'' take only the array as an argument:&lt;br /&gt;
&lt;br /&gt;
 $laststudent = pop(@student);&lt;br /&gt;
&lt;br /&gt;
You can use the ''slice'' operation to retrieve portions of an array.&lt;br /&gt;
&lt;br /&gt;
 @newarray = @student[1..3]; # elements 1-3&lt;br /&gt;
 @newarray = @student[1..3,5,7,9..15]; # elements 1-3,5,7,9-15&lt;br /&gt;
&lt;br /&gt;
The ''splice()'' function lets you replace arbitrary elements within an array.  Here is the general syntax:&lt;br /&gt;
&lt;br /&gt;
 splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY);&lt;br /&gt;
  &lt;br /&gt;
Here is an example that replaces element 3 with ''Jerry'' and elements 4 with ''Lena'':&lt;br /&gt;
&lt;br /&gt;
 splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
The ''split()'' operation creates an array from a string. It takes a delimiter and a string as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The above will produce an array with 5 elements (''test'',''array'',''1'',''2'',''3'').&lt;br /&gt;
&lt;br /&gt;
The ''join()'' function does the reverse, i.e., it creates a string from the elements of an array. It takes a delimiter and an array as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
The above will form one string with every element from ''@student'' separated by a comma.&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays, which are similar to associate arrays. Whereas arrays are represented with the ''@'', hash arrays are represented by the ''%''symbol.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Leela&amp;quot;, 25,&lt;br /&gt;
         &amp;quot;Fry&amp;quot;, 28,&lt;br /&gt;
         &amp;quot;Bender&amp;quot;, 4,&lt;br /&gt;
         &amp;quot;Lord Nibbler&amp;quot;, &amp;quot;Unknown&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Elements within a hash array accessed by prefacing the hash array name with ''$'' and then using curly braces for the index.  For example:&lt;br /&gt;
&lt;br /&gt;
 $ages{&amp;quot;Leela&amp;quot;};	# Returns 25&lt;br /&gt;
 $ages{&amp;quot;Fry&amp;quot;};		# Returns 28&lt;br /&gt;
 $ages{&amp;quot;Bender&amp;quot;};	# Returns 4&lt;br /&gt;
 $ages{&amp;quot;Lord Nibbler&amp;quot;};	# Returns &amp;quot;Unknown&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION ) {&lt;br /&gt;
     some statements&lt;br /&gt;
 } else {&lt;br /&gt;
 }&lt;br /&gt;
 You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string eq &amp;quot;this is my string&amp;quot;) { #note that for strings, it is ''eq'' &lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such a thing called global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
You can use ''for'' or ''while'' loops. The formats are very similar to C.&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 For example, following code will print the sum of number 1 from 10.&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 print &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt the loop execution, or jump to the end of loop.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl  functions are identified with the keyword ''sub'' without any arguments. The arguments are stored in a special array ''@_''followed by a function name and argument list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
sub functionname {&lt;br /&gt;
&lt;br /&gt;
  my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add {&lt;br /&gt;
     my ($x,$y)=@_&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
 print  &amp;quot;Sum of 2+5=&amp;quot;,add(2,5);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are libraries that other people developed for several useful task. [http://www.cpan.org] is the portal that contains most of the modules. It also has manuals for each module (most of which contain examples). While most of the modules are perl programs themselves, some may require compilation of libraries written using other languages. &lt;br /&gt;
&lt;br /&gt;
The  typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl. You can define other locations by setting ''PERL5LIB'' environmental variable. &lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
 A module is described in ''.pm'' file located in one of the module directories (either default or through PERL5LIB)  and accessible in a perl script through ''use'' command&lt;br /&gt;
 use PERL-MODULE&lt;br /&gt;
&lt;br /&gt;
If your modules is not installed in the system, you need to install it either by manually or through automated ''cpan'' command.&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
After you downloaded your module from CPAN, untar it (or unzip) and change to the directory containing the source code. You will notice that there is a Makefile.PL. That is a perl script that generates a makefile for your perl installation. Run this&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile. If you want to install it to a non-default module location, set PREFIX variable&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile ready, you can make  module, test it and install it. Most Perl modules requires a test to verify that it is working. If a critical component of the test fails, you may have to force it to install.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself. Some cases, such dependencies could branch easily so it may be hard to install everything  manually. Perl provides a tool that makes module installation easier as we will see next.&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that uses perl module Cpan. It is a tool that installs a module with minimum user interaction and auto-installs prerequisites. &lt;br /&gt;
 cpan&lt;br /&gt;
will start it. If it is the first time you are running ''cpan'', it will ask for configuration options. You can select the automated configuration and it will fill out the options for you. Alternatively, you can go with manual selection and specify each option by hand. The last option asks for one or more CPAN mirror locations. Make sure you have selected ''North America-&amp;gt;United States'' and select a nearby mirror. I usually go with ''perl.com''  as my main mirror and add a few in case ''perl.com'' is down. Don't worry, if you are not happy with your configuration, you can modify it  later.&lt;br /&gt;
&lt;br /&gt;
Once you are in ''cpan'' prompt, you can  interact with it&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
help command gives you the available options. You can search a module with ''i'' command&lt;br /&gt;
 ''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
and you can install a module with ''install'' command&lt;br /&gt;
 ''cpan&amp;gt;''' install /MODULENAME/&lt;br /&gt;
&lt;br /&gt;
''install'' command will download the module, compile and test it and finally install it if everything goes well. The downloaded programs are stored under ''~/.cpan '' directory (unless you specified another one during configuration). Although ''cpan'' will download the other prerequistid perl modules, if you have missing library in your system (such as graphics) that your module requires, cpan installation may fail. If  it happens, you can go to the build directory ''~/.cpan/build/YOUR-MODULE-NAME'' and try to fix the problem. ''look'' command will also take you to the compilation directory. You can leave ''cpan'' with ''q'' command.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect a MySQL you need to have DBI module and DBI::mysql installed in your system. Following guide has a good description of DBI interface in general [http://www.perl.com/pub/a/1999/10/DBI.html]&lt;br /&gt;
&lt;br /&gt;
Then you can connect a mysql database with the command&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
The databse description contains the type of the database (so that DBI knows how to connect), the name of the database and the host name. It returns a handle that you will use for this connection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;; #note $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established you can send SQL queries to your database server. First you need to prepare your SQL with ''prepare'' command on the database handle.&lt;br /&gt;
&lt;br /&gt;
$dbh-&amp;gt;prepare(&amp;quot;TYPE YOUR SQL HERE&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually returns a statement handle ($sth in our example below) that you will use for the database operations.&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Alaaddin'','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about the your quotation marks. An easier way is to use perl qq function (which is  generalize quotation)&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Alaaddin'','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with execute command:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() ||die &amp;quot;Can't execute sql&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually helps you bind variables so that you don't have to repeat them. For example, if you want to have a generic way to insert new values, you can prepare your statement &lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
and during execution specify the arguments represented with ''?''&lt;br /&gt;
 $inserth-&amp;gt;execute('Alaaddin'','Arabian Nights','Adventurer')|| die &amp;quot;Can't execute SQL&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you are querying your database, the query results are also accessed through your statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results. This function returns an array, so you can access any attribute from this array. As ''fetchrow_array'' returns only one row, you can call it continuously until it fails to return a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$sql=&amp;quot;insert into employee values ('aladdin','Arabian Nights','Adventurer')&amp;quot;; # sql for inserting&lt;br /&gt;
$sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
$sth-&amp;gt;finish();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, have some attributes and finish with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements.&lt;br /&gt;
&lt;br /&gt;
For example, consider the following HTML page&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code can be represented in a tree format&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hiararchical relation. For example, &amp;lt;html&amp;gt; node has two direct children, &amp;lt;head&amp;gt; and &amp;lt;body&amp;gt; and these siblings have their own children. Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree. You can find more about HTML::Trees at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod]].&lt;br /&gt;
In order to use ''HTML::Tree''s, you have to use ''HTML::TreeBuilder'' module.&lt;br /&gt;
 use ''HTML::TreeBuilder'';&lt;br /&gt;
 $tree=TML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
will create a new tree for you. You can then use this tree parse an html file. There are two ways to parse HTML, either directly parse a file with ''parse_file'' or parse a string with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can dump the contents of an HTML::Tree by calling ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before doing that you need to empty the existing parse data stored in the tree with ''delete'' function, otherwise, you may run out of memory.&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
Lets say we are interested in capturing headlines for  science news at  [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded this web page to ''bbc.html'', you can build a tree on it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information. Function ''look_down'' on an HTML::Tree or HTML::Element will return any child satisfying some criteria&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, following code will return all  HTML::Elements that are actually  hyperlinks in the page  and put it to an array.&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
left hand side of the call could be a variable instead of array,&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
In this case, instead of returning all links, it returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of look_down function comes from ability to specify further criteria in the following format:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has its SOMEATTRIBUTE has ITVALUE and the function specified in look_down returns 1. Usually that function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contains URLs to cse330 website.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 $_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;)=~m/www.cse330.org/&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing ''href'' attribute and using a regular expression to match it to ''www.cse330.org''.&lt;br /&gt;
&lt;br /&gt;
Now, as we said we are interested in capturing the BBC news headlines, we need to look at bbc.html to see what are the characteristics of the HTML::Elements containing the headlines.&lt;br /&gt;
&lt;br /&gt;
For example, in the current website, the main headline has the following lines (headline followed by a description)&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
....&lt;br /&gt;
            &lt;br /&gt;
		&lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence if we look down on the hyperlinks that have  class types ''tsh'' and ''shl'', we will get the hyperlinks that contain the headlines.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines but not the summaries. But no worries, if you look at the BBC html code, the headlines are inside a &amp;lt;div&amp;gt; element, and the summaries are in the next &amp;lt;div&amp;gt;. So we can reach those summaries by first moving to parent of the hyperlink and reaching the next sibling of that parent.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
  print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you should be careful about these kind of operations, if, for example parent was undefined, the above code was going to be interrupted during run time. A better way is  first to check if $links[$i]-&amp;gt;parent was defined and then check if parent's right sibling was defined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, you can use LWP::UserAgent to actually make a request to the website, get the contents and pass those contents to the tree, instead of downloading the a website and calling the parser  on that file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || #that are tsh  class&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href')=~m/science/&lt;br /&gt;
             )             ||# or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/ #or their class name ends with hl&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
 if( !($links[$i]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;)) {print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;}&lt;br /&gt;
  else {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some other examples can be found at Perl documentation for Tree Scanning at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod]&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=97</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=97"/>
		<updated>2009-10-25T20:12:53Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Variables and Arrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language with syntax similar to bash and PHP.  It's a high level, general purpose language that is used primarily to automate repetitive tasks, and to parse and process text.  Indeed, text processing was the original intended use for the language, although it has grown to be an extremely flexible language for many different types of processing.&lt;br /&gt;
&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways.  The programs, or scripts, themselves are plain text files, usually ending in '.pl' to indicate that it is a Perl script.  Given a Perl script file, the most common ways to execute the script are shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pass the script as input to the 'perl' executable: &amp;lt;pre&amp;gt;perl -w SCRIPT&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Execute the script directly by adding the following line to the top of the script:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, change the script to be executable, and finally execute the script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x SCRIPT&lt;br /&gt;
./SCRIPT &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While flexibility is one of the major strengths of Perl, the flexibility in the language itself can often lead to unintended consequences.  To overcome this, it is good practice to explicitly enable warnings and strict behavior for you scripts.  The ''-w'' above when running perl gets you part of the way there, and although it's not necessary to supply the ''-w'' it is nearly always the right thing to do.  You should also add the line:&lt;br /&gt;
&lt;br /&gt;
 use strict;&lt;br /&gt;
&lt;br /&gt;
to all of your scripts, near the top, to enable strict error checking.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Standard variables start with a dollar sign, ''$'', at the beginning of their names.  Unlike shell scripts, the ''$'' is always used, regardless of whether the variable is being set or accessed.  As with other scripting languages, you don't specify the type of the variable; it will be used treated as a integer, floating point number, string, etc, depending on how it is used in your script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Comments start with the number/sharp/hash sign, ''#'', and extend until the end of the line.  All non-comment statements end with a semicolon, '';''.  You can print  variables with the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
Variables can be used inside strings, and the value of that variable will be included in the string. (Note that you can also use the ''printf'' function when you need specific formatting of the output).&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string:&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global by default, so you need to specify locality if you want local variables. This can be done with the ''local'' and ''my'' keywords. Generally, ''my'' is safer and faster, and is therefore used most often.  If a variable is defined by ''my'' in a block, it will be local to the block.  If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $i=0;&lt;br /&gt;
 while($i&amp;lt;5)&lt;br /&gt;
 {&lt;br /&gt;
   my $squared = $i*$i;&lt;br /&gt;
   $cubed = $i*$i*$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print $squared;&lt;br /&gt;
 print $cubed;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above program segment, ''$squared'' is defined with ''my'' inside the while loop, so it is only defined inside that loop.  ''$cubed'' is not given any special scope, so it is a global variable.  Hence, after the loop, ''$squared'' will be undefined, but ''$cubed'' with have the value 64 (the last value it was assigned in the loop).&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
Arrays are represented with two different symbols, depending on whether you are referring to elements of the array, or the entire array.  Array variable names start with ''@'' when declaring them or when you are referring to the array itself.  The normal ''$'' is used when you want to access individual elements of the array.  Declaring arrays is fairly easy.  You can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
 $student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
 $student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
 $student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, lists strings or variables enclosed in parentheses, ''(value1,value2,...,valuen )'', are interpreted as arrays. You can access the contents with an array index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
You can also print the whole array:&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
The above will concatenate all the elements together without a delimiter.  Alternatively, you can print the array inside of a quotes&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print each elements separated by a space.&lt;br /&gt;
&lt;br /&gt;
The current size of an array can be accessed with the ''scalar()'' function:&lt;br /&gt;
&lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can also use special syntax to access the index of the last element in an array like so:&lt;br /&gt;
&lt;br /&gt;
 $last_index = $#student;&lt;br /&gt;
&lt;br /&gt;
Note that you start the array variable with ''$#'' instead of ''@'' or just ''$''.  In the example above, ''scalar(@student)'' is 4, and ''$#student'' is 3.&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
&lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be numbers or a characters:&lt;br /&gt;
&lt;br /&gt;
 @newarray = (1 .. 20);&lt;br /&gt;
 @newchars = ('l' .. 't');&lt;br /&gt;
&lt;br /&gt;
There is also support for adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''push()'' and ''unshift()'' take an array and a variable as arguments:&lt;br /&gt;
&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
VARIABLE can actually be a single variable or another array:&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student, @newstudents;&lt;br /&gt;
 push @student, (&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''pop()'' and ''shift()'' take only the array as an argument:&lt;br /&gt;
&lt;br /&gt;
 $laststudent = pop(@student);&lt;br /&gt;
&lt;br /&gt;
You can use the ''slice'' operation to retrieve portions of an array.&lt;br /&gt;
&lt;br /&gt;
 @newarray = @student[1..3]; # elements 1-3&lt;br /&gt;
 @newarray = @student[1..3,5,7,9..15]; # elements 1-3,5,7,9-15&lt;br /&gt;
&lt;br /&gt;
The ''splice()'' function lets you replace arbitrary elements within an array.  Here is the general syntax:&lt;br /&gt;
&lt;br /&gt;
 splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY);&lt;br /&gt;
  &lt;br /&gt;
Here is an example that replaces element 3 with ''Jerry'' and elements 4 with ''Lena'':&lt;br /&gt;
&lt;br /&gt;
 splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
The ''split()'' operation creates an array from a string. It takes a delimiter and a string as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
The above will produce an array with 5 elements (''test'',''array'',''1'',''2'',''3'').&lt;br /&gt;
&lt;br /&gt;
The ''join()'' function does the reverse, i.e., it creates a string from the elements of an array. It takes a delimiter and an array as arguments.  For example:&lt;br /&gt;
&lt;br /&gt;
 $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
The above will form one string with every element from ''@student'' separated by a comma.&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays, which are similar to associate arrays. Whereas arrays are represented with the ''@'', hash arrays are represented by the ''%''symbol.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Leela&amp;quot;, 25,&lt;br /&gt;
         &amp;quot;Fry&amp;quot;, 28,&lt;br /&gt;
         &amp;quot;Bender&amp;quot;, 4,&lt;br /&gt;
         &amp;quot;Lord Nibbler&amp;quot;, &amp;quot;Unknown&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Elements within a hash array accessed by prefacing the hash array name with ''$'' and then using curly braces for the index.  For example:&lt;br /&gt;
&lt;br /&gt;
$ages{&amp;quot;Leela&amp;quot;};		# Returns 25&lt;br /&gt;
$ages{&amp;quot;Fry&amp;quot;};		# Returns 28&lt;br /&gt;
$ages{&amp;quot;Bender&amp;quot;};	# Returns 4&lt;br /&gt;
$ages{&amp;quot;Lord Nibbler&amp;quot;};	# Returns &amp;quot;Unknown&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION ) {&lt;br /&gt;
     some statements&lt;br /&gt;
 } else {&lt;br /&gt;
 }&lt;br /&gt;
 You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string eq &amp;quot;this is my string&amp;quot;) { #note that for strings, it is ''eq'' &lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such a thing called global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
You can use ''for'' or ''while'' loops. The formats are very similar to C.&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 For example, following code will print the sum of number 1 from 10.&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 print &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt the loop execution, or jump to the end of loop.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl  functions are identified with the keyword ''sub'' without any arguments. The arguments are stored in a special array ''@_''followed by a function name and argument list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
sub functionname {&lt;br /&gt;
&lt;br /&gt;
  my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add {&lt;br /&gt;
     my ($x,$y)=@_&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
 print  &amp;quot;Sum of 2+5=&amp;quot;,add(2,5);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are libraries that other people developed for several useful task. [http://www.cpan.org] is the portal that contains most of the modules. It also has manuals for each module (most of which contain examples). While most of the modules are perl programs themselves, some may require compilation of libraries written using other languages. &lt;br /&gt;
&lt;br /&gt;
The  typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl. You can define other locations by setting ''PERL5LIB'' environmental variable. &lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
 A module is described in ''.pm'' file located in one of the module directories (either default or through PERL5LIB)  and accessible in a perl script through ''use'' command&lt;br /&gt;
 use PERL-MODULE&lt;br /&gt;
&lt;br /&gt;
If your modules is not installed in the system, you need to install it either by manually or through automated ''cpan'' command.&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
After you downloaded your module from CPAN, untar it (or unzip) and change to the directory containing the source code. You will notice that there is a Makefile.PL. That is a perl script that generates a makefile for your perl installation. Run this&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile. If you want to install it to a non-default module location, set PREFIX variable&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile ready, you can make  module, test it and install it. Most Perl modules requires a test to verify that it is working. If a critical component of the test fails, you may have to force it to install.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself. Some cases, such dependencies could branch easily so it may be hard to install everything  manually. Perl provides a tool that makes module installation easier as we will see next.&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that uses perl module Cpan. It is a tool that installs a module with minimum user interaction and auto-installs prerequisites. &lt;br /&gt;
 cpan&lt;br /&gt;
will start it. If it is the first time you are running ''cpan'', it will ask for configuration options. You can select the automated configuration and it will fill out the options for you. Alternatively, you can go with manual selection and specify each option by hand. The last option asks for one or more CPAN mirror locations. Make sure you have selected ''North America-&amp;gt;United States'' and select a nearby mirror. I usually go with ''perl.com''  as my main mirror and add a few in case ''perl.com'' is down. Don't worry, if you are not happy with your configuration, you can modify it  later.&lt;br /&gt;
&lt;br /&gt;
Once you are in ''cpan'' prompt, you can  interact with it&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
help command gives you the available options. You can search a module with ''i'' command&lt;br /&gt;
 ''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
and you can install a module with ''install'' command&lt;br /&gt;
 ''cpan&amp;gt;''' install /MODULENAME/&lt;br /&gt;
&lt;br /&gt;
''install'' command will download the module, compile and test it and finally install it if everything goes well. The downloaded programs are stored under ''~/.cpan '' directory (unless you specified another one during configuration). Although ''cpan'' will download the other prerequistid perl modules, if you have missing library in your system (such as graphics) that your module requires, cpan installation may fail. If  it happens, you can go to the build directory ''~/.cpan/build/YOUR-MODULE-NAME'' and try to fix the problem. ''look'' command will also take you to the compilation directory. You can leave ''cpan'' with ''q'' command.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect a MySQL you need to have DBI module and DBI::mysql installed in your system. Following guide has a good description of DBI interface in general [http://www.perl.com/pub/a/1999/10/DBI.html]&lt;br /&gt;
&lt;br /&gt;
Then you can connect a mysql database with the command&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
The databse description contains the type of the database (so that DBI knows how to connect), the name of the database and the host name. It returns a handle that you will use for this connection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;; #note $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established you can send SQL queries to your database server. First you need to prepare your SQL with ''prepare'' command on the database handle.&lt;br /&gt;
&lt;br /&gt;
$dbh-&amp;gt;prepare(&amp;quot;TYPE YOUR SQL HERE&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually returns a statement handle ($sth in our example below) that you will use for the database operations.&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Alaaddin'','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about the your quotation marks. An easier way is to use perl qq function (which is  generalize quotation)&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Alaaddin'','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with execute command:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() ||die &amp;quot;Can't execute sql&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually helps you bind variables so that you don't have to repeat them. For example, if you want to have a generic way to insert new values, you can prepare your statement &lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
and during execution specify the arguments represented with ''?''&lt;br /&gt;
 $inserth-&amp;gt;execute('Alaaddin'','Arabian Nights','Adventurer')|| die &amp;quot;Can't execute SQL&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you are querying your database, the query results are also accessed through your statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results. This function returns an array, so you can access any attribute from this array. As ''fetchrow_array'' returns only one row, you can call it continuously until it fails to return a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$sql=&amp;quot;insert into employee values ('aladdin','Arabian Nights','Adventurer')&amp;quot;; # sql for inserting&lt;br /&gt;
$sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
$sth-&amp;gt;finish();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, have some attributes and finish with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements.&lt;br /&gt;
&lt;br /&gt;
For example, consider the following HTML page&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code can be represented in a tree format&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hiararchical relation. For example, &amp;lt;html&amp;gt; node has two direct children, &amp;lt;head&amp;gt; and &amp;lt;body&amp;gt; and these siblings have their own children. Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree. You can find more about HTML::Trees at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod]].&lt;br /&gt;
In order to use ''HTML::Tree''s, you have to use ''HTML::TreeBuilder'' module.&lt;br /&gt;
 use ''HTML::TreeBuilder'';&lt;br /&gt;
 $tree=TML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
will create a new tree for you. You can then use this tree parse an html file. There are two ways to parse HTML, either directly parse a file with ''parse_file'' or parse a string with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can dump the contents of an HTML::Tree by calling ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before doing that you need to empty the existing parse data stored in the tree with ''delete'' function, otherwise, you may run out of memory.&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
Lets say we are interested in capturing headlines for  science news at  [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded this web page to ''bbc.html'', you can build a tree on it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information. Function ''look_down'' on an HTML::Tree or HTML::Element will return any child satisfying some criteria&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, following code will return all  HTML::Elements that are actually  hyperlinks in the page  and put it to an array.&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
left hand side of the call could be a variable instead of array,&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
In this case, instead of returning all links, it returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of look_down function comes from ability to specify further criteria in the following format:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has its SOMEATTRIBUTE has ITVALUE and the function specified in look_down returns 1. Usually that function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contains URLs to cse330 website.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 $_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;)=~m/www.cse330.org/&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing ''href'' attribute and using a regular expression to match it to ''www.cse330.org''.&lt;br /&gt;
&lt;br /&gt;
Now, as we said we are interested in capturing the BBC news headlines, we need to look at bbc.html to see what are the characteristics of the HTML::Elements containing the headlines.&lt;br /&gt;
&lt;br /&gt;
For example, in the current website, the main headline has the following lines (headline followed by a description)&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
....&lt;br /&gt;
            &lt;br /&gt;
		&lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence if we look down on the hyperlinks that have  class types ''tsh'' and ''shl'', we will get the hyperlinks that contain the headlines.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines but not the summaries. But no worries, if you look at the BBC html code, the headlines are inside a &amp;lt;div&amp;gt; element, and the summaries are in the next &amp;lt;div&amp;gt;. So we can reach those summaries by first moving to parent of the hyperlink and reaching the next sibling of that parent.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
  print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you should be careful about these kind of operations, if, for example parent was undefined, the above code was going to be interrupted during run time. A better way is  first to check if $links[$i]-&amp;gt;parent was defined and then check if parent's right sibling was defined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, you can use LWP::UserAgent to actually make a request to the website, get the contents and pass those contents to the tree, instead of downloading the a website and calling the parser  on that file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || #that are tsh  class&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href')=~m/science/&lt;br /&gt;
             )             ||# or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/ #or their class name ends with hl&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
 if( !($links[$i]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;)) {print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;}&lt;br /&gt;
  else {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some other examples can be found at Perl documentation for Tree Scanning at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod]&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=96</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=96"/>
		<updated>2009-10-25T19:47:16Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Perl */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language with syntax similar to bash and PHP.  It's a high level, general purpose language that is used primarily to automate repetitive tasks, and to parse and process text.  Indeed, text processing was the original intended use for the language, although it has grown to be an extremely flexible language for many different types of processing.&lt;br /&gt;
&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways.  The programs, or scripts, themselves are plain text files, usually ending in '.pl' to indicate that it is a Perl script.  Given a Perl script file, the most common ways to execute the script are shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Pass the script as input to the 'perl' executable: &amp;lt;pre&amp;gt;perl -w SCRIPT&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Execute the script directly by adding the following line to the top of the script:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -w&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Then, change the script to be executable, and finally execute the script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x SCRIPT&lt;br /&gt;
./SCRIPT &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While flexibility is one of the major strengths of Perl, the flexibility in the language itself can often lead to unintended consequences.  To overcome this, it is good practice to explicitly enable warnings and strict behavior for you scripts.  The ''-w'' above when running perl gets you part of the way there, and although it's not necessary to supply the ''-w'' it is nearly always the right thing to do.  You should also add the line:&lt;br /&gt;
&lt;br /&gt;
 use strict;&lt;br /&gt;
&lt;br /&gt;
to all of your scripts, near the top, to enable strict error checking.&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
Standard variables start with a dollar sign, ''$'', at the beginning of their names.  Unlike shell scripts, the ''$'' is always used, regardless of whether the variable is being set or accessed.  As with other scripting languages, you don't specify the type of the variable; it will be used treated as a integer, floating point number, string, etc, depending on how it is used in your script.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Comments start with the number/sharp/hash sign, ''#'', and extend until the end of the line.  All non-comment statements end with a semicolon, '';''.  You can print  variables with the ''print'' function:&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
Variables can be used inside strings, and the value of that variable will be included in the string. (Note that you can also use the ''printf'' function when you need specific formatting of the output).&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string:&lt;br /&gt;
&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global by default, so you need to specify locality if you want local variables. This can be done with the ''local'' and ''my'' keywords. Generally, ''my'' is safer and faster, and is therefore used most often.  If a variable is defined by ''my'' in a block, it will be local to the block.  If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $i=0;&lt;br /&gt;
 while($i&amp;lt;5)&lt;br /&gt;
 {&lt;br /&gt;
   my $squared = $i*$i;&lt;br /&gt;
   $cubed = $i*$i*$i;&lt;br /&gt;
   $i++;&lt;br /&gt;
 }&lt;br /&gt;
 print $squared;&lt;br /&gt;
 print $cubed;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above program segment, ''$squared'' is defined with ''my'' inside the while loop, so it is only defined inside that loop.  ''$cubed'' is not given any special scope, so it is a global variable.  Hence, after the loop, ''$squared'' will be undefined, but ''$cubed'' with have the value 64 (the last value it was assigned in the loop).&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
Arrays are represented with two different symbols, depending on whether you are referring to elements of the array, or the entire array.  Array variable names start with ''@'' when declaring them or when you are referring to the array itself.  The normal ''$'' is used when you want to access individual elements of the array.  Declaring arrays is fairly easy.  You can enter all elements one by one, or you can enter them together:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 $student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
 $student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
 $student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
 $student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 @student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general, lists strings or variables enclosed in parentheses, ''(value1,value2,...,valuen )'', are interpreted as arrays. You can access the contents with an array index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
 print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
You can also print the whole array:&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
The above will concatenate all the elements together without a delimiter.  Alternatively, you can print the array inside of a quotes&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print each elements separated by a space.&lt;br /&gt;
&lt;br /&gt;
The current size of an array can be accessed with the ''scalar()'' function:&lt;br /&gt;
&lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can also use special syntax to access the index of the last element in an array like so:&lt;br /&gt;
&lt;br /&gt;
 $last_index = $#student;&lt;br /&gt;
&lt;br /&gt;
Note that you start the array variable with ''$#'' instead of ''@'' or just ''$''.  In the example above, ''scalar(@student)'' is 4, and ''$#student'' is 3.&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
&lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be numbers or a characters:&lt;br /&gt;
&lt;br /&gt;
 @newarray = (1 .. 20);&lt;br /&gt;
 @newchars = ('l' .. 't');&lt;br /&gt;
&lt;br /&gt;
Perl arrays also support adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
push and unshift takes an array and a variable.&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
variable could be a single variable or another array.&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student,@newstudents;&lt;br /&gt;
 push @student,(&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
pop and shift take  array name as input&lt;br /&gt;
&lt;br /&gt;
 $onestudent=pop(@student);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can get only a portion of an array with ''slice'' operation. &lt;br /&gt;
&lt;br /&gt;
 @newarray=@student[1..3];&lt;br /&gt;
 @newarray=@student[1..3,10..15,.20];&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''splice'' function lets you replace arbitrary elements within the array&lt;br /&gt;
&lt;br /&gt;
  splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY)&lt;br /&gt;
  &lt;br /&gt;
  splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Split operating creates an array from a string, it takes a delimater and a string&lt;br /&gt;
  @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
and  ''join'' function does the reverse, i.e., create a string from the elements of an array. It takes a deliminator and an array&lt;br /&gt;
  $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays (similar to associate arrays). They are represented by ''%'' and the elements can be accessed by strings.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Michael Caine&amp;quot;, 39,&lt;br /&gt;
         &amp;quot;Dirty Den&amp;quot;, 34,&lt;br /&gt;
         &amp;quot;Angie&amp;quot;, 27,&lt;br /&gt;
         &amp;quot;Willy&amp;quot;, &amp;quot;21 in dog years&amp;quot;,&lt;br /&gt;
         &amp;quot;The Queen Mother&amp;quot;, 108);&lt;br /&gt;
&lt;br /&gt;
Now we can find the age of people with the following expressions&lt;br /&gt;
&lt;br /&gt;
$ages{&amp;quot;Michael Caine&amp;quot;};		# Returns 39&lt;br /&gt;
$ages{&amp;quot;Dirty Den&amp;quot;};		# Returns 34&lt;br /&gt;
$ages{&amp;quot;Angie&amp;quot;};			# Returns 27&lt;br /&gt;
$ages{&amp;quot;Willy&amp;quot;};			# Returns &amp;quot;21 in dog years&amp;quot;&lt;br /&gt;
$ages{&amp;quot;The Queen Mother&amp;quot;};	# Returns 108&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION ) {&lt;br /&gt;
     some statements&lt;br /&gt;
 } else {&lt;br /&gt;
 }&lt;br /&gt;
 You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string eq &amp;quot;this is my string&amp;quot;) { #note that for strings, it is ''eq'' &lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such a thing called global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
You can use ''for'' or ''while'' loops. The formats are very similar to C.&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 For example, following code will print the sum of number 1 from 10.&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 print &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt the loop execution, or jump to the end of loop.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl  functions are identified with the keyword ''sub'' without any arguments. The arguments are stored in a special array ''@_''followed by a function name and argument list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
sub functionname {&lt;br /&gt;
&lt;br /&gt;
  my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add {&lt;br /&gt;
     my ($x,$y)=@_&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
 print  &amp;quot;Sum of 2+5=&amp;quot;,add(2,5);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are libraries that other people developed for several useful task. [http://www.cpan.org] is the portal that contains most of the modules. It also has manuals for each module (most of which contain examples). While most of the modules are perl programs themselves, some may require compilation of libraries written using other languages. &lt;br /&gt;
&lt;br /&gt;
The  typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl. You can define other locations by setting ''PERL5LIB'' environmental variable. &lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
 A module is described in ''.pm'' file located in one of the module directories (either default or through PERL5LIB)  and accessible in a perl script through ''use'' command&lt;br /&gt;
 use PERL-MODULE&lt;br /&gt;
&lt;br /&gt;
If your modules is not installed in the system, you need to install it either by manually or through automated ''cpan'' command.&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
After you downloaded your module from CPAN, untar it (or unzip) and change to the directory containing the source code. You will notice that there is a Makefile.PL. That is a perl script that generates a makefile for your perl installation. Run this&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile. If you want to install it to a non-default module location, set PREFIX variable&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile ready, you can make  module, test it and install it. Most Perl modules requires a test to verify that it is working. If a critical component of the test fails, you may have to force it to install.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself. Some cases, such dependencies could branch easily so it may be hard to install everything  manually. Perl provides a tool that makes module installation easier as we will see next.&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that uses perl module Cpan. It is a tool that installs a module with minimum user interaction and auto-installs prerequisites. &lt;br /&gt;
 cpan&lt;br /&gt;
will start it. If it is the first time you are running ''cpan'', it will ask for configuration options. You can select the automated configuration and it will fill out the options for you. Alternatively, you can go with manual selection and specify each option by hand. The last option asks for one or more CPAN mirror locations. Make sure you have selected ''North America-&amp;gt;United States'' and select a nearby mirror. I usually go with ''perl.com''  as my main mirror and add a few in case ''perl.com'' is down. Don't worry, if you are not happy with your configuration, you can modify it  later.&lt;br /&gt;
&lt;br /&gt;
Once you are in ''cpan'' prompt, you can  interact with it&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
help command gives you the available options. You can search a module with ''i'' command&lt;br /&gt;
 ''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
and you can install a module with ''install'' command&lt;br /&gt;
 ''cpan&amp;gt;''' install /MODULENAME/&lt;br /&gt;
&lt;br /&gt;
''install'' command will download the module, compile and test it and finally install it if everything goes well. The downloaded programs are stored under ''~/.cpan '' directory (unless you specified another one during configuration). Although ''cpan'' will download the other prerequistid perl modules, if you have missing library in your system (such as graphics) that your module requires, cpan installation may fail. If  it happens, you can go to the build directory ''~/.cpan/build/YOUR-MODULE-NAME'' and try to fix the problem. ''look'' command will also take you to the compilation directory. You can leave ''cpan'' with ''q'' command.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect a MySQL you need to have DBI module and DBI::mysql installed in your system. Following guide has a good description of DBI interface in general [http://www.perl.com/pub/a/1999/10/DBI.html]&lt;br /&gt;
&lt;br /&gt;
Then you can connect a mysql database with the command&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
The databse description contains the type of the database (so that DBI knows how to connect), the name of the database and the host name. It returns a handle that you will use for this connection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;; #note $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established you can send SQL queries to your database server. First you need to prepare your SQL with ''prepare'' command on the database handle.&lt;br /&gt;
&lt;br /&gt;
$dbh-&amp;gt;prepare(&amp;quot;TYPE YOUR SQL HERE&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually returns a statement handle ($sth in our example below) that you will use for the database operations.&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Alaaddin'','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about the your quotation marks. An easier way is to use perl qq function (which is  generalize quotation)&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Alaaddin'','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with execute command:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() ||die &amp;quot;Can't execute sql&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually helps you bind variables so that you don't have to repeat them. For example, if you want to have a generic way to insert new values, you can prepare your statement &lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
and during execution specify the arguments represented with ''?''&lt;br /&gt;
 $inserth-&amp;gt;execute('Alaaddin'','Arabian Nights','Adventurer')|| die &amp;quot;Can't execute SQL&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you are querying your database, the query results are also accessed through your statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results. This function returns an array, so you can access any attribute from this array. As ''fetchrow_array'' returns only one row, you can call it continuously until it fails to return a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$sql=&amp;quot;insert into employee values ('aladdin','Arabian Nights','Adventurer')&amp;quot;; # sql for inserting&lt;br /&gt;
$sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
$sth-&amp;gt;finish();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, have some attributes and finish with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements.&lt;br /&gt;
&lt;br /&gt;
For example, consider the following HTML page&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code can be represented in a tree format&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hiararchical relation. For example, &amp;lt;html&amp;gt; node has two direct children, &amp;lt;head&amp;gt; and &amp;lt;body&amp;gt; and these siblings have their own children. Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree. You can find more about HTML::Trees at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod]].&lt;br /&gt;
In order to use ''HTML::Tree''s, you have to use ''HTML::TreeBuilder'' module.&lt;br /&gt;
 use ''HTML::TreeBuilder'';&lt;br /&gt;
 $tree=TML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
will create a new tree for you. You can then use this tree parse an html file. There are two ways to parse HTML, either directly parse a file with ''parse_file'' or parse a string with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can dump the contents of an HTML::Tree by calling ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before doing that you need to empty the existing parse data stored in the tree with ''delete'' function, otherwise, you may run out of memory.&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
Lets say we are interested in capturing headlines for  science news at  [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded this web page to ''bbc.html'', you can build a tree on it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information. Function ''look_down'' on an HTML::Tree or HTML::Element will return any child satisfying some criteria&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, following code will return all  HTML::Elements that are actually  hyperlinks in the page  and put it to an array.&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
left hand side of the call could be a variable instead of array,&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
In this case, instead of returning all links, it returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of look_down function comes from ability to specify further criteria in the following format:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has its SOMEATTRIBUTE has ITVALUE and the function specified in look_down returns 1. Usually that function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contains URLs to cse330 website.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 $_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;)=~m/www.cse330.org/&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing ''href'' attribute and using a regular expression to match it to ''www.cse330.org''.&lt;br /&gt;
&lt;br /&gt;
Now, as we said we are interested in capturing the BBC news headlines, we need to look at bbc.html to see what are the characteristics of the HTML::Elements containing the headlines.&lt;br /&gt;
&lt;br /&gt;
For example, in the current website, the main headline has the following lines (headline followed by a description)&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
....&lt;br /&gt;
            &lt;br /&gt;
		&lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence if we look down on the hyperlinks that have  class types ''tsh'' and ''shl'', we will get the hyperlinks that contain the headlines.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines but not the summaries. But no worries, if you look at the BBC html code, the headlines are inside a &amp;lt;div&amp;gt; element, and the summaries are in the next &amp;lt;div&amp;gt;. So we can reach those summaries by first moving to parent of the hyperlink and reaching the next sibling of that parent.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
  print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you should be careful about these kind of operations, if, for example parent was undefined, the above code was going to be interrupted during run time. A better way is  first to check if $links[$i]-&amp;gt;parent was defined and then check if parent's right sibling was defined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, you can use LWP::UserAgent to actually make a request to the website, get the contents and pass those contents to the tree, instead of downloading the a website and calling the parser  on that file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || #that are tsh  class&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href')=~m/science/&lt;br /&gt;
             )             ||# or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/ #or their class name ends with hl&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
 if( !($links[$i]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;)) {print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;}&lt;br /&gt;
  else {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some other examples can be found at Perl documentation for Tree Scanning at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod]&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=95</id>
		<title>Perl</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Perl&amp;diff=95"/>
		<updated>2009-10-25T18:08:50Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Teams for this module */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Perl=&lt;br /&gt;
Perl is an interpreted language. It is syntax is similar to Bash and PHP. It is very powerful tool to  automate several tasks that you may find too repetitive for you to do. It has a very large set of modules for any task you can imagine and support regular expressions which makes it very easy to process text.&lt;br /&gt;
==Running Perl==&lt;br /&gt;
Like many scripting languages, Perl programs can be run in several ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;as an input to perl executable  &amp;lt;pre&amp;gt;perl INPUTFILE&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;directly through executable script&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
......&lt;br /&gt;
your code here&lt;br /&gt;
....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
and change it to executable and run&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chmod a+x perlfile&lt;br /&gt;
./perlfile &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;typing in perl interface&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$perl&lt;br /&gt;
type-your-code-here....&lt;br /&gt;
end-with-ctrl-d.....&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As we will see, Perl is very flexible and this may result in relaxation of the programmer's discipline to write good program. It is usually better to force a strict  perl by using&lt;br /&gt;
 use strict;&lt;br /&gt;
so that Perl will complain on possible errors on your side (like trying to access undefined variables).&lt;br /&gt;
&lt;br /&gt;
== Variables and Arrays==&lt;br /&gt;
 &lt;br /&gt;
The variables   require ''$'' at the beginning of their names (regardless of the purpose, be it setting, be it accessing). You don't need to specify type of the variable, as it will be decided based on your setting.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #this is an integer&lt;br /&gt;
 $i=5;&lt;br /&gt;
 #this is a string&lt;br /&gt;
 $msg=&amp;quot;my string&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
Notice that we have to put '';'' just like most other high level languages. You can print  variables with ''print'' function.&lt;br /&gt;
&lt;br /&gt;
 print $msg;&lt;br /&gt;
&lt;br /&gt;
you can put a variable inside a string, and the value of that variable will be included in the string. (Note that you can also use ''printf'' function that enables formatting).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
  $i=5;&lt;br /&gt;
  print &amp;quot;The value of i is $i&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
This will generate the string&lt;br /&gt;
 The value of i is 5&lt;br /&gt;
&lt;br /&gt;
All variables are global so you need to specify locality if you want to have local variables. This could be done by ''local'' and ''my'' keywords. Generally, ''my'' is suggested to be used, as it is safer and faster. If a variable is defined by ''my'' in a block, it will be local to the block.  ''local'' is usually confused with ''my'' which in most cases what you want. If a variable is defined by ''local'', any subroutine call from that block will also have that variable defined.&lt;br /&gt;
&lt;br /&gt;
===Arrays===&lt;br /&gt;
In Perl arrays are represented with ''@'' during declerations  but accessed with ''$'' variable.&lt;br /&gt;
Declaring arrays is very easy in Perl. Either you can enter all element one by one, or you can enter them together:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$student[0]=&amp;quot;Alice&amp;quot;;&lt;br /&gt;
$student[1]=&amp;quot;Bruce&amp;quot;;&lt;br /&gt;
$student[2]=&amp;quot;Charlie&amp;quot;;&lt;br /&gt;
$student[3]=&amp;quot;Dan&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 or alternatively&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
@student=(&amp;quot;Alice&amp;quot;,&amp;quot;Bruce&amp;quot;,&amp;quot;Charlie&amp;quot;,&amp;quot;Dan&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In general,  the parentheses, ''(value1,value2,...,valuen )'' are interpreted as arrays.&lt;br /&gt;
Then you can access the contents with an array index&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
print $student[2];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Or you can print whole array with&lt;br /&gt;
&lt;br /&gt;
 print @student;&lt;br /&gt;
&lt;br /&gt;
This will put all the content without a delimeter. Alternative is to print inside a string:&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;@student';&lt;br /&gt;
&lt;br /&gt;
which will print nicely with a space between each element.&lt;br /&gt;
The size of an array can be reachable with ''scalar'' command &lt;br /&gt;
 $sz=scalar(@student);&lt;br /&gt;
&lt;br /&gt;
You can declare a sequence with &lt;br /&gt;
 (START..END);&lt;br /&gt;
&lt;br /&gt;
START and END could be a number or a character&lt;br /&gt;
&lt;br /&gt;
 @newarray=(1..20);&lt;br /&gt;
 @newchars=('l'..'t'');&lt;br /&gt;
&lt;br /&gt;
Perl arrays also support adding and removing elements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''push()''' - adds an element to the end of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''unshift()''' - adds an element to the beginning of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''pop()''' - removes the last element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;'''shift()''' - removes the first element of an array.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
push and unshift takes an array and a variable.&lt;br /&gt;
 push @ARRAY, VARIABLE&lt;br /&gt;
&lt;br /&gt;
variable could be a single variable or another array.&lt;br /&gt;
&lt;br /&gt;
 push @student, &amp;quot;Eve&amp;quot;;&lt;br /&gt;
 push @student,@newstudents;&lt;br /&gt;
 push @student,(&amp;quot;Frank&amp;quot;,&amp;quot;Gabriel&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
pop and shift take  array name as input&lt;br /&gt;
&lt;br /&gt;
 $onestudent=pop(@student);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can get only a portion of an array with ''slice'' operation. &lt;br /&gt;
&lt;br /&gt;
 @newarray=@student[1..3];&lt;br /&gt;
 @newarray=@student[1..3,10..15,.20];&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''splice'' function lets you replace arbitrary elements within the array&lt;br /&gt;
&lt;br /&gt;
  splice(@ARRAY,STARTING_INDEX,LENGTH,@REPLACING-ARRAY)&lt;br /&gt;
  &lt;br /&gt;
  splice(@student,3,2,(&amp;quot;Jerry&amp;quot;,&amp;quot;Lena&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Split operating creates an array from a string, it takes a delimater and a string&lt;br /&gt;
  @array=split(':',&amp;quot;test:array:1:2:3&amp;quot;);&lt;br /&gt;
and  ''join'' function does the reverse, i.e., create a string from the elements of an array. It takes a deliminator and an array&lt;br /&gt;
  $studentNames=join(',',@student);&lt;br /&gt;
&lt;br /&gt;
Perl also has hash arrays (similar to associate arrays). They are represented by ''%'' and the elements can be accessed by strings.&lt;br /&gt;
&lt;br /&gt;
 %ages = (&amp;quot;Michael Caine&amp;quot;, 39,&lt;br /&gt;
         &amp;quot;Dirty Den&amp;quot;, 34,&lt;br /&gt;
         &amp;quot;Angie&amp;quot;, 27,&lt;br /&gt;
         &amp;quot;Willy&amp;quot;, &amp;quot;21 in dog years&amp;quot;,&lt;br /&gt;
         &amp;quot;The Queen Mother&amp;quot;, 108);&lt;br /&gt;
&lt;br /&gt;
Now we can find the age of people with the following expressions&lt;br /&gt;
&lt;br /&gt;
$ages{&amp;quot;Michael Caine&amp;quot;};		# Returns 39&lt;br /&gt;
$ages{&amp;quot;Dirty Den&amp;quot;};		# Returns 34&lt;br /&gt;
$ages{&amp;quot;Angie&amp;quot;};			# Returns 27&lt;br /&gt;
$ages{&amp;quot;Willy&amp;quot;};			# Returns &amp;quot;21 in dog years&amp;quot;&lt;br /&gt;
$ages{&amp;quot;The Queen Mother&amp;quot;};	# Returns 108&lt;br /&gt;
&lt;br /&gt;
== Conditional Statements ==&lt;br /&gt;
''if'' statements provide the conditional statements. The general format is&lt;br /&gt;
 if ( CONDITION ) {&lt;br /&gt;
     some statements&lt;br /&gt;
 } else {&lt;br /&gt;
 }&lt;br /&gt;
 You can use standard comparison operators such as ''=='',''&amp;lt;'',''&amp;gt;'' etc.&lt;br /&gt;
&lt;br /&gt;
 if ( $string eq &amp;quot;this is my string&amp;quot;) { #note that for strings, it is ''eq'' &lt;br /&gt;
   echo &amp;quot;strings are equal&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
  if ($average_temperature&amp;gt;100) {&lt;br /&gt;
    echo &amp;quot;No, no, there is no such a thing called global warming!!!!&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loops ==&lt;br /&gt;
You can use ''for'' or ''while'' loops. The formats are very similar to C.&lt;br /&gt;
&lt;br /&gt;
 for(initialization;condition;loop_increment){&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
 while (condition){&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 For example, following code will print the sum of number 1 from 10.&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
  $sum=0;&lt;br /&gt;
  $i=1;&lt;br /&gt;
  while($i&amp;lt;=10) {&lt;br /&gt;
    $sum=$sum+$i;&lt;br /&gt;
    $i++;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 print &amp;quot;sum is $sum&amp;quot;;&lt;br /&gt;
 &amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''break'' and ''continue'' are special commands that will interrupt the loop execution, or jump to the end of loop.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
Perl  functions are identified with the keyword ''sub'' without any arguments. The arguments are stored in a special array ''@_''followed by a function name and argument list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
sub functionname {&lt;br /&gt;
&lt;br /&gt;
  my ($arg1,$arg2,...,$argn)=@_;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 sub add {&lt;br /&gt;
     my ($x,$y)=@_&lt;br /&gt;
     return ($x+$y);&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
 print  &amp;quot;Sum of 2+5=&amp;quot;,add(2,5);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
=Perl Modules=&lt;br /&gt;
Perl modules are libraries that other people developed for several useful task. [http://www.cpan.org] is the portal that contains most of the modules. It also has manuals for each module (most of which contain examples). While most of the modules are perl programs themselves, some may require compilation of libraries written using other languages. &lt;br /&gt;
&lt;br /&gt;
The  typical location for perl modules is ''/usr/lib/perl/XXX'' where XXX is the version of perl. You can define other locations by setting ''PERL5LIB'' environmental variable. &lt;br /&gt;
 export PERL5LIB=directory1:directory2&lt;br /&gt;
&lt;br /&gt;
 A module is described in ''.pm'' file located in one of the module directories (either default or through PERL5LIB)  and accessible in a perl script through ''use'' command&lt;br /&gt;
 use PERL-MODULE&lt;br /&gt;
&lt;br /&gt;
If your modules is not installed in the system, you need to install it either by manually or through automated ''cpan'' command.&lt;br /&gt;
==Manual Module Installation==&lt;br /&gt;
After you downloaded your module from CPAN, untar it (or unzip) and change to the directory containing the source code. You will notice that there is a Makefile.PL. That is a perl script that generates a makefile for your perl installation. Run this&lt;br /&gt;
 perl Makefile.PL&lt;br /&gt;
&lt;br /&gt;
This generates the actual makefile. If you want to install it to a non-default module location, set PREFIX variable&lt;br /&gt;
 perl Makefile.PL PREFIX=my_module_directory&lt;br /&gt;
&lt;br /&gt;
Once you have the makefile ready, you can make  module, test it and install it. Most Perl modules requires a test to verify that it is working. If a critical component of the test fails, you may have to force it to install.&lt;br /&gt;
&lt;br /&gt;
 make&lt;br /&gt;
 make test&lt;br /&gt;
 make install&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some modules require prerequisites which you need to install yourself. Some cases, such dependencies could branch easily so it may be hard to install everything  manually. Perl provides a tool that makes module installation easier as we will see next.&lt;br /&gt;
==Automated Module Installation==&lt;br /&gt;
''cpan'' is a perl script that uses perl module Cpan. It is a tool that installs a module with minimum user interaction and auto-installs prerequisites. &lt;br /&gt;
 cpan&lt;br /&gt;
will start it. If it is the first time you are running ''cpan'', it will ask for configuration options. You can select the automated configuration and it will fill out the options for you. Alternatively, you can go with manual selection and specify each option by hand. The last option asks for one or more CPAN mirror locations. Make sure you have selected ''North America-&amp;gt;United States'' and select a nearby mirror. I usually go with ''perl.com''  as my main mirror and add a few in case ''perl.com'' is down. Don't worry, if you are not happy with your configuration, you can modify it  later.&lt;br /&gt;
&lt;br /&gt;
Once you are in ''cpan'' prompt, you can  interact with it&lt;br /&gt;
 '''cpan&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
help command gives you the available options. You can search a module with ''i'' command&lt;br /&gt;
 ''cpan&amp;gt;''' i /KEYWORDS/&lt;br /&gt;
&lt;br /&gt;
and you can install a module with ''install'' command&lt;br /&gt;
 ''cpan&amp;gt;''' install /MODULENAME/&lt;br /&gt;
&lt;br /&gt;
''install'' command will download the module, compile and test it and finally install it if everything goes well. The downloaded programs are stored under ''~/.cpan '' directory (unless you specified another one during configuration). Although ''cpan'' will download the other prerequistid perl modules, if you have missing library in your system (such as graphics) that your module requires, cpan installation may fail. If  it happens, you can go to the build directory ''~/.cpan/build/YOUR-MODULE-NAME'' and try to fix the problem. ''look'' command will also take you to the compilation directory. You can leave ''cpan'' with ''q'' command.&lt;br /&gt;
&lt;br /&gt;
=Connecting MySQL with Perl=&lt;br /&gt;
&lt;br /&gt;
In order to connect a MySQL you need to have DBI module and DBI::mysql installed in your system. Following guide has a good description of DBI interface in general [http://www.perl.com/pub/a/1999/10/DBI.html]&lt;br /&gt;
&lt;br /&gt;
Then you can connect a mysql database with the command&lt;br /&gt;
 DBI-&amp;gt;connect(DATABASE_DESCRIPTION,USERNAME,PASS);&lt;br /&gt;
The databse description contains the type of the database (so that DBI knows how to connect), the name of the database and the host name. It returns a handle that you will use for this connection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;; #note $dbh is your database handle&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you have the connection established you can send SQL queries to your database server. First you need to prepare your SQL with ''prepare'' command on the database handle.&lt;br /&gt;
&lt;br /&gt;
$dbh-&amp;gt;prepare(&amp;quot;TYPE YOUR SQL HERE&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually returns a statement handle ($sth in our example below) that you will use for the database operations.&lt;br /&gt;
&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values ('Alaaddin'','Arabian Nights','Adventurer')&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You have to be careful about the your quotation marks. An easier way is to use perl qq function (which is  generalize quotation)&lt;br /&gt;
&lt;br /&gt;
 $sql=qq(insert into employee values ('Alaaddin'','Arabian Nights','Adventurer'));&lt;br /&gt;
 $sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have a statement prepared, you can execute it with execute command:&lt;br /&gt;
&lt;br /&gt;
 $sth-&amp;gt;execute() ||die &amp;quot;Can't execute sql&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
''prepare'' command actually helps you bind variables so that you don't have to repeat them. For example, if you want to have a generic way to insert new values, you can prepare your statement &lt;br /&gt;
 $inserth=$dbh-&amp;gt;prepare(&amp;quot;insert into employee values (?,?,?)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
and during execution specify the arguments represented with ''?''&lt;br /&gt;
 $inserth-&amp;gt;execute('Alaaddin'','Arabian Nights','Adventurer')|| die &amp;quot;Can't execute SQL&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you are querying your database, the query results are also accessed through your statement handle. After execution, ''$sth-&amp;gt;fetchrow_array'' returns a row from your results. This function returns an array, so you can access any attribute from this array. As ''fetchrow_array'' returns only one row, you can call it continuously until it fails to return a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
use DBI;&lt;br /&gt;
$database=&amp;quot;company;host=www.bayazit.net&amp;quot;;&lt;br /&gt;
$datasource=&amp;quot;dbi:mysql&amp;quot;;&lt;br /&gt;
$user=&amp;quot;boss&amp;quot;;&lt;br /&gt;
$pass=&amp;quot;employer&amp;quot;;&lt;br /&gt;
$dbh=DBI-&amp;gt;connect(&amp;quot;$datasource:$database&amp;quot;,$user,$pass) || die &amp;quot;error connecting&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
$sql=&amp;quot;insert into employee values ('aladdin','Arabian Nights','Adventurer')&amp;quot;; # sql for inserting&lt;br /&gt;
$sql=&amp;quot;select name,jobtitle from employee&amp;quot;;&lt;br /&gt;
$sth=$dbh-&amp;gt;prepare($sql);&lt;br /&gt;
$sth-&amp;gt;execute()|| die &amp;quot;Can't execute my sql&amp;quot;;&lt;br /&gt;
while(@attributes=$sth-&amp;gt;fetchrow_array) {&lt;br /&gt;
print &amp;quot;$attributes[0] $attributes[1] \n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
$sth-&amp;gt;finish();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Parsing HTML with Perl=&lt;br /&gt;
&lt;br /&gt;
Perl module [http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm HTML::Element] provides a structured way to represent HTML elements (starting with &amp;lt;tag&amp;gt;, have some attributes and finish with &amp;lt;/tag&amp;gt;).  Any HTML document can be represented as a tree made up of HTML::Elements.&lt;br /&gt;
&lt;br /&gt;
For example, consider the following HTML page&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;head&amp;gt;&lt;br /&gt;
&amp;amp;lt;title&amp;gt;List of Employees&amp;amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;center&amp;gt;&lt;br /&gt;
&amp;amp;lt;h1&amp;gt;Employees&amp;amp;lt;/h1&amp;gt; &amp;amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;table&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Name&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Department&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Title&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Alice&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Wonderland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Lost traveler&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;tr&amp;gt;&lt;br /&gt;
   &amp;amp;lt;td&amp;gt;Peter&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Neverland&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;Team leader&amp;amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code can be represented in a tree format&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt; &lt;br /&gt;
  &amp;amp;lt;head&amp;gt; &lt;br /&gt;
    &amp;amp;lt;title&amp;gt; &lt;br /&gt;
      &amp;quot;List of Employees&amp;quot;&lt;br /&gt;
  &amp;amp;lt;body&amp;gt; &lt;br /&gt;
    &amp;amp;lt;center&amp;gt; &lt;br /&gt;
      &amp;amp;lt;h1&amp;gt; &lt;br /&gt;
        &amp;quot;Employees&amp;quot;&lt;br /&gt;
      &amp;amp;lt;br&amp;gt; &lt;br /&gt;
      &amp;amp;lt;table&amp;gt; &lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Name&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Department&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Title&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Alice&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Wonderland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Lost traveler&amp;quot;&lt;br /&gt;
        &amp;amp;lt;tr&amp;gt; &lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Peter&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt; &lt;br /&gt;
            &amp;quot;Neverland&amp;quot;&lt;br /&gt;
          &amp;amp;lt;td&amp;gt;&lt;br /&gt;
            &amp;quot;Team leader&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice the hiararchical relation. For example, &amp;lt;html&amp;gt; node has two direct children, &amp;lt;head&amp;gt; and &amp;lt;body&amp;gt; and these siblings have their own children. Perl module [http://search.cpan.org/~sburke/HTML-Tree/lib/HTML/Tree.pm HTML::Tree] builds this tree. You can find more about HTML::Trees at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/AboutTrees.pod]].&lt;br /&gt;
In order to use ''HTML::Tree''s, you have to use ''HTML::TreeBuilder'' module.&lt;br /&gt;
 use ''HTML::TreeBuilder'';&lt;br /&gt;
 $tree=TML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
will create a new tree for you. You can then use this tree parse an html file. There are two ways to parse HTML, either directly parse a file with ''parse_file'' or parse a string with ''parse''.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;parse_file('downloaded.html');&lt;br /&gt;
&lt;br /&gt;
You can dump the contents of an HTML::Tree by calling ''dump'' function.&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;dump;&lt;br /&gt;
&lt;br /&gt;
You can call the parse function several times for different content, but before doing that you need to empty the existing parse data stored in the tree with ''delete'' function, otherwise, you may run out of memory.&lt;br /&gt;
 $tree-&amp;gt;delete;&lt;br /&gt;
&lt;br /&gt;
==HTML Scanning==&lt;br /&gt;
&lt;br /&gt;
Lets say we are interested in capturing headlines for  science news at  [http://news.bbc.co.uk/2/hi/science/nature/default.stm BBC Science News]. Assuming you have downloaded this web page to ''bbc.html'', you can build a tree on it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can use this tree to retrieve information. Function ''look_down'' on an HTML::Tree or HTML::Element will return any child satisfying some criteria&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE);&lt;br /&gt;
&lt;br /&gt;
For example, following code will return all  HTML::Elements that are actually  hyperlinks in the page  and put it to an array.&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
left hand side of the call could be a variable instead of array,&lt;br /&gt;
 $first_link=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;);&lt;br /&gt;
In this case, instead of returning all links, it returns the first hyperlink element found.&lt;br /&gt;
&lt;br /&gt;
The power of look_down function comes from ability to specify further criteria in the following format:&lt;br /&gt;
&lt;br /&gt;
 $tree-&amp;gt;look_down(SOMEATTRIBUTE,ITSVALUE, &lt;br /&gt;
       sub { SOME FUNCTION CODE HERE });&lt;br /&gt;
&lt;br /&gt;
This will return any element that has its SOMEATTRIBUTE has ITVALUE and the function specified in look_down returns 1. Usually that function evaluates other attributes of the element. The element that is being evaluated is referred as $_[0]. For example, the following code returns all hyperlinks that contains URLs to cse330 website.&lt;br /&gt;
&lt;br /&gt;
 @links=$tree-&amp;gt;look_down(&amp;quot;_tag&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
           sub { &lt;br /&gt;
                 $_[0]-&amp;gt;attr(&amp;quot;href&amp;quot;)=~m/www.cse330.org/&lt;br /&gt;
               });&lt;br /&gt;
&lt;br /&gt;
Note that we are accessing ''href'' attribute and using a regular expression to match it to ''www.cse330.org''.&lt;br /&gt;
&lt;br /&gt;
Now, as we said we are interested in capturing the BBC news headlines, we need to look at bbc.html to see what are the characteristics of the HTML::Elements containing the headlines.&lt;br /&gt;
&lt;br /&gt;
For example, in the current website, the main headline has the following lines (headline followed by a description)&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;tsh&amp;quot; href=&amp;quot;/2/hi/health/7026443.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Chilli compound fires painkiller&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A chemical from chilli peppers may be able to kill pain without affecting touch or movement.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
.......&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44151000/jpg/_44151791_art_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Artists impression of Gryposaurus monumentensis (copyright: Larry Felder)&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/science/nature/7023731.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Duck-billed dinosaur had big bite&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			A new species of duck-billed dinosaur that had up to 800 teeth is described by scientists.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
....&lt;br /&gt;
            &lt;br /&gt;
		&lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;mvb&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;amp;lt;a href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/media/images/44152000/jpg/_44152097_tombstone_66pic.jpg&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;66&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;Before and after tombstone&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&amp;amp;lt;img src=&amp;quot;http://newsimg.bbc.co.uk/shared/img/o.gif&amp;quot; align=&amp;quot;left&amp;quot; width=&amp;quot;5&amp;quot; height=&amp;quot;49&amp;quot; alt=&amp;quot;&amp;quot; border=&amp;quot;0&amp;quot; vspace=&amp;quot;0&amp;quot; hspace=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
		&amp;amp;lt;a class=&amp;quot;shl&amp;quot; href=&amp;quot;/2/hi/technology/7024672.stm&amp;quot;&amp;gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			&lt;br /&gt;
			Scans reveal lost gravestone text&lt;br /&gt;
			&lt;br /&gt;
		&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;amp;lt;br clear=&amp;quot;all&amp;quot; /&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		&lt;br /&gt;
	&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;amp;lt;div class=&amp;quot;o&amp;quot;&amp;gt;&lt;br /&gt;
	&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
			Illegible words on church headstones could be read once more thanks to a new scan technology.&lt;br /&gt;
			&lt;br /&gt;
		&lt;br /&gt;
			&lt;br /&gt;
&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence if we look down on the hyperlinks that have  class types ''tsh'' and ''shl'', we will get the hyperlinks that contain the headlines.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then by printing out these hyperlink elements as text, we can get the headlines &lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However, these elements just give us the headlines but not the summaries. But no worries, if you look at the BBC html code, the headlines are inside a &amp;lt;div&amp;gt; element, and the summaries are in the next &amp;lt;div&amp;gt;. So we can reach those summaries by first moving to parent of the hyperlink and reaching the next sibling of that parent.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
$tree-&amp;gt;parse_file(&amp;quot;bbc.html&amp;quot;);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a',&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; ||&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
  print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you should be careful about these kind of operations, if, for example parent was undefined, the above code was going to be interrupted during run time. A better way is  first to check if $links[$i]-&amp;gt;parent was defined and then check if parent's right sibling was defined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, you can use LWP::UserAgent to actually make a request to the website, get the contents and pass those contents to the tree, instead of downloading the a website and calling the parser  on that file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/perl&lt;br /&gt;
#&lt;br /&gt;
use LWP::UserAgent;&lt;br /&gt;
&lt;br /&gt;
$ua=LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
$req=$ua-&amp;gt;get(&amp;quot;http://news.bbc.co.uk/2/hi/science/nature/default.stm&amp;quot;);&lt;br /&gt;
use HTML::TreeBuilder;&lt;br /&gt;
$ua-&amp;gt;agent('Mozilla/5.0'); #you can modify several internal parameters, such as browser identification&lt;br /&gt;
&lt;br /&gt;
$tree=HTML::TreeBuilder-&amp;gt;new;&lt;br /&gt;
&lt;br /&gt;
$tree-&amp;gt;parse($req-&amp;gt;as_string);&lt;br /&gt;
@links=$tree-&amp;gt;look_down('_tag','a', #get all links&lt;br /&gt;
         sub {&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') eq &amp;quot;tsh&amp;quot; || #that are tsh  class&lt;br /&gt;
            ($_[0]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot; &amp;amp;&amp;amp; &lt;br /&gt;
             $_[0]-&amp;gt;attr('href')=~m/science/&lt;br /&gt;
             )             ||# or their url contain the keyword science and their parents belong to class arr&lt;br /&gt;
            $_[0]-&amp;gt;attr('class') =~m/hl$/ #or their class name ends with hl&lt;br /&gt;
            ;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         );&lt;br /&gt;
for($i=0;$i&amp;lt;scalar(@links);$i++) {&lt;br /&gt;
  print $links[$i]-&amp;gt;as_text,&amp;quot;:&amp;quot;;&lt;br /&gt;
 if( !($links[$i]-&amp;gt;parent-&amp;gt;attr('class') eq &amp;quot;arr&amp;quot;)) {print $links[$i]-&amp;gt;parent-&amp;gt;right-&amp;gt;as_text,&amp;quot;\n&amp;quot;;}&lt;br /&gt;
  else {&lt;br /&gt;
    print &amp;quot;Sideline \n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some other examples can be found at Perl documentation for Tree Scanning at [http://search.cpan.org/~sburke/HTML-Tree-3.18/lib/HTML/Tree/Scanning.pod]&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=CSE_330_Online_Textbook_-_Table_of_Contents&amp;diff=94</id>
		<title>CSE 330 Online Textbook - Table of Contents</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=CSE_330_Online_Textbook_-_Table_of_Contents&amp;diff=94"/>
		<updated>2009-10-05T13:34:48Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Module 1: [[System Administration]]&lt;br /&gt;
&lt;br /&gt;
Module 2: [[PHP]]&lt;br /&gt;
&lt;br /&gt;
Module 3: [[Databases and MySQL]]&lt;br /&gt;
&lt;br /&gt;
Module 4: [[Javascript and AJAX]]&lt;br /&gt;
&lt;br /&gt;
Module 5: Creative Programming 1&lt;br /&gt;
&lt;br /&gt;
Module 6: Perl&lt;br /&gt;
&lt;br /&gt;
Module 7: Remote Procedure Calls&lt;br /&gt;
&lt;br /&gt;
Module 8: Creative Programming 2&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Other Information:&lt;br /&gt;
&lt;br /&gt;
Project Ideas&lt;br /&gt;
&lt;br /&gt;
Firefox Extensions&lt;br /&gt;
&lt;br /&gt;
Content Management Systems&lt;br /&gt;
&lt;br /&gt;
Graphics Tools&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=93</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=93"/>
		<updated>2009-09-24T05:49:11Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* jQuery */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
Javascript is one of the most widely used scripting languages for preforming client-side functions in the web environment.  That is, the javascript code is interpreted and executed in a client's web browser.. not on the web server.  It is vaguely similar to Java, in that it is an object oriented language, but it was designed to be simpler for non-programmers to be able to accomplish common web processing tasks.&lt;br /&gt;
&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
Since the javascript code runs in the web browser, you'll have to use web browser tools for all debugging.  Generally speaking, if you have syntax or runtime errors in your javascript, the web browser will report nothing but not attempt to run the script.  So if it appears that your code is doing nothing, then it's likely a syntax error of some sort.  Fortunately, in Firefox, you can use the error console to view warnings and errors produced as javascripts are executed.  Go to Tools-&amp;gt;Error Console to access it.  This is most useful for simple syntatic debugging.  If you need a more serious debugger, you can use [http://getfirebug.com/ Firebug] for Firefox.&lt;br /&gt;
&lt;br /&gt;
=Asynchronous Javascript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that a large fraction of the work happens at the web browser, and larger requests happen behind the scenes, keeping waiting times for data to come back over the network to a minimum.  AJAX is generally also associated with higher-level APIs and libraries that aid in producing cleaner user interfaces for web applications.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=jQuery=&lt;br /&gt;
&lt;br /&gt;
jQuery is a fairly lightweight javascript library that has a number of particularly useful features.  Even thought it's only been around for a few years now, it has gained a lot of momentum in the web development community.  jQuery provides a simple interface to easily modify CSS attributes on all the elements in the HTML document, has simplified AJAX integration, and is accompanied by a number of handy user interface related APIs.  You can get a good overview of jQuery in the jQuery [http://docs.jquery.com/Tutorials:How_jQuery_Works Tutorials], and there is a short summary on [http://en.wikipedia.org/wiki/JQuery Wikipedia].  Of course, there are plenty of other tutorials a google search away.&lt;br /&gt;
&lt;br /&gt;
To use jQuery in your javascript, you need to include the jQuery javascript library, which is just another javascript file.  It's freely available online at the jQuery [http://docs.jquery.com/Downloading_jQuery website].  Assuming you've downloaded the file to your web server, put it in the directory with your PHP and HTML files, and renamed it ''jquery.js'', you include it by adding this line to the head section of your HTML of PHP file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;jquery.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also link to the jQuery library directly on other websites as mentioned in the above tutorials.  The trade-off is that having a local copy will load faster, but then requires you to manage the library files your self (e.g., downloading new versions).&lt;br /&gt;
&lt;br /&gt;
A few code snippets are given below, but the best way to learn about all the features of jQuery is to head to the [http://jquery.com jQuery website] and look through the tutorials, references, and examples there.&lt;br /&gt;
&lt;br /&gt;
The ''$'' symbol is used to denote jQuery functionality.  You can use it to call jQuery functions, access HTML document elements in order to read or write values, or modify style information via CSS for a range of document elements.  For example, here is some code that sets up a function to be called whenever the user clicks on a link:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $(&amp;quot;a&amp;quot;).click( function(event)&lt;br /&gt;
 {&lt;br /&gt;
   event.preventDefault();&lt;br /&gt;
   $(this).hide(&amp;quot;slow&amp;quot;);&lt;br /&gt;
 } );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code leverages many useful javascript and jQuery features.  ''$(&amp;quot;a&amp;quot;)'' is the jQuery way of selecting all HTML ''a'' elements.  That is, in this case, all elements in the HTML document that are ''&amp;lt;a&amp;gt;'' tags.  The ''click'' function is invoked for all such ''a'' elements in the document.  The argument to the ''click'' function is another function that is to be called when the selected elements are clicked by the user.  The ''event'' argument to this user-defined function is a default argument sent to functions which handle user actions like clicking a mouse button.  In this example, the code calls another function, ''preventDefault()'' on the event passed to stop the standard action on that event.  Here that means stopping the web browser from following the link that the user clicked on.  Finally, the ''$(this)'' variable is the jQuery way of accessing the current encapsulating object, as in Java.  This example calls the jQuery hide function on the object to make the object (again, in this case, ''this'' is referring to the ''&amp;lt;a&amp;gt;'' tag that the user clicked) disappear from the browser.  The script also passes an argument to the hide function to make the link disappear slowly as described in the API [http://docs.jquery.com/Hide documentation].&lt;br /&gt;
&lt;br /&gt;
jQuery also comes with some easy to use widgets for building better user interfaces.  One of the handiest is the [http://docs.jquery.com/UI/Dialog dialog] widget that pops-up an in-browser dialog window.  To use the dialog functions, you need to include a few more javascript libraries in your HTML header block like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;link type=&amp;quot;text/css&amp;quot; href=&amp;quot;http://jqueryui.com/latest/themes/base/ui.all.css&amp;quot; rel=&amp;quot;stylesheet&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/jquery-1.3.2.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.core.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.draggable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.resizable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.dialog.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
   // your code goes here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can then use the ''$().dialog()'' function to make dialog boxes.  The ''dialog'' function can take many arguments to customize its behavior and add standard elements like buttons; see the documentation for details.  The content of the dialog box is typically placed in a ''div'' container and then that container is hidden when the document loads.  You can put any elements in the ''div'' and they will be displayed as normal in the dialog box.  Here is a full example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&lt;br /&gt;
 &amp;lt;head&amp;gt;&lt;br /&gt;
  &amp;lt;style&amp;gt;&lt;br /&gt;
    #mydialog { display:none }&lt;br /&gt;
  &amp;lt;/style&amp;gt;&lt;br /&gt;
  &amp;lt;link type=&amp;quot;text/css&amp;quot; href=&amp;quot;http://jqueryui.com/latest/themes/base/ui.all.css&amp;quot; rel=&amp;quot;stylesheet&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/jquery-1.3.2.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.core.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.draggable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.resizable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.dialog.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
    function showdialog()&lt;br /&gt;
    {&lt;br /&gt;
      $(&amp;quot;#mydialog&amp;quot;).dialog();&lt;br /&gt;
    }&lt;br /&gt;
  &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  &amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Show Dialog&amp;quot; onclick=showdialog() /&amp;gt;&lt;br /&gt;
  &amp;lt;div id=&amp;quot;mydialog&amp;quot; title=&amp;quot;Howdy&amp;quot;&amp;gt;Look at me!&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To understand the page, start at the bottom.  There is a ''div'' element with a title and some string content.  It also has a unique id (the ids can be anything you want) which is used to identify this particular element in the rest of the document.  This div's id is ''mydialog''.  Above that is a button input.  The only thing special about it is the ''onclick'' attribute which specifies a javascript function to call when the button is clicked by a user.  In this case, the function is ''showdialog()'' which is a user defined javascript function contained in the document header.  All the function does is access the document element with id ''mydialog'' (the ''#identifier'' is the jQuery way of accessing an individual element based on the assigned id in the element attribute list), and build a dialog box from it.  Here, the dialog box will have the title ''Howdy'' from the title attribute in the ''mydialog'' element, and have a text string ''Look at me!''.  Finally, at the top of the document is the ''style'' section, which is a normal HTML/CSS section describing appearance of parts of the document.  There is only one entry, again describing the document element with id ''mydialog''.  One style attribute is set, namely the ''display'' attribute is set to ''none'' to indicate that the element should not be displayed.  Otherwise, the ''mydialog'' div would be displayed like any other part of the document rather than being used solely for our dialog box.&lt;br /&gt;
&lt;br /&gt;
Here is another example of using jQuery to do simple animations by moving an image across the browser window:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Other Resources=&lt;br /&gt;
There are a number of other w3schools tutorials and references that you may find useful, including [http://www.w3schools.com/php/default.asp PHP] (note the PHP and AJAX section), [http://www.w3schools.com/htmldom/default.asp DOM], [http://www.w3schools.com/ajax/default.asp AJAX], and [http://www.w3schools.com/css/default.asp CSS].  Many other resources exist around the web, and you should feel free to incorporate any existing libraries or other javascript or AJAX-related tools.  For example, the [http://code.google.com/apis/ajaxlibs/ Google AJAX APIs].&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=92</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=92"/>
		<updated>2009-09-24T05:45:58Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* jQuery */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
Javascript is one of the most widely used scripting languages for preforming client-side functions in the web environment.  That is, the javascript code is interpreted and executed in a client's web browser.. not on the web server.  It is vaguely similar to Java, in that it is an object oriented language, but it was designed to be simpler for non-programmers to be able to accomplish common web processing tasks.&lt;br /&gt;
&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
Since the javascript code runs in the web browser, you'll have to use web browser tools for all debugging.  Generally speaking, if you have syntax or runtime errors in your javascript, the web browser will report nothing but not attempt to run the script.  So if it appears that your code is doing nothing, then it's likely a syntax error of some sort.  Fortunately, in Firefox, you can use the error console to view warnings and errors produced as javascripts are executed.  Go to Tools-&amp;gt;Error Console to access it.  This is most useful for simple syntatic debugging.  If you need a more serious debugger, you can use [http://getfirebug.com/ Firebug] for Firefox.&lt;br /&gt;
&lt;br /&gt;
=Asynchronous Javascript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that a large fraction of the work happens at the web browser, and larger requests happen behind the scenes, keeping waiting times for data to come back over the network to a minimum.  AJAX is generally also associated with higher-level APIs and libraries that aid in producing cleaner user interfaces for web applications.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=jQuery=&lt;br /&gt;
&lt;br /&gt;
jQuery is a fairly lightweight javascript library that has a number of particularly useful features.  Even thought it's only been around for a few years now, it has gained a lot of momentum in the web development community.  jQuery provides a simple interface to easily modify CSS attributes on all the elements in the HTML document, has simplified AJAX integration, and is accompanied by a number of handy user interface related APIs.  You can get a good overview of jQuery in the jQuery [http://docs.jquery.com/Tutorials:How_jQuery_Works Tutorials], and there is a short summary on [http://en.wikipedia.org/wiki/JQuery Wikipedia].  Of course, there are plenty of other tutorials a google search away.&lt;br /&gt;
&lt;br /&gt;
To use jQuery in your javascript, you need to include the jQuery javascript library, which is just another javascript file.  It's freely available online at the jQuery [http://docs.jquery.com/Downloading_jQuery website].  Assuming you've downloaded the file to your web server, put it in the directory with your PHP and HTML files, and renamed it ''jquery.js'', you include it by adding this line to the head section of your HTML of PHP file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;jquery.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also link to the jQuery library directly on other websites as mentioned in the above tutorials.  The trade-off is that having a local copy will load faster, but then requires you to manage the library files your self (e.g., downloading new versions).&lt;br /&gt;
&lt;br /&gt;
A few code snippets are given below, but the best way to learn about all the features of jQuery is to head to the [http://jquery.com jQuery website] and look through the tutorials, references, and examples there.&lt;br /&gt;
&lt;br /&gt;
The ''$'' symbol is used to denote jQuery functionality.  You can use it to call jQuery functions, access HTML document elements in order to read or write values, or modify style information via CSS for a range of document elements.  For example, here is some code that sets up a function to be called whenever the user clicks on a link:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $(&amp;quot;a&amp;quot;).click( function(event)&lt;br /&gt;
 {&lt;br /&gt;
   event.preventDefault();&lt;br /&gt;
   $(this).hide(&amp;quot;slow&amp;quot;);&lt;br /&gt;
 } );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code leverages many useful javascript and jQuery features.  ''$(&amp;quot;a&amp;quot;)'' is the jQuery way of selecting all HTML ''a'' elements.  That is, in this case, all elements in the HTML document that are ''&amp;lt;a&amp;gt;'' tags.  The ''click'' function is invoked for all such ''a'' elements in the document.  The argument to the ''click'' function is another function that is to be called when the selected elements are clicked by the user.  The ''event'' argument to this user-defined function is a default argument sent to functions which handle user actions like clicking a mouse button.  In this example, the code calls another function, ''preventDefault()'' on the event passed to stop the standard action on that event.  Here that means stopping the web browser from following the link that the user clicked on.  Finally, the ''$(this)'' variable is the jQuery way of accessing the current encapsulating object, as in Java.  This example calls the jQuery hide function on the object to make the object (again, in this case, ''this'' is referring to the ''&amp;lt;a&amp;gt;'' tag that the user clicked) disappear from the browser.  The script also passes an argument to the hide function to make the link disappear slowly as described in the API [http://docs.jquery.com/Hide documentation].&lt;br /&gt;
&lt;br /&gt;
jQuery also comes with some easy to use widgets for building better user interfaces.  One of the handiest is the [http://docs.jquery.com/UI/Dialog dialog] widget that pops-up an in-browser dialog window.  To use the dialog functions, you need to include a few more javascript libraries in your HTML header block like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;link type=&amp;quot;text/css&amp;quot; href=&amp;quot;http://jqueryui.com/latest/themes/base/ui.all.css&amp;quot; rel=&amp;quot;stylesheet&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/jquery-1.3.2.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.core.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.draggable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.resizable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.dialog.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
   // your code goes here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can then use the ''$().dialog()'' function to make dialog boxes.  The ''dialog'' function can take many arguments to customize its behavior and add standard elements like buttons; see the documentation for details.  The content of the dialog box is typically placed in a ''div'' container and then that container is hidden when the document loads.  You can put any elements in the ''div'' and they will be displayed as normal in the dialog box.  Here is a full example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&lt;br /&gt;
 &amp;lt;head&amp;gt;&lt;br /&gt;
  &amp;lt;style&amp;gt;&lt;br /&gt;
    #dialog { display:none }&lt;br /&gt;
  &amp;lt;/style&amp;gt;&lt;br /&gt;
  &amp;lt;link type=&amp;quot;text/css&amp;quot; href=&amp;quot;http://jqueryui.com/latest/themes/base/ui.all.css&amp;quot; rel=&amp;quot;stylesheet&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/jquery-1.3.2.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.core.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.draggable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.resizable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.dialog.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
    function showdialog()&lt;br /&gt;
    {&lt;br /&gt;
      $(&amp;quot;#dialog&amp;quot;).dialog();&lt;br /&gt;
    }&lt;br /&gt;
  &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  &amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Show Dialog&amp;quot; onclick=showdialog() /&amp;gt;&lt;br /&gt;
  &amp;lt;div id=&amp;quot;dialog&amp;quot; title=&amp;quot;Howdy&amp;quot;&amp;gt;Look at me!&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To understand the page, start at the bottom.  There is a ''div'' element with a title and some string content.  It also has a unique id (the ids can be anything you want) which is used to identify this particular element in the rest of the document.  Above that is a button input.  The only thing special about it is the ''onclick'' attribute which specifies a javascript function to call when the button is clicked by a user.  In this case, the function is ''showdialog()'' which is a user defined javascript function contained in the document header.  All the function does is access the document element with id ''dialog'' (the ''#identifier'' is the jQuery way of accessing an individual element based on the assigned id in the element attribute list), and build a dialog box from it.  Here, the dialog box will have the title ''Howdy'' from the title attribute in the ''#dialog'' element, and have a text string ''Look at me!''.  Finally, at the top of the document is the ''style'' section, which is a normal HTML/CSS section describing appearance of parts of the document.  There is only one entry, again describing the document element with id ''dialog''.  One style attribute is set, namely the ''display'' attribute to set to ''none'' to indicate that the element should not be displayed.  Otherwise, the ''#dialog'' div would be displayed like any other part of the document rather than being used solely for our dialog box.&lt;br /&gt;
&lt;br /&gt;
Here is another example of using jQuery to do simple animations by moving an image across the browser window:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Other Resources=&lt;br /&gt;
There are a number of other w3schools tutorials and references that you may find useful, including [http://www.w3schools.com/php/default.asp PHP] (note the PHP and AJAX section), [http://www.w3schools.com/htmldom/default.asp DOM], [http://www.w3schools.com/ajax/default.asp AJAX], and [http://www.w3schools.com/css/default.asp CSS].  Many other resources exist around the web, and you should feel free to incorporate any existing libraries or other javascript or AJAX-related tools.  For example, the [http://code.google.com/apis/ajaxlibs/ Google AJAX APIs].&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=91</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=91"/>
		<updated>2009-09-24T05:38:47Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* jQuery */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
Javascript is one of the most widely used scripting languages for preforming client-side functions in the web environment.  That is, the javascript code is interpreted and executed in a client's web browser.. not on the web server.  It is vaguely similar to Java, in that it is an object oriented language, but it was designed to be simpler for non-programmers to be able to accomplish common web processing tasks.&lt;br /&gt;
&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
Since the javascript code runs in the web browser, you'll have to use web browser tools for all debugging.  Generally speaking, if you have syntax or runtime errors in your javascript, the web browser will report nothing but not attempt to run the script.  So if it appears that your code is doing nothing, then it's likely a syntax error of some sort.  Fortunately, in Firefox, you can use the error console to view warnings and errors produced as javascripts are executed.  Go to Tools-&amp;gt;Error Console to access it.  This is most useful for simple syntatic debugging.  If you need a more serious debugger, you can use [http://getfirebug.com/ Firebug] for Firefox.&lt;br /&gt;
&lt;br /&gt;
=Asynchronous Javascript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that a large fraction of the work happens at the web browser, and larger requests happen behind the scenes, keeping waiting times for data to come back over the network to a minimum.  AJAX is generally also associated with higher-level APIs and libraries that aid in producing cleaner user interfaces for web applications.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=jQuery=&lt;br /&gt;
&lt;br /&gt;
jQuery is a fairly lightweight javascript library that has a number of particularly useful features.  Even thought it's only been around for a few years now, it has gained a lot of momentum in the web development community.  jQuery provides a simple interface to easily modify CSS attributes on all the elements in the HTML document, has simplified AJAX integration, and is accompanied by a number of handy user interface related APIs.  You can get a good overview of jQuery in the jQuery [http://docs.jquery.com/Tutorials:How_jQuery_Works Tutorials], and there is a short summary on [http://en.wikipedia.org/wiki/JQuery Wikipedia].  Of course, there are plenty of other tutorials a google search away.&lt;br /&gt;
&lt;br /&gt;
To use jQuery in your javascript, you need to include the jQuery javascript library, which is just another javascript file.  It's freely available online at the jQuery [http://docs.jquery.com/Downloading_jQuery website].  Assuming you've downloaded the file to your web server, put it in the directory with your PHP and HTML files, and renamed it ''jquery.js'', you include it by adding this line to the head section of your HTML of PHP file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;jquery.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also link to the jQuery library directly on other websites as mentioned in the above tutorials.  The trade-off is that having a local copy will load faster, but then requires you to manage the library files your self (e.g., downloading new versions).&lt;br /&gt;
&lt;br /&gt;
A few code snippets are given below, but the best way to learn about all the features of jQuery is to head to the [http://jquery.com jQuery website] and look through the tutorials, references, and examples there.&lt;br /&gt;
&lt;br /&gt;
The ''$'' symbol is used to denote jQuery functionality.  You can use it to call jQuery functions, access HTML document elements in order to read or write values, or modify style information via CSS for a range of document elements.  For example, here is some code that sets up a function to be called whenever the user clicks on a link:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $(&amp;quot;a&amp;quot;).click( function(event)&lt;br /&gt;
 {&lt;br /&gt;
   event.preventDefault();&lt;br /&gt;
   $(this).hide(&amp;quot;slow&amp;quot;);&lt;br /&gt;
 } );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code leverages many useful javascript and jQuery features.  ''$(&amp;quot;a&amp;quot;)'' is the jQuery of selecting all HTML ''a'' elements.  That is, in this case, all elements in the HTML document that are ''&amp;lt;a&amp;gt;'' tags.  The ''.click'' function is invoked for all such ''a'' elements in the document.  The argument to the ''click'' function is another function that is to be called when the selected elements are clicked by the user.  The ''event'' argument to this user-defined function is a default argument sent to function which handle user actions like clicking a mouse button.  In this example, the code calls another function on the event passed in ''preventDefault()'' to stop the standard action on that event.  Here that means stopping the web browser from following the link that the user clicked in.  Finally, the ''$(this)'' variable is the jQuery way of, accessing the current encapsulating object, as in Java.  This example calls the jQuery hide function on the object to make the object (again, in this case, ''this'' is referring to the ''&amp;lt;a&amp;gt;'' tag that the user clicked) disappear from the browser.  The script also passes an argument to the hide function to make the link disappear slowly as described in the API [http://docs.jquery.com/Hide documentation].&lt;br /&gt;
&lt;br /&gt;
jQuery also comes with some use to use widgets for building better user interfaces.  One of the handiest is the [http://docs.jquery.com/UI/Dialog dialog] widget that pops-up an in-browser dialog.  To use the dialog functions, you need to include a few more javascript libraries in your HTML header block like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;link type=&amp;quot;text/css&amp;quot; href=&amp;quot;http://jqueryui.com/latest/themes/base/ui.all.css&amp;quot; rel=&amp;quot;stylesheet&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/jquery-1.3.2.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.core.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.draggable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.resizable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.dialog.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
   // your code goes here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can then use the ''$().dialog()'' function to make dialog boxes.  The ''dialog'' function takes a lot of arguments to customize its behavior and add standard elements like buttons; see the documentation for details.  The content of the dialog box is typically placed in a ''div'' container and then that container is hidden when the document loads.  You can put any elements in the ''div' and they will be displayed as normal in the dialog box.  Here is a full example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&lt;br /&gt;
 &amp;lt;head&amp;gt;&lt;br /&gt;
  &amp;lt;style&amp;gt;&lt;br /&gt;
    #dialog { display:none }&lt;br /&gt;
  &amp;lt;/style&amp;gt;&lt;br /&gt;
  &amp;lt;link type=&amp;quot;text/css&amp;quot; href=&amp;quot;http://jqueryui.com/latest/themes/base/ui.all.css&amp;quot; rel=&amp;quot;stylesheet&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/jquery-1.3.2.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.core.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.draggable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.resizable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.dialog.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
    function showdialog()&lt;br /&gt;
    {&lt;br /&gt;
      $(&amp;quot;#dialog&amp;quot;).dialog();&lt;br /&gt;
    }&lt;br /&gt;
  &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  &amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Show Dialog&amp;quot; onclick=showdialog() /&amp;gt;&lt;br /&gt;
  &amp;lt;div id=&amp;quot;dialog&amp;quot; title=&amp;quot;Howdy&amp;quot;&amp;gt;Look at me!&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To understand the page, start at the bottom.  There is a ''div'' element with a title and some string content.  It also has a unique id (the ids can be anything you want) which is used to identify this particular element in the rest of the document.  Above that is a button input.  The only thing special about it is the ''onclick'' attribute which specifies a javascript function to call when the button is clicked by a user.  In this case, the function is ''showdialog()'' which is a user defined javascript function contained in the document header.  All the function does is access the document element with id ''dialog'' (''$(''#identifier&amp;quot;)'' is the jQuery way of accessing an individual element based on the assigned id in the element attribute list), and build a dialog box from it.  Here, the dialog box will have the title ''Howdy'' from the title attribute in the ''#dialog'' element, and have a text string ''Look at me!''.  Finally, at the top of the document is the ''style'' section, which is a normal HTML/CSS section describing appearance of parts of the document.  There is only one entry, again describing the document element with id ''dialog''.  One style attribute is set, namely the ''display'' attribute to set to ''none'' to indicate that the element should not be displayed.  Otherwise, the ''#dialog'' div would be displayed like any other part of the document rather than being used solely for our dialog box.&lt;br /&gt;
&lt;br /&gt;
Here is another example of using jQuery to do simple animations by moving an image across the browser window:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Other Resources=&lt;br /&gt;
There are a number of other w3schools tutorials and references that you may find useful, including [http://www.w3schools.com/php/default.asp PHP] (note the PHP and AJAX section), [http://www.w3schools.com/htmldom/default.asp DOM], [http://www.w3schools.com/ajax/default.asp AJAX], and [http://www.w3schools.com/css/default.asp CSS].  Many other resources exist around the web, and you should feel free to incorporate any existing libraries or other javascript or AJAX-related tools.  For example, the [http://code.google.com/apis/ajaxlibs/ Google AJAX APIs].&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=90</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=90"/>
		<updated>2009-09-24T05:37:17Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* jQuery */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
Javascript is one of the most widely used scripting languages for preforming client-side functions in the web environment.  That is, the javascript code is interpreted and executed in a client's web browser.. not on the web server.  It is vaguely similar to Java, in that it is an object oriented language, but it was designed to be simpler for non-programmers to be able to accomplish common web processing tasks.&lt;br /&gt;
&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
Since the javascript code runs in the web browser, you'll have to use web browser tools for all debugging.  Generally speaking, if you have syntax or runtime errors in your javascript, the web browser will report nothing but not attempt to run the script.  So if it appears that your code is doing nothing, then it's likely a syntax error of some sort.  Fortunately, in Firefox, you can use the error console to view warnings and errors produced as javascripts are executed.  Go to Tools-&amp;gt;Error Console to access it.  This is most useful for simple syntatic debugging.  If you need a more serious debugger, you can use [http://getfirebug.com/ Firebug] for Firefox.&lt;br /&gt;
&lt;br /&gt;
=Asynchronous Javascript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that a large fraction of the work happens at the web browser, and larger requests happen behind the scenes, keeping waiting times for data to come back over the network to a minimum.  AJAX is generally also associated with higher-level APIs and libraries that aid in producing cleaner user interfaces for web applications.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=jQuery=&lt;br /&gt;
&lt;br /&gt;
jQuery is a fairly lightweight javascript library that has a number of particularly useful features.  Even thought it's only been around for a few years now, it has gained a lot of momentum in the web development community.  jQuery provides a simple interface to easily modify CSS attributes on all the elements in the HTML document, has simplified AJAX integration, and is accompanied by a number of handy user interface related APIs.  You can get a good overview of jQuery in the jQuery [http://docs.jquery.com/Tutorials:How_jQuery_Works Tutorials], and there is a short summary on [http://en.wikipedia.org/wiki/JQuery Wikipedia].  Of course, there are plenty of other tutorials a google search away.&lt;br /&gt;
&lt;br /&gt;
To use jQuery in your javascript, you need to include the jQuery javascript library, which is just another javascript file.  It's freely available online at the jQuery [http://docs.jquery.com/Downloading_jQuery website].  Assuming you've downloaded the file to your web server, put it in the directory with your PHP and HTML files, and renamed it ''jquery.js'', you include it by adding this line to the head section of your HTML of PHP file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;jquery.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also link to the jQuery library directly on other websites as mentioned in the above tutorials.  The trade-off is that having a local copy will load faster, but then requires you to manage the library files your self (e.g., downloading new versions).&lt;br /&gt;
&lt;br /&gt;
A few code snippets are given below, but the best way to learn about all the feature of jQuery is to head to the [http://jquery.com jQuery website] and look through the tutorials, references, and examples there.&lt;br /&gt;
&lt;br /&gt;
The ''$'' symbol is used to denote jQuery functionality.  You can use it to call jQuery functions, access HTML document elements in order to read or write values, or modify style information via CSS for a range of document elements.  For example, here is some code that sets up a function to be called whenever the user clicks on a link:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $(&amp;quot;a&amp;quot;).click( function(event)&lt;br /&gt;
 {&lt;br /&gt;
   event.preventDefault();&lt;br /&gt;
   $(this).hide(&amp;quot;slow&amp;quot;);&lt;br /&gt;
 } );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code leverages many useful javascript and jQuery features.  ''$(&amp;quot;a&amp;quot;)'' is the jQuery of selecting all HTML ''a'' elements.  That is, in this case, all elements in the HTML document that are ''&amp;lt;a&amp;gt;'' tags.  The ''.click'' function is invoked for all such ''a'' elements in the document.  The argument to the ''click'' function is another function that is to be called when the selected elements are clicked by the user.  The ''event'' argument to this user-defined function is a default argument sent to function which handle user actions like clicking a mouse button.  In this example, the code calls another function on the event passed in ''preventDefault()'' to stop the standard action on that event.  Here that means stopping the web browser from following the link that the user clicked in.  Finally, the ''$(this)'' variable is the jQuery way of, accessing the current encapsulating object, as in Java.  This example calls the jQuery hide function on the object to make the object (again, in this case, ''this'' is referring to the ''&amp;lt;a&amp;gt;'' tag that the user clicked) disappear from the browser.  The script also passes an argument to the hide function to make the link disappear slowly as described in the API [http://docs.jquery.com/Hide documentation].&lt;br /&gt;
&lt;br /&gt;
jQuery also comes with some use to use widgets for building better user interfaces.  One of the handiest is the [http://docs.jquery.com/UI/Dialog dialog] widget that pops-up an in-browser dialog.  To use the dialog functions, you need to include a few more javascript libraries in your HTML header block like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;link type=&amp;quot;text/css&amp;quot; href=&amp;quot;http://jqueryui.com/latest/themes/base/ui.all.css&amp;quot; rel=&amp;quot;stylesheet&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/jquery-1.3.2.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.core.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.draggable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.resizable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.dialog.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
   // your code goes here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can then use the ''$().dialog()'' function to make dialog boxes.  The ''dialog'' function takes a lot of arguments to customize its behavior and add standard elements like buttons; see the documentation for details.  The content of the dialog box is typically placed in a ''div'' container and then that container is hidden when the document loads.  You can put any elements in the ''div' and they will be displayed as normal in the dialog box.  Here is a full example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;lt;html&amp;gt;&lt;br /&gt;
 &amp;lt;head&amp;gt;&lt;br /&gt;
  &amp;lt;style&amp;gt;&lt;br /&gt;
    #dialog { display:none }&lt;br /&gt;
  &amp;lt;/style&amp;gt;&lt;br /&gt;
  &amp;lt;link type=&amp;quot;text/css&amp;quot; href=&amp;quot;http://jqueryui.com/latest/themes/base/ui.all.css&amp;quot; rel=&amp;quot;stylesheet&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/jquery-1.3.2.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.core.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.draggable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.resizable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.dialog.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
    function showdialog()&lt;br /&gt;
    {&lt;br /&gt;
      $(&amp;quot;#dialog&amp;quot;).dialog();&lt;br /&gt;
    }&lt;br /&gt;
  &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
  &amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Show Dialog&amp;quot; onclick=showdialog() /&amp;gt;&lt;br /&gt;
  &amp;lt;div id=&amp;quot;dialog&amp;quot; title=&amp;quot;Howdy&amp;quot;&amp;gt;Look at me!&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To understand the page, start at the bottom.  There is a ''div'' element with a title and some string content.  It also has a unique id (the ids can be anything you want) which is used to identify this particular element in the rest of the document.  Above that is a button input.  The only thing special about it is the ''onclick'' attribute which specifies a javascript function to call when the button is clicked by a user.  In this case, the function is ''showdialog()'' which is a user defined javascript function contained in the document header.  All the function does is access the document element with id ''dialog'' (''$(''#identifier&amp;quot;)'' is the jQuery way of accessing an individual element based on the assigned id in the element attribute list), and build a dialog box from it.  Here, the dialog box will have the title ''Howdy'' from the title attribute in the ''#dialog'' element, and have a text string ''Look at me!''.  Finally, at the top of the document is the ''style'' section, which is a normal HTML/CSS section describing appearance of parts of the document.  There is only one entry, again describing the document element with id ''dialog''.  One style attribute is set, namely the ''display'' attribute to set to ''none'' to indicate that the element should not be displayed.  Otherwise, the ''#dialog'' div would be displayed like any other part of the document rather than being used solely for our dialog box.&lt;br /&gt;
&lt;br /&gt;
Here is another example of using jQuery to do simple animations by moving an image across the browser window:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Other Resources=&lt;br /&gt;
There are a number of other w3schools tutorials and references that you may find useful, including [http://www.w3schools.com/php/default.asp PHP] (note the PHP and AJAX section), [http://www.w3schools.com/htmldom/default.asp DOM], [http://www.w3schools.com/ajax/default.asp AJAX], and [http://www.w3schools.com/css/default.asp CSS].  Many other resources exist around the web, and you should feel free to incorporate any existing libraries or other javascript or AJAX-related tools.  For example, the [http://code.google.com/apis/ajaxlibs/ Google AJAX APIs].&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=89</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=89"/>
		<updated>2009-09-24T05:05:16Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* jQuery */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
Javascript is one of the most widely used scripting languages for preforming client-side functions in the web environment.  That is, the javascript code is interpreted and executed in a client's web browser.. not on the web server.  It is vaguely similar to Java, in that it is an object oriented language, but it was designed to be simpler for non-programmers to be able to accomplish common web processing tasks.&lt;br /&gt;
&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
Since the javascript code runs in the web browser, you'll have to use web browser tools for all debugging.  Generally speaking, if you have syntax or runtime errors in your javascript, the web browser will report nothing but not attempt to run the script.  So if it appears that your code is doing nothing, then it's likely a syntax error of some sort.  Fortunately, in Firefox, you can use the error console to view warnings and errors produced as javascripts are executed.  Go to Tools-&amp;gt;Error Console to access it.  This is most useful for simple syntatic debugging.  If you need a more serious debugger, you can use [http://getfirebug.com/ Firebug] for Firefox.&lt;br /&gt;
&lt;br /&gt;
=Asynchronous Javascript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that a large fraction of the work happens at the web browser, and larger requests happen behind the scenes, keeping waiting times for data to come back over the network to a minimum.  AJAX is generally also associated with higher-level APIs and libraries that aid in producing cleaner user interfaces for web applications.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=jQuery=&lt;br /&gt;
&lt;br /&gt;
jQuery is a fairly lightweight javascript library that has a number of particularly useful features.  Even thought it's only been around for a few years now, it has gained a lot of momentum in the web development community.  jQuery provides a simple interface to easily modify CSS attributes on all the elements in the HTML document, has simplified AJAX integration, and is accompanied by a number of handy user interface related APIs.  You can get a good overview of jQuery in the jQuery [http://docs.jquery.com/Tutorials:How_jQuery_Works Tutorials], and there is a short summary on [http://en.wikipedia.org/wiki/JQuery Wikipedia].  Of course, there are plenty of other tutorials a google search away.&lt;br /&gt;
&lt;br /&gt;
To use jQuery in your javascript, you need to include the jQuery javascript library, which is just another javascript file.  It's freely available online at the jQuery [http://docs.jquery.com/Downloading_jQuery website].  Assuming you've downloaded the file to your web server, put it in the directory with your PHP and HTML files, and renamed it ''jquery.js'', you include it by adding this line to the head section of your HTML of PHP file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;jquery.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also link to the jQuery library directly on other websites as mentioned in the above tutorials.  The trade-off is that having a local copy will load faster, but then requires you to manage the library files your self (e.g., downloading new versions).&lt;br /&gt;
&lt;br /&gt;
A few code snippets are given below, but the best way to learn about all the feature of jQuery is to head to the [http://jquery.com jQuery website] and look through the tutorials, references, and examples there.&lt;br /&gt;
&lt;br /&gt;
The ''$'' symbol is used to denote jQuery functionality.  You can use it to call jQuery functions, access HTML document elements in order to read or write values, or modify style information via CSS for a range of document elements.  For example, here is some code that sets up a function to be called whenever the user clicks on a link:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 $(&amp;quot;a&amp;quot;).click( function(event)&lt;br /&gt;
 {&lt;br /&gt;
   event.preventDefault();&lt;br /&gt;
   $(this).hide(&amp;quot;slow&amp;quot;);&lt;br /&gt;
 } );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code leverages many useful javascript and jQuery features.  ''$(&amp;quot;a&amp;quot;)'' is the jQuery of selecting all HTML ''a'' elements.  That is, in this case, all elements in the HTML document that are ''&amp;lt;a&amp;gt;'' tags.  The ''.click'' function is invoked for all such ''a'' elements in the document.  The argument to the ''click'' function is another function that is to be called when the selected elements are clicked by the user.  The ''event'' argument to this user-defined function is a default argument sent to function which handle user actions like clicking a mouse button.  In this example, the code calls another function on the event passed in ''preventDefault()'' to stop the standard action on that event.  Here that means stopping the web browser from following the link that the user clicked in.  Finally, the ''$(this)'' variable is the jQuery way of, accessing the current encapsulating object, as in Java.  This example calls the jQuery hide function on the object to make the object (again, in this case, ''this'' is referring to the ''&amp;lt;a&amp;gt;'' tag that the user clicked) disappear from the browser.  The script also passes an argument to the hide function to make the link disappear slowly as described in the API [http://docs.jquery.com/Hide documentation].&lt;br /&gt;
&lt;br /&gt;
jQuery also comes with some use to use widgets for building better user interfaces.  One of the handiest is the [http://docs.jquery.com/UI/Dialog dialog] widget that pops-up an in-browser dialog.  To use the dialog functions, you need to include a few more javascript libraries in your HTML header block like so:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;link type=&amp;quot;text/css&amp;quot; href=&amp;quot;http://jqueryui.com/latest/themes/base/ui.all.css&amp;quot; rel=&amp;quot;stylesheet&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/jquery-1.3.2.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.core.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.draggable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.resizable.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://jqueryui.com/latest/ui/ui.dialog.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
   // your code goes here&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example of using jQuery to do simple animations by moving an image across the browser window:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Other Resources=&lt;br /&gt;
There are a number of other w3schools tutorials and references that you may find useful, including [http://www.w3schools.com/php/default.asp PHP] (note the PHP and AJAX section), [http://www.w3schools.com/htmldom/default.asp DOM], [http://www.w3schools.com/ajax/default.asp AJAX], and [http://www.w3schools.com/css/default.asp CSS].  Many other resources exist around the web, and you should feel free to incorporate any existing libraries or other javascript or AJAX-related tools.  For example, the [http://code.google.com/apis/ajaxlibs/ Google AJAX APIs].&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=88</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=88"/>
		<updated>2009-09-24T04:15:33Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
Javascript is one of the most widely used scripting languages for preforming client-side functions in the web environment.  That is, the javascript code is interpreted and executed in a client's web browser.. not on the web server.  It is vaguely similar to Java, in that it is an object oriented language, but it was designed to be simpler for non-programmers to be able to accomplish common web processing tasks.&lt;br /&gt;
&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
Since the javascript code runs in the web browser, you'll have to use web browser tools for all debugging.  Generally speaking, if you have syntax or runtime errors in your javascript, the web browser will report nothing but not attempt to run the script.  So if it appears that your code is doing nothing, then it's likely a syntax error of some sort.  Fortunately, in Firefox, you can use the error console to view warnings and errors produced as javascripts are executed.  Go to Tools-&amp;gt;Error Console to access it.  This is most useful for simple syntatic debugging.  If you need a more serious debugger, you can use [http://getfirebug.com/ Firebug] for Firefox.&lt;br /&gt;
&lt;br /&gt;
=Asynchronous Javascript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that a large fraction of the work happens at the web browser, and larger requests happen behind the scenes, keeping waiting times for data to come back over the network to a minimum.  AJAX is generally also associated with higher-level APIs and libraries that aid in producing cleaner user interfaces for web applications.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=jQuery=&lt;br /&gt;
&lt;br /&gt;
Here is another example of using jQuery to do simple animations by moving an image across the browser window:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Other Resources=&lt;br /&gt;
There are a number of other w3schools tutorials and references that you may find useful, including [http://www.w3schools.com/php/default.asp PHP] (note the PHP and AJAX section), [http://www.w3schools.com/htmldom/default.asp DOM], [http://www.w3schools.com/ajax/default.asp AJAX], and [http://www.w3schools.com/css/default.asp CSS].  Many other resources exist around the web, and you should feel free to incorporate any existing libraries or other javascript or AJAX-related tools.  For example, the [http://code.google.com/apis/ajaxlibs/ Google AJAX APIs].&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=87</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=87"/>
		<updated>2009-09-24T04:09:56Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Asynchronous Javascript And XML */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
Javascript is one of the most widely used scripting languages for preforming client-side functions in the web environment.  That is, the javascript code is interpreted and executed in a client's web browser.. not on the web server.  It is vaguely similar to Java, in that it is an object oriented language, but it was designed to be simpler for non-programmers to be able to accomplish common web processing tasks.&lt;br /&gt;
&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
Since the javascript code runs in the web browser, you'll have to use web browser tools for all debugging.  Generally speaking, if you have syntax or runtime errors in your javascript, the web browser will report nothing but not attempt to run the script.  So if it appears that your code is doing nothing, then it's likely a syntax error of some sort.  Fortunately, in Firefox, you can use the error console to view warnings and errors produced as javascripts are executed.  Go to Tools-&amp;gt;Error Console to access it.  This is most useful for simple syntatic debugging.  If you need a more serious debugger, you can use [http://getfirebug.com/ Firebug] for Firefox.&lt;br /&gt;
&lt;br /&gt;
=Asynchronous Javascript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that a large fraction of the work happens at the web browser, and larger requests happen behind the scenes, keeping waiting times for data to come back over the network to a minimum.  AJAX is generally also associated with higher-level APIs and libraries that aid in producing cleaner user interfaces for web applications.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Other Resources=&lt;br /&gt;
There are a number of other w3schools tutorials and references that you may find useful, including [http://www.w3schools.com/php/default.asp PHP] (note the PHP and AJAX section), [http://www.w3schools.com/htmldom/default.asp DOM], and [http://www.w3schools.com/ajax/default.asp AJAX].  Many other resources exist around the web, and you should feel free to incorporate any existing libraries or other javascript or AJAX-related tools.  For example, [http://en.wikipedia.org/wiki/JQuery jQuery], and [http://code.google.com/apis/ajaxlibs/ Google AJAX APIs].  &lt;br /&gt;
&lt;br /&gt;
Here is one example of using jQuery to do simple animations by moving an image across the browser window:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=86</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=86"/>
		<updated>2009-09-24T04:01:03Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Javascript */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
Javascript is one of the most widely used scripting languages for preforming client-side functions in the web environment.  That is, the javascript code is interpreted and executed in a client's web browser.. not on the web server.  It is vaguely similar to Java, in that it is an object oriented language, but it was designed to be simpler for non-programmers to be able to accomplish common web processing tasks.&lt;br /&gt;
&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
Since the javascript code runs in the web browser, you'll have to use web browser tools for all debugging.  Generally speaking, if you have syntax or runtime errors in your javascript, the web browser will report nothing but not attempt to run the script.  So if it appears that your code is doing nothing, then it's likely a syntax error of some sort.  Fortunately, in Firefox, you can use the error console to view warnings and errors produced as javascripts are executed.  Go to Tools-&amp;gt;Error Console to access it.  This is most useful for simple syntatic debugging.  If you need a more serious debugger, you can use [http://getfirebug.com/ Firebug] for Firefox.&lt;br /&gt;
&lt;br /&gt;
=Asynchronous Javascript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that most of the work happens at the web browser, keeping waiting times for data to come back over the network to a minimum.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Other Resources=&lt;br /&gt;
There are a number of other w3schools tutorials and references that you may find useful, including [http://www.w3schools.com/php/default.asp PHP] (note the PHP and AJAX section), [http://www.w3schools.com/htmldom/default.asp DOM], and [http://www.w3schools.com/ajax/default.asp AJAX].  Many other resources exist around the web, and you should feel free to incorporate any existing libraries or other javascript or AJAX-related tools.  For example, [http://en.wikipedia.org/wiki/JQuery jQuery], and [http://code.google.com/apis/ajaxlibs/ Google AJAX APIs].  &lt;br /&gt;
&lt;br /&gt;
Here is one example of using jQuery to do simple animations by moving an image across the browser window:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=85</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=85"/>
		<updated>2009-09-20T01:52:52Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Moving a target with Jquery */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
=Asynchronous Javascript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that most of the work happens at the web browser, keeping waiting times for data to come back over the network to a minimum.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Other Resources=&lt;br /&gt;
There are a number of other w3schools tutorials and references that you may find useful, including [http://www.w3schools.com/php/default.asp PHP] (note the PHP and AJAX section), [http://www.w3schools.com/htmldom/default.asp DOM], and [http://www.w3schools.com/ajax/default.asp AJAX].  Many other resources exist around the web, and you should feel free to incorporate any existing libraries or other javascript or AJAX-related tools.  For example, [http://en.wikipedia.org/wiki/JQuery jQuery], and [http://code.google.com/apis/ajaxlibs/ Google AJAX APIs].  &lt;br /&gt;
&lt;br /&gt;
Here is one example of using jQuery to do simple animations by moving an image across the browser window:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=84</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=84"/>
		<updated>2009-09-20T01:47:08Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Asynchronous JavaScript And XML */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
=Asynchronous Javascript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that most of the work happens at the web browser, keeping waiting times for data to come back over the network to a minimum.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Moving a target with Jquery=&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=83</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=83"/>
		<updated>2009-09-19T22:06:51Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Sending Parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
=Asynchronous JavaScript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that most of the work happens at the web browser, keeping waiting times for data to come back over the network to a minimum.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply-post.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Moving a target with Jquery=&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=82</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=82"/>
		<updated>2009-09-19T21:40:52Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Moving a target with Jquery */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
=Asynchronous JavaScript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that most of the work happens at the web browser, keeping waiting times for data to come back over the network to a minimum.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Moving a target with Jquery=&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
&amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
  // these are for storing target locations&lt;br /&gt;
  var targetLeft=0;&lt;br /&gt;
  var targetTop=0;&lt;br /&gt;
  function moveTarget()&lt;br /&gt;
  {&lt;br /&gt;
    targetLeft+=10; //increase target location&lt;br /&gt;
    targetTop+=10; &lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
    $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
    if(targetLeft&amp;amp;lt;200) &lt;br /&gt;
    { //stop if we reached left=200&lt;br /&gt;
      window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready( function() &lt;br /&gt;
  { //at the beginning create a clock timeout&lt;br /&gt;
    window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
  });&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
&amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
&amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=81</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=81"/>
		<updated>2009-09-19T21:39:09Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Sending Parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
=Asynchronous JavaScript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that most of the work happens at the web browser, keeping waiting times for data to come back over the network to a minimum.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]; ?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Moving a target with Jquery=&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
 // these are for storing target locations&lt;br /&gt;
 var targetLeft=0;&lt;br /&gt;
 var targetTop=0;&lt;br /&gt;
 function moveTarget(){&lt;br /&gt;
     targetLeft+=10; //increase target location&lt;br /&gt;
     targetTop+=10; &lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
     if(targetLeft&amp;amp;lt;200) { //stop if we reached left=200&lt;br /&gt;
       window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready(function() { //at the beginning create a clock timeout&lt;br /&gt;
   window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
      &lt;br /&gt;
 });&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
 &amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
   &amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 &amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=80</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=80"/>
		<updated>2009-09-19T21:38:48Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Sending Parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
=Asynchronous JavaScript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that most of the work happens at the web browser, keeping waiting times for data to come back over the network to a minimum.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are sent either in the url (to, for example, a PHP page) or through the ''send'' function.&lt;br /&gt;
Consider following PHP code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply ()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the x and y values are sent as part of the url as GET parameters. You can also send them as part of the ''send'' function, provided that your receiving page uses POST parameters.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply()&lt;br /&gt;
{&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Moving a target with Jquery=&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
 // these are for storing target locations&lt;br /&gt;
 var targetLeft=0;&lt;br /&gt;
 var targetTop=0;&lt;br /&gt;
 function moveTarget(){&lt;br /&gt;
     targetLeft+=10; //increase target location&lt;br /&gt;
     targetTop+=10; &lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
     if(targetLeft&amp;amp;lt;200) { //stop if we reached left=200&lt;br /&gt;
       window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready(function() { //at the beginning create a clock timeout&lt;br /&gt;
   window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
      &lt;br /&gt;
 });&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
 &amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
   &amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 &amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=79</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=79"/>
		<updated>2009-09-19T21:32:05Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* XMLHttpRequest Object */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
=Asynchronous JavaScript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that most of the work happens at the web browser, keeping waiting times for data to come back over the network to a minimum.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function()&lt;br /&gt;
 {&lt;br /&gt;
   if(xmlHttp.readyState==4)&lt;br /&gt;
   {&lt;br /&gt;
     .... do some cool stuff ...&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function()&lt;br /&gt;
  {&lt;br /&gt;
    if(xmlHttp.readyState==4)&lt;br /&gt;
    {&lt;br /&gt;
      var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
      pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are send either in the url (to, for example, a PHP page) or through ''send'' function.&lt;br /&gt;
Consider following PHP  code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]?&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply (){&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function() {&lt;br /&gt;
  if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that x and y values are sent in url. You can also send them in ''send'' function, provided that your receiving page asumes they are form posts.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]?&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply (){&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
 var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function() {&lt;br /&gt;
  if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Moving a target with Jquery=&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
 // these are for storing target locations&lt;br /&gt;
 var targetLeft=0;&lt;br /&gt;
 var targetTop=0;&lt;br /&gt;
 function moveTarget(){&lt;br /&gt;
     targetLeft+=10; //increase target location&lt;br /&gt;
     targetTop+=10; &lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
     if(targetLeft&amp;amp;lt;200) { //stop if we reached left=200&lt;br /&gt;
       window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready(function() { //at the beginning create a clock timeout&lt;br /&gt;
   window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
      &lt;br /&gt;
 });&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
 &amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
   &amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 &amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=78</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=78"/>
		<updated>2009-09-19T21:29:00Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* XMLHttpRequest Object */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
=Asynchronous JavaScript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that most of the work happens at the web browser, keeping waiting times for data to come back over the network to a minimum.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object handles communication with the web servers.  To use it, you must first initialize a variable to be an instance of the ''XMLHttpRequest'' class.&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an ''XMLHttpRequest'', you can use it to send requests to a server. Initially, you need to connect to the server side page you are interested in:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;, ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, REMOTE_URL is the page that you are going to send requests to, and ASYNCRONOUS_FLAG is true or false to indicate whether the calls should be made asynchronously or synchronously. In asynchronous communication, the client can do other things while waiting for the server response, whereas in synchronous communication, the client blocks execution and waits until the response comes back before proceeding.  Once the object has been opened, you can then use the ''send'' method to send a request to the server. Note that you do not explicitly set data fields to send, because it is contained in the URL request.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
The ''XMLHttpRequest'' object has an attribute, ''readyState'', that stores the current state of the request.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''2''.  Every time the state changes, javascript automatically calls a function specified by the ''onreadystatechange'' attribute.  So, before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function() {&lt;br /&gt;
    alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is how asynchronous requests are handled. Once you send a request, you access the response only through the ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to the request being completed. The server's response is returned as part of the ''XMLHttpRequest' ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function() {&lt;br /&gt;
    if(xmlHttp.readyState==4){&lt;br /&gt;
    .... do some cool stuff ...&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, if you have following html file, server.html:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints the hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
   xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
   xmlHttp.onreadystatechange=function() {&lt;br /&gt;
    if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are send either in the url (to, for example, a PHP page) or through ''send'' function.&lt;br /&gt;
Consider following PHP  code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]?&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply (){&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function() {&lt;br /&gt;
  if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that x and y values are sent in url. You can also send them in ''send'' function, provided that your receiving page asumes they are form posts.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]?&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply (){&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
 var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function() {&lt;br /&gt;
  if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Moving a target with Jquery=&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
 // these are for storing target locations&lt;br /&gt;
 var targetLeft=0;&lt;br /&gt;
 var targetTop=0;&lt;br /&gt;
 function moveTarget(){&lt;br /&gt;
     targetLeft+=10; //increase target location&lt;br /&gt;
     targetTop+=10; &lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
     if(targetLeft&amp;amp;lt;200) { //stop if we reached left=200&lt;br /&gt;
       window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready(function() { //at the beginning create a clock timeout&lt;br /&gt;
   window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
      &lt;br /&gt;
 });&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
 &amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
   &amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 &amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=77</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=77"/>
		<updated>2009-09-19T20:27:14Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Asynchronous JavaScript And XML */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
=Asynchronous JavaScript And XML=&lt;br /&gt;
AJAX, Asynchronous Javascript And XML, is a group of related web development techniques, standards, and technologies used on the client (web browser) side of the standard web server-client communication model.  Although other languages can be used, the most common is to use javascript to send asynchronous requests to the web server in order to retrieve XML data that the javascript then uses to produce dynamic, fast, user-friendly web pages.  The key idea is that most of the work happens at the web browser, keeping waiting times for data to come back over the network to a minimum.&lt;br /&gt;
&lt;br /&gt;
The javascript ''XMLHttpRequest'' object is used to exchange data with the web servers without needing to reload the entire web page in the browser.  This ''XMLHttpRequest'' object is now supported by all major browsers, although some browsers implement support in slightly different ways, leading to different behavior of the same code on different platforms.  For this course, you should focus on supporting Firefox, but in any real production website it's very important to do validation testing using all of the major browsers including Firefox, Internet Explorer, Safari, and Opera as a minimum.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
''XMLHttpRequest'' provides the communication with the remote servers. Hence, the first thing you need is to initialize it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an XMLHttpRequest, you can use it to send data to a server. Initially, you need to connect your server side page:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;,ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, ''REMOTE_URL'' is the page that you want to communicate and ''ASYNCRONOUS_FLAG'' is whether the call is going to asyncronous. In asyncronous communication, you can let your client do other things while waiting for server response. Once you have the object initialized, you can then use ''send'' method to send your request. Note that, there is not explicit data setting. That is because you send your data withing the url.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
XMLHttpRequest object has an attribute ''readyState'' that stores the state of the current status.&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''3''.  Every time its state change,the object calls a function specified by the attribute  ''onreadystatechange''. So before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function() {&lt;br /&gt;
    alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is where the asyncronousity comes. Once you send a request, you access the response only ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to ''completed request''. The server response  is returned at XMLHttpRequest's ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function() {&lt;br /&gt;
    if(xmlHttp.readyState==4){&lt;br /&gt;
    .... do some cool stuff ...&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, lets assume you have following html file&lt;br /&gt;
server.html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
   xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
   xmlHttp.onreadystatechange=function() {&lt;br /&gt;
    if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are send either in the url (to, for example, a PHP page) or through ''send'' function.&lt;br /&gt;
Consider following PHP  code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]?&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply (){&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function() {&lt;br /&gt;
  if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that x and y values are sent in url. You can also send them in ''send'' function, provided that your receiving page asumes they are form posts.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]?&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply (){&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
 var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function() {&lt;br /&gt;
  if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Moving a target with Jquery=&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
 // these are for storing target locations&lt;br /&gt;
 var targetLeft=0;&lt;br /&gt;
 var targetTop=0;&lt;br /&gt;
 function moveTarget(){&lt;br /&gt;
     targetLeft+=10; //increase target location&lt;br /&gt;
     targetTop+=10; &lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
     if(targetLeft&amp;amp;lt;200) { //stop if we reached left=200&lt;br /&gt;
       window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready(function() { //at the beginning create a clock timeout&lt;br /&gt;
   window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
      &lt;br /&gt;
 });&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
 &amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
   &amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 &amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=76</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=76"/>
		<updated>2009-09-19T20:17:32Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Teams for this module */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
=Asynchronous JavaScript And XML=&lt;br /&gt;
AJAX is a way to use existing standards. It provides a framework for javascript to get the data from external servers. Javascript object ''XMLHttpRequest''  is used exchange data with other servers without reloading the page. This provides less data transmission between the servers and the clients and makes Internet applications smaller, faster and more user-friendly.&lt;br /&gt;
&lt;br /&gt;
XMLHttpRequest is supported by all modern browsers however there are slight changes (in case of IE, large changes) so for the rest of this module, we will assume Firefox as our browser.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
''XMLHttpRequest'' provides the communication with the remote servers. Hence, the first thing you need is to initialize it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an XMLHttpRequest, you can use it to send data to a server. Initially, you need to connect your server side page:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;,ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, ''REMOTE_URL'' is the page that you want to communicate and ''ASYNCRONOUS_FLAG'' is whether the call is going to asyncronous. In asyncronous communication, you can let your client do other things while waiting for server response. Once you have the object initialized, you can then use ''send'' method to send your request. Note that, there is not explicit data setting. That is because you send your data withing the url.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
XMLHttpRequest object has an attribute ''readyState'' that stores the state of the current status.&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''3''.  Every time its state change,the object calls a function specified by the attribute  ''onreadystatechange''. So before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function() {&lt;br /&gt;
    alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is where the asyncronousity comes. Once you send a request, you access the response only ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to ''completed request''. The server response  is returned at XMLHttpRequest's ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function() {&lt;br /&gt;
    if(xmlHttp.readyState==4){&lt;br /&gt;
    .... do some cool stuff ...&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, lets assume you have following html file&lt;br /&gt;
server.html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
   xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
   xmlHttp.onreadystatechange=function() {&lt;br /&gt;
    if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are send either in the url (to, for example, a PHP page) or through ''send'' function.&lt;br /&gt;
Consider following PHP  code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]?&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply (){&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function() {&lt;br /&gt;
  if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that x and y values are sent in url. You can also send them in ''send'' function, provided that your receiving page asumes they are form posts.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]?&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply (){&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
 var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function() {&lt;br /&gt;
  if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Moving a target with Jquery=&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
 // these are for storing target locations&lt;br /&gt;
 var targetLeft=0;&lt;br /&gt;
 var targetTop=0;&lt;br /&gt;
 function moveTarget(){&lt;br /&gt;
     targetLeft+=10; //increase target location&lt;br /&gt;
     targetTop+=10; &lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
     if(targetLeft&amp;amp;lt;200) { //stop if we reached left=200&lt;br /&gt;
       window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready(function() { //at the beginning create a clock timeout&lt;br /&gt;
   window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
      &lt;br /&gt;
 });&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
 &amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
   &amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 &amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=75</id>
		<title>JavaScript</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=JavaScript&amp;diff=75"/>
		<updated>2009-09-19T20:14:32Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Javascript */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Javascript =&lt;br /&gt;
You should start by reading the w3schools javascript tutorial, which covers most of the important features of the language:&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
=Asynchronous JavaScript And XML=&lt;br /&gt;
AJAX is a way to use existing standards. It provides a framework for javascript to get the data from external servers. Javascript object ''XMLHttpRequest''  is used exchange data with other servers without reloading the page. This provides less data transmission between the servers and the clients and makes Internet applications smaller, faster and more user-friendly.&lt;br /&gt;
&lt;br /&gt;
XMLHttpRequest is supported by all modern browsers however there are slight changes (in case of IE, large changes) so for the rest of this module, we will assume Firefox as our browser.&lt;br /&gt;
&lt;br /&gt;
==  XMLHttpRequest Object ==&lt;br /&gt;
&lt;br /&gt;
''XMLHttpRequest'' provides the communication with the remote servers. Hence, the first thing you need is to initialize it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
Once you have an XMLHttpRequest, you can use it to send data to a server. Initially, you need to connect your server side page:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.open(&amp;quot;METHOD&amp;quot;,&amp;quot;REMOTE_URL&amp;quot;,ASYNCRONOUS_FLAG);&lt;br /&gt;
&lt;br /&gt;
where METHOD is &amp;quot;GET&amp;quot; or &amp;quot;POST&amp;quot;, ''REMOTE_URL'' is the page that you want to communicate and ''ASYNCRONOUS_FLAG'' is whether the call is going to asyncronous. In asyncronous communication, you can let your client do other things while waiting for server response. Once you have the object initialized, you can then use ''send'' method to send your request. Note that, there is not explicit data setting. That is because you send your data withing the url.&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.send(null);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
XMLHttpRequest object has an attribute ''readyState'' that stores the state of the current status.&lt;br /&gt;
&amp;lt;table class=&amp;quot;ex&amp;quot; border=&amp;quot;1&amp;quot; width=&amp;quot;50%&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; id=&amp;quot;table7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot; width=&amp;quot;10%&amp;quot;&amp;gt;State&amp;lt;/th&amp;gt;&amp;lt;th align=&amp;quot;left&amp;quot;&amp;gt;Description&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;0&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is not initialized&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been set up&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request has been sent&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;3&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is in process&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;4&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The request is complete&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, when a request is sent, the state is set to ''3''.  Every time its state change,the object calls a function specified by the attribute  ''onreadystatechange''. So before calling a remote site, you should setup a new function:&lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function() {&lt;br /&gt;
    alert('state changed to '+xmlHttp.readyState);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This is where the asyncronousity comes. Once you send a request, you access the response only ''onreadystatechange'' function. Meanwhile, your client page can be doing other things. If you are waiting for a response from the server, you should check whether the ''readyState'' is ''4'', corresponding to ''completed request''. The server response  is returned at XMLHttpRequest's ''responseText'' attribute. &lt;br /&gt;
&lt;br /&gt;
 xmlHttp.onreadystatechange=function() {&lt;br /&gt;
    if(xmlHttp.readyState==4){&lt;br /&gt;
    .... do some cool stuff ...&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
For example, lets assume you have following html file&lt;br /&gt;
server.html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
hello world&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This server page just prints hello world message.&lt;br /&gt;
&lt;br /&gt;
Here is an AJAX implementation that gets the message from the server and prints it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
&lt;br /&gt;
   xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;server.html&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
   xmlHttp.onreadystatechange=function() {&lt;br /&gt;
    if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
   xmlHttp.send(null);&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
==Sending Parameters==&lt;br /&gt;
The parameters are send either in the url (to, for example, a PHP page) or through ''send'' function.&lt;br /&gt;
Consider following PHP  code that takes two parameters, multiplies them and return the result.&lt;br /&gt;
&lt;br /&gt;
multiply.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_GET[&amp;quot;x&amp;quot;]*$_GET[&amp;quot;y&amp;quot;]?&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use AJAX to get the multiplication of two numbers:&lt;br /&gt;
&lt;br /&gt;
multiply-ajax.php&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply (){&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
  xmlHttp.open(&amp;quot;GET&amp;quot;,&amp;quot;multiply.php?x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function() {&lt;br /&gt;
  if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
  xmlHttp.send(null);&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that x and y values are sent in url. You can also send them in ''send'' function, provided that your receiving page asumes they are form posts.&lt;br /&gt;
&lt;br /&gt;
multiply-post.php&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;? echo $_POST[&amp;quot;x&amp;quot;]*$_POST[&amp;quot;y&amp;quot;]?&amp;gt;&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 multiply-ajax.php&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;lt;html&amp;gt;&lt;br /&gt;
&amp;amp;lt;body&amp;gt;&lt;br /&gt;
&amp;amp;lt;script&amp;gt;&lt;br /&gt;
function multiply (){&lt;br /&gt;
  var xmlHttp;&lt;br /&gt;
  xmlHttp=new XMLHttpRequest();&lt;br /&gt;
  var x,y;&lt;br /&gt;
  x=document.getElementById(&amp;quot;input_x&amp;quot;).value;&lt;br /&gt;
  y=document.getElementById(&amp;quot;input_y&amp;quot;).value;&lt;br /&gt;
 var req=&amp;quot;x=&amp;quot;+x+&amp;quot;&amp;amp;y=&amp;quot;+y;&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.open(&amp;quot;POST&amp;quot;,&amp;quot;multiply.php&amp;quot;,true);&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.onreadystatechange=function() {&lt;br /&gt;
  if(xmlHttp.readyState==4){&lt;br /&gt;
    var pElement=document.getElementById(&amp;quot;msg&amp;quot;);&lt;br /&gt;
     pElement.innerHTML=xmlHttp.responseText;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Content-length&amp;quot;, req.length);&lt;br /&gt;
  xmlHttp.setRequestHeader(&amp;quot;Connection&amp;quot;, &amp;quot;close&amp;quot;);&lt;br /&gt;
  xmlHttp.send(req);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
x:&amp;amp;lt;input id=&amp;quot;input_x&amp;quot; type=&amp;quot;text&amp;quot;/ &amp;gt;&amp;amp;lt;br&amp;gt;&lt;br /&gt;
y:&amp;amp;lt;input id=&amp;quot;input_y&amp;quot; type=&amp;quot;text&amp;quot;/&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;
&amp;amp;lt;a href=&amp;quot;javascript:multiply();&amp;quot;&amp;gt;Multiply&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;amp;lt;br&amp;gt;&lt;br /&gt;
------------------------&amp;amp;lt;/br&amp;gt;&lt;br /&gt;
Result: &lt;br /&gt;
&amp;amp;lt;p id=&amp;quot;msg&amp;quot;&amp;gt;&amp;amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Moving a target with Jquery=&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &amp;amp;lt;html&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;head&amp;gt;                                                                  &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;          &lt;br /&gt;
 &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;                                         &lt;br /&gt;
 // these are for storing target locations&lt;br /&gt;
 var targetLeft=0;&lt;br /&gt;
 var targetTop=0;&lt;br /&gt;
 function moveTarget(){&lt;br /&gt;
     targetLeft+=10; //increase target location&lt;br /&gt;
     targetTop+=10; &lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;top&amp;quot;,targetTop); //move target to (top,left)&lt;br /&gt;
     $(&amp;quot;#target&amp;quot;).css(&amp;quot;left&amp;quot;,targetLeft); &lt;br /&gt;
     if(targetLeft&amp;amp;lt;200) { //stop if we reached left=200&lt;br /&gt;
       window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //set the next timer&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
  $(document).ready(function() { //at the beginning create a clock timeout&lt;br /&gt;
   window.setTimeout(&amp;quot;moveTarget()&amp;quot;,200); //call this function in 200 ms.&lt;br /&gt;
      &lt;br /&gt;
 });&lt;br /&gt;
&lt;br /&gt;
 &amp;amp;lt;/script&amp;gt;                                                               &lt;br /&gt;
 &amp;amp;lt;/head&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;body&amp;gt;                                                                  &lt;br /&gt;
   &amp;amp;lt;!-- this is my target--&amp;gt;                                        &lt;br /&gt;
&amp;amp;lt;img id=&amp;quot;target&amp;quot;  src=&amp;quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/180px-Smiley.svg.png&amp;quot; style=&amp;quot;position:relative&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 &amp;amp;lt;/body&amp;gt;                                                                 &lt;br /&gt;
 &amp;amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Teams for this module=&lt;br /&gt;
Team 0: Gail Crystal Burks , Young Kook Park Please use zoo.cse.wustl.edu&lt;br /&gt;
&lt;br /&gt;
Team 1: William Cannon Fargo , Andrew Tateh Shaw Please use zoo.cse.wustl.edu&lt;br /&gt;
&lt;br /&gt;
Team 2: Jacqueline Rose Steege , Andrew Nemec Bort  Please use zoo.cse.wustl.edu&lt;br /&gt;
&lt;br /&gt;
Team 3: Jonathan Kirst, Adam Michael Basloe Please use zoo.cse.wustl.edu&lt;br /&gt;
&lt;br /&gt;
Team 4: John Thomas Pizzini ,  Michael Browning Please use oz.cse.wustl.edu&lt;br /&gt;
&lt;br /&gt;
Team 5: Paul Manfred Heider, Michael Frances Fahey Please use oz.cse.wustl.edu&lt;br /&gt;
&lt;br /&gt;
Team 6: Mark Evan Davis , Philip Jon Melzer Please use oz.cse.wustl.edu&lt;br /&gt;
&lt;br /&gt;
Team 7: Andrew David Kanyer , Natalie Nikolayevna Sklobovskaya Please use oz.cse.wustl.edu&lt;br /&gt;
&lt;br /&gt;
Team 8: Jonathan Matthew Wald , Vanetia Nikole Cannon , Benjamin Reiter  Please use wad.cse.wustl.edu&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=CSE_330_Online_Textbook_-_Table_of_Contents&amp;diff=74</id>
		<title>CSE 330 Online Textbook - Table of Contents</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=CSE_330_Online_Textbook_-_Table_of_Contents&amp;diff=74"/>
		<updated>2009-09-18T23:40:03Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Module 1: [[System Administration]]&lt;br /&gt;
&lt;br /&gt;
Module 2: [[PHP]]&lt;br /&gt;
&lt;br /&gt;
Module 3: [[Databases and MySQL]]&lt;br /&gt;
&lt;br /&gt;
Module 4: Javascript and AJAX&lt;br /&gt;
&lt;br /&gt;
Module 5: Creative Programming 1&lt;br /&gt;
&lt;br /&gt;
Module 6: Perl&lt;br /&gt;
&lt;br /&gt;
Module 7: Remote Procedure Calls&lt;br /&gt;
&lt;br /&gt;
Module 8: Creative Programming 2&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Other Information:&lt;br /&gt;
&lt;br /&gt;
Project Ideas&lt;br /&gt;
&lt;br /&gt;
Firefox Extensions&lt;br /&gt;
&lt;br /&gt;
Content Management Systems&lt;br /&gt;
&lt;br /&gt;
Graphics Tools&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=73</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=73"/>
		<updated>2009-09-18T14:14:54Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Other MySQL Information */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 apt-get install mysql-server &lt;br /&gt;
&lt;br /&gt;
When the server is being installed, it will ask you for a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. You can install it with:&lt;br /&gt;
&lt;br /&gt;
 apt-get install phpmyadmin&lt;br /&gt;
&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then selecte ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. The format of ''grant'' is&lt;br /&gt;
&lt;br /&gt;
 grant PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with apt-get.  If it's already on your system, apt-get will simply not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
You can close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The PHP manual has fairly detailed descriptions of each mysql related function, so always look there if you're not sure how to do something in PHP: [http://us2.php.net/manual/en/ref.mysql.php].&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
The configuration file for mysql is  ''/etc/mysql/my.cnf''. It specifies mysql directories and other parameters.  For example, if you want to migrate all the data from one mysql server to another, it is sufficient to copy files from ''datadir'', which defaults to ''/var/lib/mysql'' from ''/etc/mysql/my.cnf'' on Ubuntu. Another interesting parameter is ''bind-address''. By default, it restricts the access to mysql to the localhost, so you will probably want to comment it out, thereby allowing access from any host.&lt;br /&gt;
&lt;br /&gt;
''mysqladmin'' is the official command line program to communicate with the server for management tasks related to the server.  Common tasks that can be handled by ''mysqladmin'' include flushing some mysql elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client.  In order to use this command, you need to provide the root password selected when the mysql server was installed.&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=72</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=72"/>
		<updated>2009-09-18T14:11:02Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* PHP and MySQL */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 apt-get install mysql-server &lt;br /&gt;
&lt;br /&gt;
When the server is being installed, it will ask you for a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. You can install it with:&lt;br /&gt;
&lt;br /&gt;
 apt-get install phpmyadmin&lt;br /&gt;
&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then selecte ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. The format of ''grant'' is&lt;br /&gt;
&lt;br /&gt;
 grant PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with apt-get.  If it's already on your system, apt-get will simply not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
You can close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The PHP manual has fairly detailed descriptions of each mysql related function, so always look there if you're not sure how to do something in PHP: [http://us2.php.net/manual/en/ref.mysql.php].&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
&lt;br /&gt;
The configuration file for MySQL is  ''/etc/mysql/my.cnf''. It specifies MySQL directories and other parameters. If you want to migrate MySQL, it is sufficient to copy files from ''datadir'', which is in debian located at ''/var/lib/mysql''. One interesting parameter is bind-address. By default, it restricts the database access to the localhost, so you may want to comment it out.&lt;br /&gt;
&lt;br /&gt;
The program ''mysqladmin'' is the official program to communicate with the server for managerial tasks. Common tasks that can be handled by ''mysqladmin'' include flushing some MySQL elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client. In order to use this command, you need to provide the administrator password.&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=71</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=71"/>
		<updated>2009-09-18T14:10:30Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* PHP and MySQL */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 apt-get install mysql-server &lt;br /&gt;
&lt;br /&gt;
When the server is being installed, it will ask you for a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. You can install it with:&lt;br /&gt;
&lt;br /&gt;
 apt-get install phpmyadmin&lt;br /&gt;
&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then selecte ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. The format of ''grant'' is&lt;br /&gt;
&lt;br /&gt;
 grant PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with apt-get.  If it's already on your system, apt-get will simply not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
You can close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The PHP manual has fairly detailed descriptions of each mysql related function, so always look there if you're not sure how to do something in PHP: [http://us2.php.net/manual/en/ref.mysql.php].&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
&lt;br /&gt;
The configuration file for MySQL is  ''/etc/mysql/my.cnf''. It specifies MySQL directories and other parameters. If you want to migrate MySQL, it is sufficient to copy files from ''datadir'', which is in debian located at ''/var/lib/mysql''. One interesting parameter is bind-address. By default, it restricts the database access to the localhost, so you may want to comment it out.&lt;br /&gt;
&lt;br /&gt;
The program ''mysqladmin'' is the official program to communicate with the server for managerial tasks. Common tasks that can be handled by ''mysqladmin'' include flushing some MySQL elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client. In order to use this command, you need to provide the administrator password.&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=70</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=70"/>
		<updated>2009-09-18T14:09:56Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* PHP and MySQL */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 apt-get install mysql-server &lt;br /&gt;
&lt;br /&gt;
When the server is being installed, it will ask you for a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. You can install it with:&lt;br /&gt;
&lt;br /&gt;
 apt-get install phpmyadmin&lt;br /&gt;
&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then selecte ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. The format of ''grant'' is&lt;br /&gt;
&lt;br /&gt;
 grant PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to have the the ''php5-mysql'' package installed.  This is most likely already installed, but it's ok to try to install it again like normal with apt-get.  If it's already on your system, apt-get will simply not do anything.  Once installed, you can use PHP to connect to a mysql database by using the ''mysql_connect'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 $sqlserver=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The HOSTNAME is either an IP address or DNS name of the machine running the mysql server, the DATABASEUSER is the name of a user who can run the required commands, and the PASSWORD is that user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $sqlserver will be NULL. Once connected, you can select a database with the ''mysql_select_db'' PHP function:&lt;br /&gt;
&lt;br /&gt;
 mysql_select_db('DATABASENAME',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
This would use the connection opened earlier ($sqlserver) to access the DATABASENAME database.&lt;br /&gt;
&lt;br /&gt;
You can close a connection with ''mysql_close'':&lt;br /&gt;
&lt;br /&gt;
 mysql_close($sqlserver);&lt;br /&gt;
&lt;br /&gt;
SQL commands are sent to the database through the ''mysql_query'' command:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY, $sqlserver) &lt;br /&gt;
&lt;br /&gt;
SQLQUERY is any valid SQL statement, including select, delete, update, and insert statements.  Here is an example:&lt;br /&gt;
&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','Command','Commander','2006-02-25)&amp;quot;, $sqlserver) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
This example inserts a new row into the employee table, or causes the PHP script to stop processing with ''die'' is an error occurs.  ''mysql_query'' also receives data from the server. For example, if you run a ''select'' query, then you can loop over the results for each returned row:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
  $result = mysql_query('select * from employee',$sqlserver);&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name: {$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department: {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title: {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the select query is sent, individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the values can be reached by the name of the column from which the data came. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH. If you are interested in the total number of rows returned, you can call the ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
The PHP manual has fairly detailed descriptions of each mysql related function, so always look there if you're not sure how to do something in PHP: [http://us2.php.net/manual/en/ref.mysql.php].&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
&lt;br /&gt;
The configuration file for MySQL is  ''/etc/mysql/my.cnf''. It specifies MySQL directories and other parameters. If you want to migrate MySQL, it is sufficient to copy files from ''datadir'', which is in debian located at ''/var/lib/mysql''. One interesting parameter is bind-address. By default, it restricts the database access to the localhost, so you may want to comment it out.&lt;br /&gt;
&lt;br /&gt;
The program ''mysqladmin'' is the official program to communicate with the server for managerial tasks. Common tasks that can be handled by ''mysqladmin'' include flushing some MySQL elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client. In order to use this command, you need to provide the administrator password.&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=69</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=69"/>
		<updated>2009-09-18T13:48:58Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* SQL Commands */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 apt-get install mysql-server &lt;br /&gt;
&lt;br /&gt;
When the server is being installed, it will ask you for a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. You can install it with:&lt;br /&gt;
&lt;br /&gt;
 apt-get install phpmyadmin&lt;br /&gt;
&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then selecte ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. The format of ''grant'' is&lt;br /&gt;
&lt;br /&gt;
 grant PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Sales&amp;quot;,&amp;quot;Lost traveler&amp;quot;,&amp;quot;2000-03-09&amp;quot;,4);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Management&amp;quot;,&amp;quot;Leader&amp;quot;,&amp;quot;2004-06-13&amp;quot;,59);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Accounting&amp;quot;,&amp;quot;Ring bearer&amp;quot;,&amp;quot;2002-01-01&amp;quot;,17);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,jobtitle) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
The ''select'' command returns rows from a table, optionally based on some conditions:&lt;br /&gt;
&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
In this case, you specify which columns to retrieve with the ATTRIBUTE fields, from table TABLE.  You can also specify the ''*'' attribute that is used to return all columns in the table.  For example:&lt;br /&gt;
&lt;br /&gt;
 select * from employee;&lt;br /&gt;
&lt;br /&gt;
This might return:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| name  | dept       | jobtitle      | joined     | id |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
| Alice | Wonderland | Lost traveler | 2000-03-09 | 4  |&lt;br /&gt;
| Peter | Neverland  | Leader        | 2004-06-13 | 59 |&lt;br /&gt;
| Frodo | Shire      | Ring bearer   | 2002-01-01 | 17 |&lt;br /&gt;
| Fry   |            | Delivery Boy  |            |    |&lt;br /&gt;
+-------+------------+---------------+------------+----+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is another example to only get get Peter's job title:&lt;br /&gt;
&lt;br /&gt;
 select jobtitle from employee where name='Peter';&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
The ''update'' command is used to modify existing data in a table. The format is similar to ''select'':&lt;br /&gt;
&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
For example, this will update Frodo's department:&lt;br /&gt;
&lt;br /&gt;
 update employee set jobtitle='Hero' where name='Frodo';&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
The ''delete from'' command is used to delete rows from a table.  Again, the format is similar to the previous commands:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Fry from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name='Alice';&lt;br /&gt;
&lt;br /&gt;
You can also use wild cards in the CONDITIONS for any SQL command.  For example, this command would delete both Fry and Frodo from the employee table:&lt;br /&gt;
&lt;br /&gt;
 delete from employee where name like 'Fr%';&lt;br /&gt;
&lt;br /&gt;
In this case the CONDITION change from ''name='' to ''name like'' and the comparator uses the SQL wild card (%) to match any number of characters.  The above command, then, deletes all row where the name column starts with ''Fr''&lt;br /&gt;
&lt;br /&gt;
You can also delete all rows from a table with:&lt;br /&gt;
&lt;br /&gt;
 delete from employee;&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to install ''php5-mysql'' package first. Then from a PHP page, you can connect to a mysql database by using ''mysql_connect'' command.&lt;br /&gt;
&lt;br /&gt;
 $link=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The hostname is either IP or name of the server running MySQL daemon, the database user is the name of the user who can run the required commands and password is the user's the user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $link will be NULL. Once connected, you can select a database:&lt;br /&gt;
 mysql_select_db('DATABASENAME',[$link]);&lt;br /&gt;
If $link is not specified, mysql_select_db tries to find an open connection.&lt;br /&gt;
&lt;br /&gt;
You can close a connection with mysql_close&lt;br /&gt;
 mysql_close($link);&lt;br /&gt;
&lt;br /&gt;
SQL commands are send through ''mysql_query'' command. &lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY) &lt;br /&gt;
&lt;br /&gt;
for example,&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','B5','Commander')&amp;quot;) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
will insert data to employee table. ''mysql_query'' also receive the data from the server. For example, if you have send a ''select'' query, then you can loop over the results for each returned row.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
  $result = mysql_query('select * from employee');&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name :{$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department : {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title : {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the sql command is sent, the individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the attributes can be reached by their names. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH, hence you can avoid specifying this parameter. If you are interested in the number of rows returned, you can call ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
PHP Manual has a very detailed description of each mysql related function [http://us2.php.net/manual/en/ref.mysql.php]&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
&lt;br /&gt;
The configuration file for MySQL is  ''/etc/mysql/my.cnf''. It specifies MySQL directories and other parameters. If you want to migrate MySQL, it is sufficient to copy files from ''datadir'', which is in debian located at ''/var/lib/mysql''. One interesting parameter is bind-address. By default, it restricts the database access to the localhost, so you may want to comment it out.&lt;br /&gt;
&lt;br /&gt;
The program ''mysqladmin'' is the official program to communicate with the server for managerial tasks. Common tasks that can be handled by ''mysqladmin'' include flushing some MySQL elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client. In order to use this command, you need to provide the administrator password.&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=68</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=68"/>
		<updated>2009-09-18T13:35:04Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Inserting New Data */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 apt-get install mysql-server &lt;br /&gt;
&lt;br /&gt;
When the server is being installed, it will ask you for a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. You can install it with:&lt;br /&gt;
&lt;br /&gt;
 apt-get install phpmyadmin&lt;br /&gt;
&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then selecte ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. The format of ''grant'' is&lt;br /&gt;
&lt;br /&gt;
 grant PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
&lt;br /&gt;
The ''insert into'' command inserts a new row into a table in a database.  The general form of the command is:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
&lt;br /&gt;
The above commands adds one row the table TABLENAME.  The number of values supplied in parenthesis must be equal to the number of columns in the table, and the values are added in the same order as the order of the columns.&lt;br /&gt;
&lt;br /&gt;
If you want to insert a new row with only some values set in the row, you can specify which columns to use:&lt;br /&gt;
&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
 insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Wonderland&amp;quot;,&amp;quot;Lost traveler&amp;quot;);&lt;br /&gt;
 insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Neverland&amp;quot;,&amp;quot;Leader&amp;quot;);&lt;br /&gt;
 insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Shire&amp;quot;,&amp;quot;Ring bearer&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Here is another example that leaves some of the fields in the new row blank:&lt;br /&gt;
&lt;br /&gt;
 insert into employee (name,blah) values (&amp;quot;Fry&amp;quot;,&amp;quot;Delivery Boy&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
''select'' command returns the rows from a database where some conditions are matched.&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
Instead of individual attributes, you can ask to receive all row with ''select *''&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
 select * from employee;&lt;br /&gt;
will return&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+&lt;br /&gt;
| name  | dept       | jobtitle      |&lt;br /&gt;
+-------+------------+---------------+&lt;br /&gt;
| Alice | Wonderland | Lost traveler |&lt;br /&gt;
| Peter | Neverland  | Leader        |&lt;br /&gt;
| Frodo | Shire      | Ring beerer   |&lt;br /&gt;
+-------+------------+---------------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or you can get  Peter's title.&lt;br /&gt;
 select `jobtitle` from employee where `name` like 'Peter';&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
''update'' command is used to update individual rows. The format is&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where conditions]&lt;br /&gt;
&lt;br /&gt;
For example, the following will update Frodo's job title.&lt;br /&gt;
&lt;br /&gt;
 update employee set `jobtitle`='Hero' where `name` like 'Frodo'&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
''delete from'' command is used to delete row(s) from the database&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Frodo.&lt;br /&gt;
&lt;br /&gt;
 delete from employee where `name` like 'Frodo';&lt;br /&gt;
where as&lt;br /&gt;
 delete from  employee;&lt;br /&gt;
deletes everything from the table.&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to install ''php5-mysql'' package first. Then from a PHP page, you can connect to a mysql database by using ''mysql_connect'' command.&lt;br /&gt;
&lt;br /&gt;
 $link=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The hostname is either IP or name of the server running MySQL daemon, the database user is the name of the user who can run the required commands and password is the user's the user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $link will be NULL. Once connected, you can select a database:&lt;br /&gt;
 mysql_select_db('DATABASENAME',[$link]);&lt;br /&gt;
If $link is not specified, mysql_select_db tries to find an open connection.&lt;br /&gt;
&lt;br /&gt;
You can close a connection with mysql_close&lt;br /&gt;
 mysql_close($link);&lt;br /&gt;
&lt;br /&gt;
SQL commands are send through ''mysql_query'' command. &lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY) &lt;br /&gt;
&lt;br /&gt;
for example,&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','B5','Commander')&amp;quot;) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
will insert data to employee table. ''mysql_query'' also receive the data from the server. For example, if you have send a ''select'' query, then you can loop over the results for each returned row.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
  $result = mysql_query('select * from employee');&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name :{$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department : {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title : {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the sql command is sent, the individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the attributes can be reached by their names. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH, hence you can avoid specifying this parameter. If you are interested in the number of rows returned, you can call ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
PHP Manual has a very detailed description of each mysql related function [http://us2.php.net/manual/en/ref.mysql.php]&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
&lt;br /&gt;
The configuration file for MySQL is  ''/etc/mysql/my.cnf''. It specifies MySQL directories and other parameters. If you want to migrate MySQL, it is sufficient to copy files from ''datadir'', which is in debian located at ''/var/lib/mysql''. One interesting parameter is bind-address. By default, it restricts the database access to the localhost, so you may want to comment it out.&lt;br /&gt;
&lt;br /&gt;
The program ''mysqladmin'' is the official program to communicate with the server for managerial tasks. Common tasks that can be handled by ''mysqladmin'' include flushing some MySQL elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client. In order to use this command, you need to provide the administrator password.&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=67</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=67"/>
		<updated>2009-09-17T23:05:48Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Managing Tables */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 apt-get install mysql-server &lt;br /&gt;
&lt;br /&gt;
When the server is being installed, it will ask you for a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. You can install it with:&lt;br /&gt;
&lt;br /&gt;
 apt-get install phpmyadmin&lt;br /&gt;
&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then selecte ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. The format of ''grant'' is&lt;br /&gt;
&lt;br /&gt;
 grant PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'' and  ''drop table'', respectively.  When creating a table, the user needs to supply the attribute information for each column in the table.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
mysql contains several attribute types for strings, numerical values, dates, etc. The common types include char, varchar, text, blob, int, float, and date. The mysql manual contains a full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]].&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee:&lt;br /&gt;
&lt;br /&gt;
 create table employee (name char(40), dept char(20), jobtitle char(30), joined date, id int);&lt;br /&gt;
&lt;br /&gt;
The employee table has 5 columns, a ''name'' string with a maximum length of 40 characters, a ''dept'' string with a maximum length of 20 characters, a ''jobtitle'' string with a maximum length of 30 characters, a ''joined'' date, and an ''id'' integer.&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database by running:&lt;br /&gt;
&lt;br /&gt;
 show tables;&lt;br /&gt;
&lt;br /&gt;
You can get a detailed description of the columns in a table with:&lt;br /&gt;
&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
''insert into'' command inserts a new row to the database&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
If you want to insert a new row with only some attributes set, you can do that by telling which attributes you want to be set:&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
  insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Wonderland&amp;quot;,&amp;quot;Lost traveler&amp;quot;);&lt;br /&gt;
  insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Neverland&amp;quot;,&amp;quot;Leader&amp;quot;);&lt;br /&gt;
  insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Shire&amp;quot;,&amp;quot;Ring beerer&amp;quot;);&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
''select'' command returns the rows from a database where some conditions are matched.&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
Instead of individual attributes, you can ask to receive all row with ''select *''&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
 select * from employee;&lt;br /&gt;
will return&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+&lt;br /&gt;
| name  | dept       | jobtitle      |&lt;br /&gt;
+-------+------------+---------------+&lt;br /&gt;
| Alice | Wonderland | Lost traveler |&lt;br /&gt;
| Peter | Neverland  | Leader        |&lt;br /&gt;
| Frodo | Shire      | Ring beerer   |&lt;br /&gt;
+-------+------------+---------------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or you can get  Peter's title.&lt;br /&gt;
 select `jobtitle` from employee where `name` like 'Peter';&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
''update'' command is used to update individual rows. The format is&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where conditions]&lt;br /&gt;
&lt;br /&gt;
For example, the following will update Frodo's job title.&lt;br /&gt;
&lt;br /&gt;
 update employee set `jobtitle`='Hero' where `name` like 'Frodo'&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
''delete from'' command is used to delete row(s) from the database&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Frodo.&lt;br /&gt;
&lt;br /&gt;
 delete from employee where `name` like 'Frodo';&lt;br /&gt;
where as&lt;br /&gt;
 delete from  employee;&lt;br /&gt;
deletes everything from the table.&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to install ''php5-mysql'' package first. Then from a PHP page, you can connect to a mysql database by using ''mysql_connect'' command.&lt;br /&gt;
&lt;br /&gt;
 $link=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The hostname is either IP or name of the server running MySQL daemon, the database user is the name of the user who can run the required commands and password is the user's the user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $link will be NULL. Once connected, you can select a database:&lt;br /&gt;
 mysql_select_db('DATABASENAME',[$link]);&lt;br /&gt;
If $link is not specified, mysql_select_db tries to find an open connection.&lt;br /&gt;
&lt;br /&gt;
You can close a connection with mysql_close&lt;br /&gt;
 mysql_close($link);&lt;br /&gt;
&lt;br /&gt;
SQL commands are send through ''mysql_query'' command. &lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY) &lt;br /&gt;
&lt;br /&gt;
for example,&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','B5','Commander')&amp;quot;) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
will insert data to employee table. ''mysql_query'' also receive the data from the server. For example, if you have send a ''select'' query, then you can loop over the results for each returned row.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
  $result = mysql_query('select * from employee');&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name :{$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department : {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title : {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the sql command is sent, the individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the attributes can be reached by their names. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH, hence you can avoid specifying this parameter. If you are interested in the number of rows returned, you can call ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
PHP Manual has a very detailed description of each mysql related function [http://us2.php.net/manual/en/ref.mysql.php]&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
&lt;br /&gt;
The configuration file for MySQL is  ''/etc/mysql/my.cnf''. It specifies MySQL directories and other parameters. If you want to migrate MySQL, it is sufficient to copy files from ''datadir'', which is in debian located at ''/var/lib/mysql''. One interesting parameter is bind-address. By default, it restricts the database access to the localhost, so you may want to comment it out.&lt;br /&gt;
&lt;br /&gt;
The program ''mysqladmin'' is the official program to communicate with the server for managerial tasks. Common tasks that can be handled by ''mysqladmin'' include flushing some MySQL elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client. In order to use this command, you need to provide the administrator password.&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=66</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=66"/>
		<updated>2009-09-17T22:57:39Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Using MySQL */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 apt-get install mysql-server &lt;br /&gt;
&lt;br /&gt;
When the server is being installed, it will ask you for a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. You can install it with:&lt;br /&gt;
&lt;br /&gt;
 apt-get install phpmyadmin&lt;br /&gt;
&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then selecte ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many ''tables'', which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
mysql also has its own user structure which means that you will have to add mysql users and give them permissions to access specific databases.&lt;br /&gt;
&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database on the server.  To manage users, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the same PC with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  &lt;br /&gt;
&lt;br /&gt;
==Managing Users==&lt;br /&gt;
User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. The format of ''grant'' is&lt;br /&gt;
&lt;br /&gt;
 grant PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'', ''drop table'' command.  When creating a table, the user needs to supply the attribute information.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
MySQL contains several attribute types for strings, numerical values, dates etc. The common types include  char, varchar, text, blob, int, float,date etc. MySQL manual contains full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]]&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee&lt;br /&gt;
 create table employee ( name char(20), dept char(20), jobtitle char(20));&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database with &lt;br /&gt;
 show tables;&lt;br /&gt;
command. You can get a detailed description of a table with:&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
''insert into'' command inserts a new row to the database&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
If you want to insert a new row with only some attributes set, you can do that by telling which attributes you want to be set:&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
  insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Wonderland&amp;quot;,&amp;quot;Lost traveler&amp;quot;);&lt;br /&gt;
  insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Neverland&amp;quot;,&amp;quot;Leader&amp;quot;);&lt;br /&gt;
  insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Shire&amp;quot;,&amp;quot;Ring beerer&amp;quot;);&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
''select'' command returns the rows from a database where some conditions are matched.&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
Instead of individual attributes, you can ask to receive all row with ''select *''&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
 select * from employee;&lt;br /&gt;
will return&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+&lt;br /&gt;
| name  | dept       | jobtitle      |&lt;br /&gt;
+-------+------------+---------------+&lt;br /&gt;
| Alice | Wonderland | Lost traveler |&lt;br /&gt;
| Peter | Neverland  | Leader        |&lt;br /&gt;
| Frodo | Shire      | Ring beerer   |&lt;br /&gt;
+-------+------------+---------------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or you can get  Peter's title.&lt;br /&gt;
 select `jobtitle` from employee where `name` like 'Peter';&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
''update'' command is used to update individual rows. The format is&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where conditions]&lt;br /&gt;
&lt;br /&gt;
For example, the following will update Frodo's job title.&lt;br /&gt;
&lt;br /&gt;
 update employee set `jobtitle`='Hero' where `name` like 'Frodo'&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
''delete from'' command is used to delete row(s) from the database&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Frodo.&lt;br /&gt;
&lt;br /&gt;
 delete from employee where `name` like 'Frodo';&lt;br /&gt;
where as&lt;br /&gt;
 delete from  employee;&lt;br /&gt;
deletes everything from the table.&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to install ''php5-mysql'' package first. Then from a PHP page, you can connect to a mysql database by using ''mysql_connect'' command.&lt;br /&gt;
&lt;br /&gt;
 $link=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The hostname is either IP or name of the server running MySQL daemon, the database user is the name of the user who can run the required commands and password is the user's the user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $link will be NULL. Once connected, you can select a database:&lt;br /&gt;
 mysql_select_db('DATABASENAME',[$link]);&lt;br /&gt;
If $link is not specified, mysql_select_db tries to find an open connection.&lt;br /&gt;
&lt;br /&gt;
You can close a connection with mysql_close&lt;br /&gt;
 mysql_close($link);&lt;br /&gt;
&lt;br /&gt;
SQL commands are send through ''mysql_query'' command. &lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY) &lt;br /&gt;
&lt;br /&gt;
for example,&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','B5','Commander')&amp;quot;) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
will insert data to employee table. ''mysql_query'' also receive the data from the server. For example, if you have send a ''select'' query, then you can loop over the results for each returned row.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
  $result = mysql_query('select * from employee');&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name :{$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department : {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title : {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the sql command is sent, the individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the attributes can be reached by their names. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH, hence you can avoid specifying this parameter. If you are interested in the number of rows returned, you can call ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
PHP Manual has a very detailed description of each mysql related function [http://us2.php.net/manual/en/ref.mysql.php]&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
&lt;br /&gt;
The configuration file for MySQL is  ''/etc/mysql/my.cnf''. It specifies MySQL directories and other parameters. If you want to migrate MySQL, it is sufficient to copy files from ''datadir'', which is in debian located at ''/var/lib/mysql''. One interesting parameter is bind-address. By default, it restricts the database access to the localhost, so you may want to comment it out.&lt;br /&gt;
&lt;br /&gt;
The program ''mysqladmin'' is the official program to communicate with the server for managerial tasks. Common tasks that can be handled by ''mysqladmin'' include flushing some MySQL elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client. In order to use this command, you need to provide the administrator password.&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=65</id>
		<title>Databases and MySQL</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Databases_and_MySQL&amp;diff=65"/>
		<updated>2009-09-17T22:50:45Z</updated>

		<summary type="html">&lt;p&gt;Wiseman: /* Using MySQL */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=SQL With MySQL=&lt;br /&gt;
&lt;br /&gt;
SQL (Structured Query Language) is a language used to interact with a relational database.  While SQL is the standard basis for database communication, different database vendors usually add their own small modifications or enhancements.  In order to use SQL, you need to use a client that communicates with the server. Many high-level languages like Java, C, and PHP have the libraries for that purpose.  We will be using ''MySQL'' in this class (pronounced: My S-Q-L), which is one of the most widely used databases.  You can use the command line program ''mysql'' to communicate with the server directly.&lt;br /&gt;
&lt;br /&gt;
=Installing MySQL=&lt;br /&gt;
&lt;br /&gt;
You will need both the mysql server and the mysql client. Installing the server automatically installs the client package, so you can get them both by running:&lt;br /&gt;
&lt;br /&gt;
 apt-get install mysql-server &lt;br /&gt;
&lt;br /&gt;
When the server is being installed, it will ask you for a root password for the mysql server. Pick anything you like for the password, but make sure you can remember it. You will need this password when you access your database server for administration purposes (like adding new databases or database users). If you forget the password, you can reset it later, but it's not a simple process.&lt;br /&gt;
&lt;br /&gt;
There are also several tools that can help you manage mysql databases easily.  One in particular is is ''PhpMyAdmin'' that allows you to manage your database over the web. You can install it with:&lt;br /&gt;
&lt;br /&gt;
 apt-get install phpmyadmin&lt;br /&gt;
&lt;br /&gt;
Installing phpmyadmin will automatically configure Apache for you if you let it.  When installing, select ''Apache2'' with the space bar when it asks which databases to auto configure.  Then selecte ''Yes'' when it asks if you want to configure the database for phpmyadmin.  You will have to supply the root mysql password you chose when installing mysql.  You will also have to add a phpmyadmin password for use later.  Make sure to pick something you can remember.&lt;br /&gt;
&lt;br /&gt;
=Using MySQL=&lt;br /&gt;
==Starting MySQL==&lt;br /&gt;
&lt;br /&gt;
The interface to mysql is the ''mysql'' command.  It is generally run like this:&lt;br /&gt;
&lt;br /&gt;
 mysql -u USERNAME -p -h HOSTNAME DATABASENAME&lt;br /&gt;
&lt;br /&gt;
Note that, the user USERNAME needs to have access to the database DATABASENAME, otherwise mysql will return an error.  See the next section to learn how to give users access to databases.  When you run the command as above, it will prompt you for a password, which is the password for user USERNAME.  Also, if you are connecting to a mysql server on the local PC, you can leave off the ''-h HOSTNAME'' argument.  For example, to connect to the ''mydb'' database on the local host with user ''me'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p mydb&lt;br /&gt;
&lt;br /&gt;
To do the same for a mysql server running on host ''somewhere.dyndns.org'', do:&lt;br /&gt;
&lt;br /&gt;
 mysql -u me -p -h somewhere.dyndns.org mydb&lt;br /&gt;
&lt;br /&gt;
==Managing Databases==&lt;br /&gt;
Each mysql server can host many different ''databases''.  Each database contains many tables (described later), which store the actual information in the database.  There is no relationship between data or tables in separate databases, i.e., each database should be a self contained entity.&lt;br /&gt;
&lt;br /&gt;
Databases in mysql are created with:&lt;br /&gt;
&lt;br /&gt;
 create database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
The user must have administrative administrative rights in order to create a database. A database can be deleted with the ''drop'' command:&lt;br /&gt;
&lt;br /&gt;
 drop database DATABASENAME;&lt;br /&gt;
&lt;br /&gt;
At the mysql command prompt, you can change your active database like so:&lt;br /&gt;
&lt;br /&gt;
 use DATABASE;&lt;br /&gt;
&lt;br /&gt;
Once the active database is changed, all your commands will be applied to the newly active database.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will create and switch to a new database:&lt;br /&gt;
&lt;br /&gt;
 create database company;&lt;br /&gt;
 use company;&lt;br /&gt;
&lt;br /&gt;
==Managing Users==&lt;br /&gt;
All administrative information about the mysql server is contained in the ''mysql'' database.  To manage users or databases, you need to access mysql as the root user for the ''mysql'' database (assuming you are on the localhost with the server):&lt;br /&gt;
&lt;br /&gt;
 mysql -u root -p mysql&lt;br /&gt;
&lt;br /&gt;
You will be prompted for a password, which in this case is the root password you selected when you installed mysql.  User information is stored in the ''user'' table. The fields of this table are: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Field                 | Type                              | Null | Key | Default | Extra |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
| Host                  | char(60)                          | NO   | PRI |         |       |&lt;br /&gt;
| User                  | char(16)                          | NO   | PRI |         |       |&lt;br /&gt;
| Password              | char(41)                          | NO   |     |         |       |&lt;br /&gt;
| Select_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Insert_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Update_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Delete_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Drop_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Reload_priv           | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Shutdown_priv         | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Process_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| File_priv             | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Grant_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| References_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Index_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_db_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Super_priv            | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_tmp_table_priv | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Lock_tables_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Execute_priv          | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_slave_priv       | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Repl_client_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_view_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Show_view_priv        | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_routine_priv   | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Alter_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| Create_user_priv      | enum('N','Y')                     | NO   |     | N       |       |&lt;br /&gt;
| ssl_type              | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |&lt;br /&gt;
| ssl_cipher            | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_issuer           | blob                              | NO   |     |         |       |&lt;br /&gt;
| x509_subject          | blob                              | NO   |     |         |       |&lt;br /&gt;
| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |&lt;br /&gt;
+-----------------------+-----------------------------------+------+-----+---------+-------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Important fields are ''Host'' (which specifies from which host(s) a user can access the database), ''User'' (name of the user), ''Password'' (the password for this user), and several boolean privilege attributes. Fortunately, mysql provides the ''grant'' command that makes dealing with mysql users simpler. The format of ''grant'' is&lt;br /&gt;
&lt;br /&gt;
 grant PRIVILEGE_NAMES on DATABASE.TABLE to 'USERNAME'@'HOSTNAME' identified by 'USERPASSWORD';&lt;br /&gt;
&lt;br /&gt;
The PRIVILEGE_NAMES could be individual privileges (SELECT, INSERT, DROP, etc.), it could be a group of privileges (USAGE), or it could be the special ''ALL'' privilege which grants USERNAME all possible privileges. The TABLE name can be, and often is, a wild card (*) which means that USERNAME is given the indicated privileges for all tables in the database DATABASE. The HOSTNAME can be any host name (somewhere.dyndns.org), it could contain the SQL wild card '%' (%.dynsdns.org) to allow access from multiple hosts that share a common domain, or it could be just the wild card (%) to indicate access should be granted from all hosts.&lt;br /&gt;
&lt;br /&gt;
Here is an example that gives all privileges to the user ''boss' with the password ''employer'', where the user connects only from the localhost.&lt;br /&gt;
 &lt;br /&gt;
 grant all  on company.* to 'boss'@'localhost' identified by 'employer';&lt;br /&gt;
&lt;br /&gt;
After each grant, you need to tell mysql to reload grant tables by flushing privileges:&lt;br /&gt;
&lt;br /&gt;
  flush privileges;&lt;br /&gt;
&lt;br /&gt;
==Managing Tables==&lt;br /&gt;
Similar to databases, tables are created and deleted with ''create table'', ''drop table'' command.  When creating a table, the user needs to supply the attribute information.&lt;br /&gt;
&lt;br /&gt;
 create table TABLENAME (ATTRIBUTE TYPE, ATTRIBUTE TYPE,.....);&lt;br /&gt;
&lt;br /&gt;
MySQL contains several attribute types for strings, numerical values, dates etc. The common types include  char, varchar, text, blob, int, float,date etc. MySQL manual contains full list of available types [[http://dev.mysql.com/doc/refman/5.0/en/data-types.html]]&lt;br /&gt;
&lt;br /&gt;
For example, the following command will create a new table called employee&lt;br /&gt;
 create table employee ( name char(20), dept char(20), jobtitle char(20));&lt;br /&gt;
&lt;br /&gt;
You can see the tables in a database with &lt;br /&gt;
 show tables;&lt;br /&gt;
command. You can get a detailed description of a table with:&lt;br /&gt;
 describe TABLENAME;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SQL Commands==&lt;br /&gt;
===Inserting New Data===&lt;br /&gt;
''insert into'' command inserts a new row to the database&lt;br /&gt;
 insert into TABLENAME values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE,.....)&lt;br /&gt;
If you want to insert a new row with only some attributes set, you can do that by telling which attributes you want to be set:&lt;br /&gt;
 insert into TABLENAME (ATTIBUTE1,ATTRIBUTE2) values (ATTRIBUTE1_VALUE, ATTRIBUTE2_VALUE)&lt;br /&gt;
&lt;br /&gt;
For example, the following commands will insert new employees to the company database.&lt;br /&gt;
&lt;br /&gt;
  insert into employee values (&amp;quot;Alice&amp;quot;,&amp;quot;Wonderland&amp;quot;,&amp;quot;Lost traveler&amp;quot;);&lt;br /&gt;
  insert into employee values (&amp;quot;Peter&amp;quot;,&amp;quot;Neverland&amp;quot;,&amp;quot;Leader&amp;quot;);&lt;br /&gt;
  insert into employee values (&amp;quot;Frodo&amp;quot;,&amp;quot;Shire&amp;quot;,&amp;quot;Ring beerer&amp;quot;);&lt;br /&gt;
===Retrieving Existing Data===&lt;br /&gt;
''select'' command returns the rows from a database where some conditions are matched.&lt;br /&gt;
 select ATTRIBUTE1,ATTRIBUTE2,..... from TABLE [where CONDITION]&lt;br /&gt;
&lt;br /&gt;
Instead of individual attributes, you can ask to receive all row with ''select *''&lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
 select * from employee;&lt;br /&gt;
will return&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
+-------+------------+---------------+&lt;br /&gt;
| name  | dept       | jobtitle      |&lt;br /&gt;
+-------+------------+---------------+&lt;br /&gt;
| Alice | Wonderland | Lost traveler |&lt;br /&gt;
| Peter | Neverland  | Leader        |&lt;br /&gt;
| Frodo | Shire      | Ring beerer   |&lt;br /&gt;
+-------+------------+---------------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or you can get  Peter's title.&lt;br /&gt;
 select `jobtitle` from employee where `name` like 'Peter';&lt;br /&gt;
&lt;br /&gt;
===Updating Existing Data===&lt;br /&gt;
''update'' command is used to update individual rows. The format is&lt;br /&gt;
 update TABLENAME set ATTRIBUTE1=value1,ATTRIBUTE2=value2 .... [where conditions]&lt;br /&gt;
&lt;br /&gt;
For example, the following will update Frodo's job title.&lt;br /&gt;
&lt;br /&gt;
 update employee set `jobtitle`='Hero' where `name` like 'Frodo'&lt;br /&gt;
&lt;br /&gt;
===Deleting Existing Data===&lt;br /&gt;
''delete from'' command is used to delete row(s) from the database&lt;br /&gt;
 delete from employee where CONDITION;&lt;br /&gt;
&lt;br /&gt;
For example, the following command will delete Frodo.&lt;br /&gt;
&lt;br /&gt;
 delete from employee where `name` like 'Frodo';&lt;br /&gt;
where as&lt;br /&gt;
 delete from  employee;&lt;br /&gt;
deletes everything from the table.&lt;br /&gt;
&lt;br /&gt;
=PHP and MySQL=&lt;br /&gt;
&lt;br /&gt;
In order to use mysql function in php, you need to install ''php5-mysql'' package first. Then from a PHP page, you can connect to a mysql database by using ''mysql_connect'' command.&lt;br /&gt;
&lt;br /&gt;
 $link=mysql_connect('HOSTNAME','DATABASEUSER','PASSWORD');&lt;br /&gt;
&lt;br /&gt;
The hostname is either IP or name of the server running MySQL daemon, the database user is the name of the user who can run the required commands and password is the user's the user's password. ''mysql_connect'' returns a mysql object that you will use later to communicate with the server. If the connection fails, $link will be NULL. Once connected, you can select a database:&lt;br /&gt;
 mysql_select_db('DATABASENAME',[$link]);&lt;br /&gt;
If $link is not specified, mysql_select_db tries to find an open connection.&lt;br /&gt;
&lt;br /&gt;
You can close a connection with mysql_close&lt;br /&gt;
 mysql_close($link);&lt;br /&gt;
&lt;br /&gt;
SQL commands are send through ''mysql_query'' command. &lt;br /&gt;
&lt;br /&gt;
 mysql_query(SQLQUERY) &lt;br /&gt;
&lt;br /&gt;
for example,&lt;br /&gt;
 mysql_query(&amp;quot;insert into employee values ('Sheridan','B5','Commander')&amp;quot;) or die('Error, insert query failed');&lt;br /&gt;
&lt;br /&gt;
will insert data to employee table. ''mysql_query'' also receive the data from the server. For example, if you have send a ''select'' query, then you can loop over the results for each returned row.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
  $result = mysql_query('select * from employee');&lt;br /&gt;
&lt;br /&gt;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))&lt;br /&gt;
  {&lt;br /&gt;
    echo &amp;quot;Name :{$row['name']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Department : {$row['dept']} &amp;lt;br&amp;gt;&amp;quot; .&lt;br /&gt;
         &amp;quot;Job Title : {$row['jobtitle']} &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the sql command is sent, the individual rows are fetched with ''mysql_fetch_array''. The parameter ''MYSQL_ASSOC'' tells the function that the results should be in the format of an associative array, i.e., the attributes can be reached by their names. If you want a numeric array, you can use MYSQL_NUM. The default is MYSQL_BOTH, hence you can avoid specifying this parameter. If you are interested in the number of rows returned, you can call ''mysql_num_rows'' function.&lt;br /&gt;
&lt;br /&gt;
PHP Manual has a very detailed description of each mysql related function [http://us2.php.net/manual/en/ref.mysql.php]&lt;br /&gt;
&lt;br /&gt;
=Other MySQL Information=&lt;br /&gt;
&lt;br /&gt;
The configuration file for MySQL is  ''/etc/mysql/my.cnf''. It specifies MySQL directories and other parameters. If you want to migrate MySQL, it is sufficient to copy files from ''datadir'', which is in debian located at ''/var/lib/mysql''. One interesting parameter is bind-address. By default, it restricts the database access to the localhost, so you may want to comment it out.&lt;br /&gt;
&lt;br /&gt;
The program ''mysqladmin'' is the official program to communicate with the server for managerial tasks. Common tasks that can be handled by ''mysqladmin'' include flushing some MySQL elements (such as tables, privileges etc.), kill mysql threads, restart, shutdown, or validate a client. In order to use this command, you need to provide the administrator password.&lt;/div&gt;</summary>
		<author><name>Wiseman</name></author>
		
	</entry>
</feed>