<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hot Scripts Blog &#187; CGI &amp; Perl</title>
	<atom:link href="http://www.hotscripts.com/blog/category/tutorials/cgi-perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hotscripts.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 17 May 2012 12:18:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Regexes in Perl</title>
		<link>http://www.hotscripts.com/blog/regexes-in-perl/</link>
		<comments>http://www.hotscripts.com/blog/regexes-in-perl/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 03:01:24 +0000</pubDate>
		<dc:creator>DEVpapers</dc:creator>
				<category><![CDATA[CGI & Perl]]></category>

		<guid isPermaLink="false">http://www.hotscripts.com/blog/?p=651</guid>
		<description><![CDATA[An in depth look to regexes with plenty of examples to learn from.<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/regexes-in-perl/">Regexes in Perl</a></p>

Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/regex-operators-in-perl/' rel='bookmark' title='Regex Operators in Perl'>Regex Operators in Perl</a></li>
<li><a href='http://www.hotscripts.com/blog/finding-a-string-with-preg_match/' rel='bookmark' title='Finding a string with preg_match()'>Finding a string with preg_match()</a></li>
<li><a href='http://www.hotscripts.com/blog/pattern-matching-regular-expressions/' rel='bookmark' title='Pattern matching with Regular Expressions'>Pattern matching with Regular Expressions</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div><span>In the last tutorial, we went over the three regex operators.  We also learned what regexes were and why we&#8217;d use these in our every scripts.  This tutorial will be going deeper in the many uses to manipulate our data in any way we wish.</p>
<p>Regular Expressions are a huge topic all on it&#8217;s own, there is no possible way to go over all of them here so you may wish to purchase a book on the topic (yes, complete books are dedicated to these) for future reading and understanding.</p>
<p>We&#8217;ll use the same originally example on a basic regex before we continue.</p>
<p><em>while (&lt;STDIN&gt;)<br />
{<br />
if(m/exit/)<br />
{<br />
exit;<br />
}<br />
}</em></p>
<p><strong><br />
Special Characters</strong></p>
<p>Before we begin, there are many special characters we need to be aware of.  Please note the backslash \ preceding the character(s) is not optional and everything is case-sensitive.</p>
<p>\077 : octal character<br />
\a      : internal alarm<br />
\c[    : control character<br />
\D    :  match a non-digit character<br />
\d     :  match a digit character<br />
\E     :  end case modifier<br />
\e     :  escape key<br />
\f      :  form feed character<br />
\L     : lowercase all characters until the end case modifier \E is found<br />
\l      :  lowercases the preceding character<br />
\n     :  new line.  acts as a &lt;br&gt; tag does in HTML<br />
\r     :  return<br />
\S    :  matches a non-white space character<br />
\s     :  matches a white space character<br />
\t      :  inserts a tab<br />
\U    :  uppercase until the end case modifier \E is found<br />
\u    :  uppercase the next character<br />
\W  :  matches a non-word character<br />
\w    :  matches a word character</p>
<p><strong><br />
Match (nearly) any characters</strong></p>
<p>One of the most useful, in my opinion of course, special characters in regexes is the . (the dot).  This matches any and all characters other than the new line \n.  This means, all letters a-z, numbers 0-9, dashes - and any weird @$()! character will be matched except the new line feed.</p>
<p><em>my $sentence = "this is a sentence, woohoo!";<br />
$sentence =~ s/./T/;<br />
print $sentence;<br />
This is a sentence, woohoo!</em></p>
<p>The . (dot) matches any non-new line character and since we're doing a substitution for "T", the first character it comes across will be replaced with this letter.  The "t" was replaced by "T".</p>
<p>If . is a metacharacter,what if we really wanted to match a period in our regex?  This comes up very frequently, especially with the period, and lucky for us there is a very quick solution.  If you place a backslash in front of the metachacter, it acts as a normal character instead of being special.</p>
<p><em>my $sentence = "We have ourselves a .";<br />
$sentence = s/\./period/;<br />
print $sentence;<br />
We have ourselves a period</em></p>
<p>The only change from the first example and this one to match the period is adding \. to the regex.  So instead of matching any character whatsoever, we're literally matching a period and nothing more.</p>
<p>It is easy for us to match all characters at one time as well.  We already know the dot matches nearly all characters, so we use that along with the global modifer /g to substitute any and all matches.</p>
<p><em>my $sentence = "This is a sentence, woohoo!";<br />
$sentence =~ s/./-/g;<br />
print $sentence;</em><br />
---------------------------</p>
<p><strong>Character Classes</strong></p>
<p>Instead of matching entire words or phrases, we can also match characters.  You can set a character class within square brackets [] such as [abc123] and you can set up a range of characters such as [a-zA-Z].  The latter checks to see if any case of any letter appears in our string.</p>
<p>The range operator can work on parts of the alphabet such as c-f or numerics such as 0-9 or 2-5.  Remember everything is case sensitive, that&#8217;s why we used [a-zA-Z] to match all cases of the letters.</p>
<p><em>my $string = &#8220;We&#8217;re off to see the wizard, the most wonderful wizard of all!&#8221;;<br />
if ($string =~ m/!/)<br />
{<br />
print &#8220;hey hey now, there&#8217;s no reason to shout!&#8221;;<br />
}</em></p>
<p>In our above example, we are testing to see if we can match our explanation point which we can see at the end of the line, it indeed matches. Now lets do a range test that fails.  We&#8217;ll rewrite $string so it&#8217;s all lowercase and see if it contains any uppercase characters.</p>
<p><em>my $string = &#8220;we&#8217;re off to see the wizard, the most wonderful wizard of all!&#8221;;<br />
if ($string =~ m/[A-Z]/)<br />
{<br />
print &#8220;I wonder if this will print..&#8221;;<br />
}</em></p>
<p>The above example will not print because nothing in our character range exists.  Remember, you can check for any letter or number in a range or you can check for any character inside square brackets[].</p>
<p><strong><br />
Multiple chances with matching</strong></p>
<p>You can do multiple tests to see if a variable or string contains this, that or another thing.  If you wanted to see if your string had the word &#8220;blue&#8221; or the color &#8220;red&#8221;, you don&#8217;t have to write to separate regexes to see if it matches.  We use the alternative match pattern instead.</p>
<p>Alternative matching matches one or the other, or in some cases another other <img src='http://www.hotscripts.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  It is not used to test to see if both cases are found, it will stop at the first match it finds and end, whether this be the first possible match or the sixteenth.</p>
<p><em>while(&lt;STDIN&gt;)<br />
{<br />
if(m/red|blue/)<br />
{<br />
print &#8220;these are my fav colors&#8221;;<br />
exit;<br />
}<br />
}</em></p>
<p>This will loop endlessly until it finds something that matches either &#8220;red&#8221; or &#8220;blue&#8221; literally.  &#8220;Red&#8221; and &#8220;bLue&#8221; will not match, neither will any of the alternative ways to write these.</p>
<p>You separate each possible match with a pipe |, and you can use a single word, a single character, a sentence, a number or anything else you want to stick inside.  This idea is pretty straight forward so we&#8217;ll assume the one example will suffice, remember to not just follow the examples written in these tutorials but to make your own and TEST, TEST, TEST!</p>
<p><strong><br />
Quantifiers</strong></p>
<p>Along with simple matching to see if one thing exists or not, we can check to see how many times it exists. Or rather, make sure it matches exactly the number of times we want.</p>
<p>We do this using the quantifiers from the list below:</p>
<p>*        :  matches zero or more times<br />
+        :  matches one or more times<br />
?        :  matches either one or zero times<br />
{#}    :  matches a percise number of times<br />
{#,}   :  matches atleast a certain number of times<br />
{#,#} :  matches between first number and second number times</p>
<p>In our first example, we will be using the + quantifier to match one or more instances of our test.  If it does, we&#8217;ll do a simple substitution.</p>
<p><em>my $quantifier = &#8220;The frog goes rrribbit, rrribbit&#8221;;<br />
$quantifier =~ s/r+/r/g;</em></p>
<p><em>print $quantifier;</em></p>
<p><em> The frog goes ribbit, ribbit</em></p>
<p>Since the + quantifier matches only characters or words that exist atleast one, maybe more, times, we replaced all occurances of more than one &#8220;r&#8221; consecutively.  This would work if you had one &#8220;r&#8221; or 100,000 of them.  As long as it&#8217;s more than one, Perl is happy.</p>
<p>In this next example, we are checking to see if the user typed in between 10 and 30 characters.</p>
<p><em>while(&lt;STDIN&gt;)<br />
{<br />
if(m/.{10,30}/)<br />
{<br />
print &#8220;good for you!\n&#8221;;<br />
}<br />
}</em></p>
<p>Quanitifiers are greedy by nature.  This means they&#8217;ll slurp up the biggest match as they can that follows the match you&#8217;re telling it to.  So instead of taking the first applicable match, it will take the biggest (in terms of character size) as possible thus occassionally providing unwanted results.</p>
<p><em>my $quote = &#8220;to be or not to be, that is the question&#8221;;<br />
$quote =~ s/.*be/To/;<br />
print $quote;<br />
To, that is the question</em></p>
<p>What we tried to do was change the first word from &#8220;to&#8221; to &#8220;To&#8221; with a capital &#8220;T&#8221; by substituting any and all characters before &#8220;be&#8221;.  Remember, .* means to match all characters and as we placed the characters &#8220;be&#8221; after it, we tried to match the first few characters of our sentence.</p>
<p>What really happened was it found a bigger match&#8211; instead of substituting the first match of &#8220;to&#8221;, it matched everything and substituted everything uptil the word before the comma.</p>
<p><strong><br />
Regex Anchors (assertsions)</strong></p>
<p>Often times we need total control over what to match and where. Rather than matching the first match or taking your chance using a quantifier, we can tell our regex to match specific conditions.  These conditions include matching at the beginning or end of a string, match word or non-word boundaries, etc.</p>
<p>Here is a chart of the majority of the anchors we can use that will give us the power we need for accurate results and matching.</p>
<p>^   :  matches the beginning of the line<br />
$   :  matches the end of the line<br />
\A :  matches the beginning of the string<br />
\B :  matches a non-word boundary<br />
\b  :  matches a word boundary<br />
\Z  :  matches the end of the line<br />
\z   :  matches the end of the line</p>
<p>The \A and \Z are pretty much the same as ^ and $, the main difference is ^ and $ can match once and once only&#8211; at the beginning or the end of a string.  The \A and \Z can match multiple times for internal boundaries.</p>
<p>Let&#8217;s use an example we came across above.  We&#8217;re trying to see if the user typed exit.  This time, we&#8217;re making sure exit is the only thing the user typed.  This will not match if the user typed &#8220;I need an exit&#8221; or &#8220;exit is that way&#8221;.</p>
<p><em>while (&lt;STDIN&gt;)<br />
{<br />
if(m/^exit$/)<br />
{<br />
exit;<br />
}<br />
}</em></p>
<p>We just used two of the anchors we learned from the list we read earlier. ^exit is explicitly telling it to match exit at the beginning of the string. exit$ is telling it to match at the end of the string.  In short, by using both the ^ and $ anchors, you are asking it to match if it&#8217;s the only word or set of characters (or even a single character) in the string.</p>
<p>If we just wanted to match the beginning of the string, we would have just used the ^ carrot.  Likewise with the $ if all we wanted to do was match the end of the string.</p>
<p>Word boundaries are anything separated by a whitespace, just like our every day text.  To match a word boundary means to match anything between one whitespace and the next.</p>
<p></span></div>
<div class="hotlist"><strong>Author: Syperder Co</strong><br />
I waltzed into the Web Design community as a professional when I was just 15 years of age founding <a href="http://www.spyderscripts.net/">SpyderWebDesigns</a>. Through the years my interests shifted from web development to backbone and user interaction.</p>
<p>In 2000 Sulfericacid.com was born. The world&#8217;s largest free and 100% ad-free web site where you could use and download 24 Perl and CGI script along with tutorials without limits or restrictions. January 2005 the site was renamed to SpyderScripts.com as a subsidy of SpyderCo.</p>
<p>In 2001 I also founded an SEO company SpyderSubmission.com. We&#8217;ve helped nearly 2300 web sites achieve higher rankings than they ever could have imagined since our launch four years ago.</p>
<p>On a more personal note, I&#8217;ve attained 28 certifications from BrainBench.com and about 40 certifications in total from all resources. One of these is a near Masters in Perl which ranks second highest test score in the state and 17th throughout the country.</p>
<p>I have a Perl Abbot status on PerlMonks.org working on getting my Perl Saint status this fall.</p></div>
<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/regexes-in-perl/">Regexes in Perl</a></p>
<p>Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/regex-operators-in-perl/' rel='bookmark' title='Regex Operators in Perl'>Regex Operators in Perl</a></li>
<li><a href='http://www.hotscripts.com/blog/finding-a-string-with-preg_match/' rel='bookmark' title='Finding a string with preg_match()'>Finding a string with preg_match()</a></li>
<li><a href='http://www.hotscripts.com/blog/pattern-matching-regular-expressions/' rel='bookmark' title='Pattern matching with Regular Expressions'>Pattern matching with Regular Expressions</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.hotscripts.com/blog/regexes-in-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arrays in Perl</title>
		<link>http://www.hotscripts.com/blog/arrays-perl/</link>
		<comments>http://www.hotscripts.com/blog/arrays-perl/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 03:01:24 +0000</pubDate>
		<dc:creator>DEVpapers</dc:creator>
				<category><![CDATA[CGI & Perl]]></category>

		<guid isPermaLink="false">http://www.hotscripts.com/blog/?p=663</guid>
		<description><![CDATA[A deeper look into array variables and how to manipulate them.<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/arrays-perl/">Arrays in Perl</a></p>

Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/working-with-arrays-in-php/' rel='bookmark' title='Working with Arrays in PHP'>Working with Arrays in PHP</a></li>
<li><a href='http://www.hotscripts.com/blog/loops-in-perl-pt-1/' rel='bookmark' title='Loops in Perl &#8211; Pt. 1'>Loops in Perl &#8211; Pt. 1</a></li>
<li><a href='http://www.hotscripts.com/blog/randomness-in-perl/' rel='bookmark' title='Randomness In Perl'>Randomness In Perl</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><span>An array is a list of elements, think of it as a collection of scalars combined to one name. Though it&#8217;s primary use is being called with @, it shares with scalars and hashes the symbol $. Afterall, an array is a bunch of scalars linked together.<br />
<strong>Array assignments/creation:</strong></span></p>
<p>To make an array, we set it as @array = (); Like with scalars and hashes, the name following the variable sign ($ or @) can be virtually any name you like if it starts with an alpha character. An example of an array would be:</p>
<p><em>my @numbers = (1,2,3);</em></p>
<p>An array can be any length (as long as you have enough virtual memory), but it wouldn&#8217;t make much sense using one value when instead a scalar would work just fine.</p>
<p>To print an entire array, we would do something like this:</p>
<p><em>my @numbers = (1,2,3);<br />
print @numbers;</em></p>
<p><strong>How to print an array</strong></p>
<p>The result of this print would be 123. Note there aren&#8217;t any spaces between keys. There are a number of ways to get around this, one of them is using quotes around the array elements and tacking a blank space at the end.</p>
<p><em>my @numbers = (&#8220;1 &#8220;, &#8220;2 &#8220;, &#8220;3 &#8220;);<br />
print @numbers;</em></p>
<p>The result of our new print would be 1 2 3. Probably the result you&#8217;d prefer if you&#8217;re planning on storing crucial bits of information such as names and phone numbers. You can always use quotes around each of your elements, in most cases it&#8217;s required.  For confusion purposes, pretend quotes are always requried and use them around each and every single element in your list.</p>
<p>Now that we know how to create and print an entire array, let&#8217;s go a little deeper and take a look at some its powerful features. An easier way to do this would be to print an array inside quotes. This interprets the array as a list rather than a single line with a bunch of data.</p>
<p><em>print &#8220;@numbers&#8221;;</em></p>
<p>Using the same values as above, this would print 1 2 3. Even though this method works, it won&#8217;t work well enough to be practical in most instances. Later on you&#8217;ll see why when we discuss how to sort arrays. In this example, we are using the join function on the array @numbers and adding a blank space between each element in the list.</p>
<p><em>print join( &#8221; &#8220;, sort @numbers);</em></p>
<p>Let&#8217;s say we don&#8217;t want to print the entire array, if we had a list of 20,000 elements and we only wanted one, why would we? You can print any element you want, afterall each of them are scalars (or more percisely can be used as scalars). Using our previous examples of &#8220;1&#8243; &#8220;2&#8243; &#8220;3&#8243;, let&#8217;s say we wanted to print out the number 2.</p>
<p><em>print $numbers[1];</em></p>
<p>Perl, like most languages, begin their numbering system at zero so of the three elements we have, the results would be [0] = 1, [1] = 2, [2] = 3. To print a single element, we must treat it as a single element and use it&#8217;s scalar sign $.</p>
<p>What would we do if we didn&#8217;t know how many elements our array held and wanted to print just the last one? There are two ways we can go about doing this, the simplest way being print $numbers[-1]. We will be going over the other method shortly when we get into POP.</p>
<p><strong>More array assignments</strong></p>
<p>You can add to an array by assigning another value. You want to be careful when doing this to be sure your number isn&#8217;t overwriting some of your data.</p>
<p><em>$numbers[3] = &#8220;4&#8243;;</em></p>
<p>Now our @numbers contains the numbers 1, 2, 3, 4. Our [3] represents the fouth digit (counting from zero as being one) and we added another number to our list. You could, for the sake of doing it, assign it so high it leaves all unused positions undefined. Not that it&#8217;s recommended as there really isn&#8217;t a logical purpose that I can imagen, it uses more bandwith and decreases the speed if the number is high enough, but for the curious you could do something like:</p>
<p><em>$numbers[999999] = &#8220;5&#8243;;</em></p>
<p>We now have 999996 undefined values because the array had to build enough room for 9999999. Again, this shouldn&#8217;t be done for real life applications, but you can have fun with it and see how it affects the speed of your scripts.<br />
<strong>Pop and Push</strong></p>
<p>POP and PUSH are methods we use to add or remove the last element of our array. If during a print out of your array and for some reason you notice the last element isn&#8217;t needed or if you want to remove it so you can work with it more specifically, you would use POP. This &#8216;pops&#8217; the last element from the list.</p>
<p><em>my @numbers = (&#8220;1&#8243;, &#8220;2&#8243;, &#8220;3&#8243;);<br />
pop(@numbers);</em></p>
<p>Now all we have left is 1 and 2, our friend 3 got pushed off and will never be seen again. If you wanted to use this, maybe you wanted to print this before discarding it.</p>
<p><em>my $lastnumber = pop(@numbers);</em></p>
<p>You can do whatever you want with &#8220;3&#8243; now, you can print it, forget about it, change it&#8217;s value and push it back in, etc. The method push is the counterpart of pop. This is how you add information to the end of everything. Sometimes we don&#8217;t want to add things to the beginning of our arrays (because they print in left to right, top to bottom, order.) Let&#8217;s say we want to add the 3 we just removed back into our list.</p>
<p><em>push(@array, &#8220;3&#8243;);</em></p>
<p>Now 1, 2 and 3 are friends again though 3 might be a little confused or mad that you got rid of him in the first place. No matter how many items you have in your array, this will put it at the end (which we could call using $numbers[-1] like we discussed earlier).<br />
<strong>Shift and Unshift</strong></p>
<p>Push and Pop work at the end of your list, similarily shift and unshift work at the beginning. Shift works like push did as this adds more data. Let&#8217;s add the number 0.</p>
<p><em>unshift(@numbers, &#8220;0&#8243;);</em></p>
<p>Our array of numbers contains the numbers 0, 1, 2 and 3 now. As you probably have guessed it already, unshift will remove the first object in your list. 0 was a trouble maker and didn&#8217;t get along with our dear friends 1, 2 or 3, so let&#8217;s do them a favor and get rid of 0 for them.</p>
<p><em>shift(@numbers);</em></p>
<p>We are now back to 1, 2 and 3 again. You can store the unshifted data into a variable if you want by assigning a scalar to it:</p>
<p><em>my $firstitem = shift(@numbers);</em></p>
<p><strong>Reversing an array</strong></p>
<p>Perl has a very nice reverse feature letting us simply reversing the printing order of the array. We are getting sick and tired of our prints coming out as &#8220;123&#8243;, so let&#8217;s reverse it.</p>
<p><em>print (reverse @numbers);</em></p>
<p>Using 1,2,3 as our array, we would get a print back of 321. As we went over before, this format isn&#8217;t very useful for every day applications. We need to be able to present the data in a useable manner, so to add spaces between elements we&#8217;ll be using split again.</p>
<p><em>print join(&#8221; &#8220;, reverse @numbers);</em></p>
<p><strong>Counting indexes or entries</strong></p>
<p>$#array returns the number of indexes or in the array. This is a useful way in dermining the last valid index in the array if you had something to push onto the end of it. We will be discussing the push functions in a later section, all you need to know right now is how to calculate the last index.</p>
<p><em>my @cookies (&#8220;chocolate chip &#8220;, &#8220;peanut butter &#8220;, &#8220;oreo&#8221;);<br />
print $#cookies;</em></p>
<p>Many times in our scripts we would like to view just the last one or two (or few) pieces of information. You already learned how to use $index[] to assign more data into the array, so this concept shouldn&#8217;t be very new to you. If you begin index with a positive number, it starts at the beginning of the list. Using a negative index starts from the end of the list and moves backwards.</p>
<p><em>my @planets = (&#8220;mercury&#8221;, &#8220;mars&#8221;, &#8220;earth&#8221;, &#8220;saturn&#8221;, &#8220;pluto&#8221;, &#8220;marsian&#8217;s homeland&#8221;);<br />
print $planets[0];<br />
print $planets[-1];</em></p>
<p>The first print will bring back &#8220;mercury&#8221; as it&#8217;s index[0]. index[-1] is the very last element in the list and in our case, it will print &#8220;marsian&#8217;s homeland&#8221;. If you wanted to view the 2nd to the last element, you would use [-2], or for the third [-3] and so on.</p>
<p>To better help you understand indexes, run the code from the bottom of the page called &#8216;indexes&#8217;..</p>
<p>The result of the print would be 2 because we have three items (item or index 0, 1, 2). If instead you wanted to find the number of elements in the list rather than worrying about indexes, scalar is the function we&#8217;d use. Scalar takes any expression and returns the scalar value. Since we are using it on a list @cookies, it returns the number of array entries you have.</p>
<p><em>my @planets = (&#8220;mercury&#8221;, &#8220;mars&#8221;, &#8220;earth&#8221;, &#8220;saturn&#8221;, &#8220;pluto&#8221;, &#8220;marsian&#8217;s homeland&#8221;);<br />
print $planets[0];<br />
print $planets[-1];</em></p>
<p><em>scalar @cookies;</em><br />
<strong>Merging</strong></p>
<p>Your company just merged with another firm down the street and you want one large list of employees rather than two separate lists. This can be a fairly common task and in Perl it&#8217;s quite simple to merge two or more arrays to combine one, larger array.</p>
<p><em>my @employees1 = (&#8220;Fred Flintstone&#8221;, &#8220;Barny Rubble&#8221;, &#8220;Dino Fintstone&#8221;);<br />
my @employees2 = (&#8220;Wilma Flintson&#8221;, &#8220;Bamm-Bamm&#8221;, &#8220;Jigglypuff&#8221;);<br />
my @allemployees = (@employees1, @employees2);</em></p>
<p>@allemployees now contains all the names from employees1 and employees2 with employees1&#8242;s data first. If you want the second sent of employees to appear on top, all you have to do is change the assignment order to = (@employees2, @employees1);</p>
<p><strong><br />
Array splicing</strong></p>
<p>Perl enables us to be able to remove (and optionally return) more than one element at a time. We can do this using array splices. A splice takes a list, an offset and a lenth.</p>
<p><em>splice (@array, offset, length);</em></p>
<p>The offset is an integer where you want the splicing to begin. You use array indexing again (isn&#8217;t it remarkable how often these indexes come up?). The length is also a whole number and this is how many indexes you want to splice.</p>
<p><em>my @colors = (&#8220;red&#8221;, &#8220;blue&#8221;, &#8220;orange&#8221;, &#8220;purple&#8221;, &#8220;pink&#8221;, &#8220;white&#8221;); splice(@colors, 0, 2);<br />
print @colors;</em></p>
<p>We set the offset at 0, which is the 0 index red and the length of 2 . This will remove the first three sets of information from the list leaving purple, pink and white left. If we spliced (@colors, 2, 1), we would be removing the index[2] (orange). Since our length is only 1, we&#8217;re only removing one index.</p>
<p><strong><br />
Sorting</strong></p>
<p>Printing a list of 100,000 names would be a mess if we couldn&#8217;t sort the list, wouldn&#8217;t it? You&#8217;d have names or phone numbers or dates in insertion order rather than a user-friendly alphabetical or numerical order. Luckily for us, Perl makes it simple for us to sort an entire list alphabetically.</p>
<p><em>my @animals = (&#8220;frog&#8221;, &#8220;toad&#8221;, &#8220;snake&#8221;, &#8220;grasshopper&#8221;, &#8220;bird&#8221;);<br />
print join(&#8221; &#8220;, sort @animals);</em></p>
<p>sort (@array) will take the array and put it in alpha-numerical order for you. Using sort(@array) on our animals, we would get back : bird, frog, grasshopper, snake, toad. If we had numbers (&#8220;1&#8243;, &#8220;3&#8243;, &#8220;5&#8243;) in with our animals, the numbers would print before the animals.<br />
<strong>Deleting</strong></p>
<p>Sometimes you will want to remove a certain element from your list. This can be done using delete. To remove a single piece of information we need to treat the list as a scalar.</p>
<p><em>my @animals = (&#8220;frog&#8221;, &#8220;toad&#8221;, &#8220;snake&#8221;);<br />
delete $animals[0];</em></p>
<p>Oh oh, it appears our newest pet frog was eaten by a snake in our garden. It&#8217;s a very sad tragedy and we will mourn deeply for the rest of our lives, but to remove our dead frog&#8217;s memory we use the delete $array[index] command..</p>
<p>If instead you wanted to delete an entire array, the quickest way would be to assign it a null value. A null value is different than an undefined value, so don&#8217;t try @animals = undef; thinking it&#8217;ll delete your array. Instead, use:</p>
<p><em>@animals = ();</em></p>
<p>The toad and the snake ran away, that&#8217;s three animals we just lost. How ever are we going to live with ourselves? Between the tears you cry, we assign our list of animals to a blank list and we have nothing else to live for <img src='http://www.hotscripts.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  .</p>
<p><strong><br />
Summary</strong></p>
<p>Arrays can be a very useful way for you to reuse information from a list. You have learned how to create an array, read information from an entire set of values and how to pick apart just pieces of information. You have also learned how to merge more than one list together to form a single and larger list. Using the techniques we went over, you should now be able to determine how many indexes you have and know how to delete and/or store certain indexes into a new array all by itself.</p>
<div class="hotlist"><strong>Author: Syperder Co</strong><br />
I waltzed into the Web Design community as a professional when I was just 15 years of age founding <a href="http://www.spyderscripts.net/">SpyderWebDesigns</a>. Through the years my interests shifted from web development to backbone and user interaction.In 2000 Sulfericacid.com was born. The world&#8217;s largest free and 100% ad-free web site where you could use and download 24 Perl and CGI script along with tutorials without limits or restrictions. January 2005 the site was renamed to SpyderScripts.com as a subsidy of SpyderCo.</p>
<p>In 2001 I also founded an SEO company SpyderSubmission.com. We&#8217;ve helped nearly 2300 web sites achieve higher rankings than they ever could have imagined since our launch four years ago.</p>
<p>On a more personal note, I&#8217;ve attained 28 certifications from BrainBench.com and about 40 certifications in total from all resources. One of these is a near Masters in Perl which ranks second highest test score in the state and 17th throughout the country.</p>
<p>I have a Perl Abbot status on PerlMonks.org working on getting my Perl Saint status this fall.</p></div>
<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/arrays-perl/">Arrays in Perl</a></p>
<p>Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/working-with-arrays-in-php/' rel='bookmark' title='Working with Arrays in PHP'>Working with Arrays in PHP</a></li>
<li><a href='http://www.hotscripts.com/blog/loops-in-perl-pt-1/' rel='bookmark' title='Loops in Perl &#8211; Pt. 1'>Loops in Perl &#8211; Pt. 1</a></li>
<li><a href='http://www.hotscripts.com/blog/randomness-in-perl/' rel='bookmark' title='Randomness In Perl'>Randomness In Perl</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.hotscripts.com/blog/arrays-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to Perl</title>
		<link>http://www.hotscripts.com/blog/introduction-perl/</link>
		<comments>http://www.hotscripts.com/blog/introduction-perl/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 03:01:24 +0000</pubDate>
		<dc:creator>DEVpapers</dc:creator>
				<category><![CDATA[CGI & Perl]]></category>

		<guid isPermaLink="false">http://www.hotscripts.com/blog/?p=668</guid>
		<description><![CDATA[This is an introduction to the Perl language. Learn what it does, what it can and can't be used for, what you need to have to use it, and more.<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/introduction-perl/">Introduction to Perl</a></p>

Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/randomness-in-perl/' rel='bookmark' title='Randomness In Perl'>Randomness In Perl</a></li>
<li><a href='http://www.hotscripts.com/blog/scalars-in-perl/' rel='bookmark' title='Scalars in Perl'>Scalars in Perl</a></li>
<li><a href='http://www.hotscripts.com/blog/loops-in-perl-pt-1/' rel='bookmark' title='Loops in Perl &#8211; Pt. 1'>Loops in Perl &#8211; Pt. 1</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div><span><span style="font-family: Verdana;"><strong>What is it?<br />
</strong><br />
Perl was one of the first computer languages every written, through it&#8217;s years of existance the contributers of the language have made it one of the the most robust and powerful languages there are.  Perl is short for Practical Extraction Report Language, because that&#8217;s what it is.  It creates and extracts data on the fly with remarkable speed.</span></p>
<p><span style="font-family: Verdana;">You will often times hear the words Perl and CGI together as if they&#8217;re one and the same.  It&#8217;s safe to think this, as CGI is a module built for Perl, the main difference is CGI is typically OOP (object-oriented programming) while Perl itself usually is not.  CGI is short for Common Gateway Interface which is a great name because it&#8217;s Perl&#8217;s interface to work on the web.</span></p>
<p><span style="font-family: Verdana;"><strong>What does it do?<br />
</strong><br />
Since this is one of the fastest languages there are, this makes a great report processing and retrieval language.  You can parse hundreds of thousands of lines of code from a text file in just a few seconds (I typically can get around 80,000 lines returned in a fraction of a second).</span></p>
<p><span style="font-family: Verdana;">Some very common uses for Perl/CGI: Data extraction, encryption, contact forms, guestbooks, full ecommerce sites, web parsers and web bots, search engines, image manipulation, security.</span></p>
<p><span style="font-family: Verdana;"><strong>What can&#8217;t it do?</strong></span></p>
<p><strong> </strong>It can do nearly anything you can think of when working in strictly Perl on your own computer.  You can create graphics, setup password scripts, encrypt files, track users, etc.  But on the web, it&#8217;s only limitation is the fact it&#8217;s a server-side language.</p>
<p></span></p>
<p><span style="font-family: Verdana;">This means that the content created using Perl/CGI is not dymanic; all of it&#8217;s work is done at the time the page loads.  This is unlike JavaScript which can have content change at any given time.  A simple example of this would be a contact form.  The fields can&#8217;t be checked or verified on the page that just loaded, the page has to reload or redirect before Perl/CGI can use this information.</span></p>
<p><span style="font-family: Verdana;"><strong>What do I need?<br />
</strong><br />
If you want to run Perl scripts on your own computer and you&#8217;re on Windows or Mac, you&#8217;ll need to download ActiveState Perl.  The link for this can be found in Tutorials section, be sure to always download the latest version.  At the time of writing this article, the latest version is 5.8.6.</span></p>
<p><span style="font-family: Verdana;">To use CGI, you&#8217;ll either need your own server or a server hosted by another company online.  Your server or web host must allow CGI support. If it doesn&#8217;t, you will not be able to use web based Perl scripts.  There are many free servers out there that do allow CGI and nearly all paid hosts will.  CGI is a standard which makes finding a worthy host a much simpler task.</span></div>
<div class="hotlist"><strong>Author: Syperder Co</strong><br />
I waltzed into the Web Design community as a professional when I was just 15 years of age founding <a href="http://www.spyderscripts.net/">SpyderWebDesigns</a>. Through the years my interests shifted from web development to backbone and user interaction.In 2000 Sulfericacid.com was born. The world&#8217;s largest free and 100% ad-free web site where you could use and download 24 Perl and CGI script along with tutorials without limits or restrictions. January 2005 the site was renamed to SpyderScripts.com as a subsidy of SpyderCo.</p>
<p>In 2001 I also founded an SEO company SpyderSubmission.com. We&#8217;ve helped nearly 2300 web sites achieve higher rankings than they ever could have imagined since our launch four years ago.</p>
<p>On a more personal note, I&#8217;ve attained 28 certifications from BrainBench.com and about 40 certifications in total from all resources. One of these is a near Masters in Perl which ranks second highest test score in the state and 17th throughout the country.</p>
<p>I have a Perl Abbot status on PerlMonks.org working on getting my Perl Saint status this fall.</p></div>
<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/introduction-perl/">Introduction to Perl</a></p>
<p>Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/randomness-in-perl/' rel='bookmark' title='Randomness In Perl'>Randomness In Perl</a></li>
<li><a href='http://www.hotscripts.com/blog/scalars-in-perl/' rel='bookmark' title='Scalars in Perl'>Scalars in Perl</a></li>
<li><a href='http://www.hotscripts.com/blog/loops-in-perl-pt-1/' rel='bookmark' title='Loops in Perl &#8211; Pt. 1'>Loops in Perl &#8211; Pt. 1</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.hotscripts.com/blog/introduction-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CGI Developer&#8217;s Guide</title>
		<link>http://www.hotscripts.com/blog/cgi-developers-guide/</link>
		<comments>http://www.hotscripts.com/blog/cgi-developers-guide/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 03:01:22 +0000</pubDate>
		<dc:creator>DEVpapers</dc:creator>
				<category><![CDATA[CGI & Perl]]></category>

		<guid isPermaLink="false">http://www.hotscripts.com/blog/?p=685</guid>
		<description><![CDATA[An introduction to the Common Gateway Interface (CGI)<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/cgi-developers-guide/">CGI Developer&#8217;s Guide</a></p>

Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/what-is-cgi/' rel='bookmark' title='What is CGI?'>What is CGI?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div><span><span style="font-family: Verdana; ">Thanks to the World Wide Web, almost anyone can provide information on the Internet in a visually pleasing and widely distributable manner. You have undoubtedly navigated the Web and have looked at other people&#8217;s sites, and you now probably know that intimidating acronyms such as &#8220;HTTP&#8221; and &#8220;HTML&#8221; are simply fancy acronyms for &#8220;Web&#8221; and &#8220;way to express information on the Web.&#8221; Perhaps you have some experience providing information over the Web as well. </span></p>
<p><span style="font-family: Verdana; ">The Web has proven to be an ideal medium for distributing information as can be seen from its immense popularity and exponential growth. Although some have questioned the Web&#8217;s utility and attributed its growth and popularity mostly to media hype, the Web is unquestionably an important means of providing all sorts of information. Not only are many up-to-the-minute news services (providing real-time news, weather, and sports) and reference materials available electronically, vast amounts of other types of data exist as well. The Internal Revenue Service, which made all of its 1995 tax forms and other information available over the World Wide Web, recently remarked that it was actually receiving fan mail for its Web site. Who would have thought that the IRS could ever receive fan mail for anything? It was not because its site was good-looking, but because it was a genuinely useful tool for thousands, perhaps millions, of people. </span></p>
<p><span style="font-family: Verdana; ">What makes the Web unique and so appealing as an information server? First, it provides a hypermedia interface to data. Think about the hard disk drive on your own <a id="KonaLink0" style="text-decoration: underline ! important; position: static;" href="http://www.devpapers.com/#" target="undefined"><span style="color: blue ! important; font-weight: 400; font-size: 13px; position: static;"><span style="color: blue ! important; font-family: Verdana; font-weight: 400; font-size: 13px; position: static;">computer</span></span></a>. Typically, data has been expressed in a linear fashion analogous to a filing system. For example, you have a bunch of folders, and within each folder, you either have documents or more folders.  The Web uses a different paradigm for expressing information called hypermedia. A hypertext interface consists of a document and links. <em>Links</em> are words on which you can click to see other documents or retrieve other types of information.  The Web extends the concept of hypertext to include other types of media such as graphics, sounds, and video (hence the name &#8220;hypermedia&#8221;). Selecting text or graphics on a document enables you to see related information in any number of forms about the item you selected.<br />
</span><span style="font-family: Verdana; "><br />
Almost every type of person benefits from this easy and unique way of representing and distributing information, from academics who want to immediately share data with their peers to business people who want to offer information about their company to anyone who is curious. However, although giving information is extremely important, over the past few years, many have realized that receiving information is just as important. </span></p>
<p><span style="font-family: Verdana; ">Although the Web provides a unique, hypermedia interface to information, there are many other effective ways to distribute data. For example, network services such as the File Transfer Protocol (FTP) and gopher existed long before the World Wide Web. E-mail has been the primary medium for communicating and exchanging information over the Internet and most other networks almost since the inception of these networks. Why did the Web become such a popular way to distribute information? The multimedia aspect of the Web clearly contributed to its wild success, but in order for the Web to become most effective, it had to be interactive. </span></p>
<p><span style="font-family: Verdana; ">Without the capability to receive input from users as well as provide information, the Web would be a completely static medium. Information would be available only in a format defined by the author. This seems to undermine one of the powers of computing in general: interactive information. For example, instead of forcing a user to browse through several documents as if he or she were flipping through a book or a dictionary, it would be better to let the user specify the keywords of the topic in which he or she is interested. Users can customize the presentation of the data rather than rely on a rigid structure defined by the content provider. </span></p>
<p><span style="font-family: Verdana; ">The term <em>Web server</em> can be confusing because it can refer to either the physical machine or the software running on it that makes it interact with Web browsers. When a browser queries a given Web address, it first makes a connection to the machine over the Internet, submitting the request for a document to the Web server software. This software runs constantly, waiting for such requests to come in and responding appropriately. </span></p>
<p><span style="font-family: Verdana; ">Although Web servers can send and receive data, the server itself has limited functionality. For example, the most basic Web server can only send the requested file to the browser. The server normally does not know what to do with any additional input. Unless the Web provider tells the server how to handle that additional information, the server most likely ignores the input. </span></p>
<p><span style="font-family: Verdana; ">In order for the server to do anything more advanced than retrieving and sending files to the Web browser, you must know how to extend the functionality of the Web server. For example, a Web server cannot search a database based on a keyword entered by a user and return several matching documents unless you have somehow programmed that capability into the server. </span></p>
<h2 id="toc-what-is-cgi"><a name="WhatIsCGI"><span style="font-family: Verdana; color: #ff0000; ">What Is CGI?</span></a><span style="font-family: Verdana; "> </span></h2>
<p><span style="font-family: Verdana; ">The Common Gateway Interface (CGI) is an interface to the Web server that enables you to extend the server&#8217;s functionality. Using CGI, you can interact with users who access your site. On a theoretical level, CGI enables you to extend the capability of your server to parse (interpret) input from the browser and return information based on user input. On a practical level, CGI is an interface that enables the programmer to write programs that can easily communicate with the server. </span></p>
<p><span style="font-family: Verdana; ">Normally, if you wanted to extend the Web server&#8217;s capabilities, you would have to modify the server yourself. This is an undesirable solution because it requires a low-level understanding of network programming over the Internet and the World Wide Web protocol. It would also require editing and recompiling the server source code or writing a custom server for each task. For example, suppose you want to extend your server to act as a Web-to-e-mail gateway that would take user input from the browser and e-mail it to another user. You would have to insert code into the server that would parse the input from the browser, e-mail the input to the other user, and send a response back to the browser over a network connection. </span></p>
<p><span style="font-family: Verdana; ">First, such a task requires having access to the server code, something that is not always possible. Second, it is difficult and requires extensive technical knowledge. Third, it works only for your specific server. If you want to move your Web server to a different platform, you would have to start over or at least spend a lot of time porting the code to that platform. </span></p>
<h2 id="toc-why-cgi"><a name="WhyCGI"><span style="font-family: Verdana; color: #ff0000; ">Why CGI?</span></a><span style="font-family: Verdana; "> </span></h2>
<p><span style="font-family: Verdana; ">CGI provides a portable and simple solution to these problems. The CGI protocol defines a standard way for programs to communicate with the Web server. Without much special knowledge, you can write a program in any computer language that interfaces and communicates with the Web server. This program will work with all Web servers that understand the CGI protocol. </span></p>
<p><span style="font-family: Verdana; ">CGI communication is handled over the standard input and output, which means that if you know how to print and read data using your programming language, you can write a Web server application. Other than parsing the input and output, programming CGI applications is almost equivalent to programming any other application. For example, if you want to program a &#8220;Hello, world!&#8221; program, you use your language&#8217;s print functions and the format defined for CGI programs to print the proper message. </span></p>
<h2 id="toc-choosing-your-language"><a name="ChoosingYourLanguage"><span style="font-family: Verdana; color: #ff0000; ">Choosing Your Language</span></a></h2>
<p><span style="font-family: Verdana; ">Because CGI is a &#8220;common interface,&#8221; you are not restricted to any specific computer language. An important question many people ask is what programming languages can you use to program CGI? You can use any language that can do the following: </span></p>
<ul>
<li><span style="font-family: Verdana; ">Print to the standard output </span></li>
<li><span style="font-family: Verdana; ">Read from the standard input </span></li>
<li><span style="font-family: Verdana; ">Read from environment variables </span></li>
</ul>
<p><span style="font-family: Verdana; ">Almost all programming languages and many scripting languages perform these three activities, and you can use any one of them. </span></p>
<p><span style="font-family: Verdana; ">Languages fall under one of the following two classes: compiled or interpreted. A compiled language-such as C or C++-tends to be smaller and faster, whereas interpreted languages-such as Perl or Rexx-require loading a sometimes large interpreter upon startup. Additionally, you can distribute binaries (code compiled into machine language) without source code if your language is compiled. Distributing interpreted scripts normally means distributing the source code. </span></p>
<p><span style="font-family: Verdana; ">Before you choose your language, you must first consider your priorities. You need to balance the speed and efficiency gains of one programming language versus the ease of programming in another. If you think you want to learn another language rather than use one you already know, carefully weigh the advantages and disadvantages of the two languages. </span></p>
<p><span style="font-family: Verdana; ">Perhaps the two most commonly used languages for CGI programming are C and Perl (both of which are covered in this book). Both have their own distinct advantages and disadvantages. Perl is a very high-level yet powerful language especially useful for parsing text. Although its ease of use, flexibility, and power make it an attractive language for CGI programming, its relatively large size and slower performance sometimes makes it unsuitable for certain applications. C programs are smaller, more efficient, and offer more low-level control over the system, and yet are more difficult to program, do not have easy built-in text processing routines, and are more difficult to debug. </span></p>
<p><span style="font-family: Verdana; ">Which language is the superior CGI programming language? Whichever language you are most comfortable programming. Both are just as effective for programming CGI applications, and with the proper libraries, both have similar capabilities. However, if you have a heavily accessed server, you might want to use smaller compiled C programs. If you need to quickly write an application that requires a lot of text processing, you might want to use Perl instead. </span></p>
<h2 id="toc-caveats"><a name="Caveats"><span style="font-family: Verdana; color: #ff0000; ">Caveats</span></a><span style="font-family: Verdana; "> </span></h2>
<p><span style="font-family: Verdana; ">There are some important alternatives to CGI applications. Many servers now include a programming API that makes it easier to program direct extensions to the server as opposed to separate CGI applications. Server APIs tend to be more efficient than CGI programs. Other servers include built-in functionality that can handle special features without CGI such as database interfacing. Finally, some applications can be handled by some new client-side (rather than server-side) technologies such as Java. With such rapid change in technology, is CGI rapidly becoming obsolete? </span></p>
<p><span style="font-family: Verdana; ">Probably not. CGI has several advantages over the newer technologies. </span></p>
<ul>
<li><span style="font-family: Verdana; ">It is &#8220;common,&#8221; or portable. You can write a CGI application using almost any programming language on any platform. Some of the alternatives such as server APIs restrict you to certain languages and are much more difficult to learn. </span></li>
<li><span style="font-family: Verdana; ">Client-side technologies such as Java aren&#8217;t likely to replace CGI because there are certain applications that server-side applications are much better suited to perform. </span></li>
<li><span style="font-family: Verdana; ">Many of the limitations of CGI are limitations of HTML or HTTP. As the standards for the Web in general evolve, so does the capability of CGI. </span></li>
</ul>
<h2 id="toc-summary"><a name="Summary"><span style="font-family: Verdana; color: #ff0000; ">Summary</span></a><span style="font-family: Verdana; "> </span></h2>
<p><span style="font-family: Verdana; ">The Common Gateway Interface is the protocol by which programs interact with Web servers. The versatility of CGI gives programmers the opportunity to write gateway programs in almost any language, although there are many trade-offs associated with different languages. Without this ability, making interactive Web pages would be difficult at best, requiring modifications to the server and putting interactivity out of the reach of most programmers who are not also site administrators. </span></p>
<p><span style="font-family: Verdana; ">More At: </span><a href="http://www.webbasedprogramming.com/CGI-Developers-Guide/"><span style="font-family: Verdana; ">http://www.webbasedprogramming.com/CGI-Developers-Guide/</span></a></p>
<p></span></div>
<div class="hotlist"><strong>About the Author: TheBrain Cell</strong> of <a href="http://www.webbasedprogramming.com/">webbasedprogramming.com</a></div>
<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/cgi-developers-guide/">CGI Developer&#8217;s Guide</a></p>
<p>Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/what-is-cgi/' rel='bookmark' title='What is CGI?'>What is CGI?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.hotscripts.com/blog/cgi-developers-guide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Useful Perl Scripts With Regular Expressions</title>
		<link>http://www.hotscripts.com/blog/useful-perl-scripts-with-regular-expressions/</link>
		<comments>http://www.hotscripts.com/blog/useful-perl-scripts-with-regular-expressions/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 03:01:22 +0000</pubDate>
		<dc:creator>DEVpapers</dc:creator>
				<category><![CDATA[CGI & Perl]]></category>

		<guid isPermaLink="false">http://www.hotscripts.com/blog/?p=687</guid>
		<description><![CDATA[Many people talk about Perl and many more about regular expressions but unless you are a programmer you probably never use either. We will discuss a few unique and very useful ways to use both of them.<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/useful-perl-scripts-with-regular-expressions/">Useful Perl Scripts With Regular Expressions</a></p>

Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/pattern-matching-regular-expressions/' rel='bookmark' title='Pattern matching with Regular Expressions'>Pattern matching with Regular Expressions</a></li>
<li><a href='http://www.hotscripts.com/blog/regex-operators-in-perl/' rel='bookmark' title='Regex Operators in Perl'>Regex Operators in Perl</a></li>
<li><a href='http://www.hotscripts.com/blog/hashes-in-perl/' rel='bookmark' title='Hashes in Perl'>Hashes in Perl</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><span style="font-family: Verdana; "><strong>Introduction</strong></span></p>
<p><span style="font-family: Verdana; ">Most computer users, especially software engineers, have had a need to modify multiple files to either add a line of text, modify a line of text, or completely remove a line of text. The problem is that there never seems to be a piece of software out there that can help you with this problem. Some programs let you get close to doing what you want but in my experience none ever let you do exactly what you want; so a few hours are spent opening each file and editing them manually.</span></p>
<p><span style="font-family: Verdana; ">Well I finally had enough and I wrote a couple little Perl scripts that use regular expressions to edit only the selected file types. The script will modify, add, or remove any text you would want. Since these are Perl scripts you cannot just type what you want to do into a pretty text box and have the program do it; rather you will have to make some small modifications to the files in order for it to modify the files as you would like.</span></p>
<p><span style="font-family: Verdana; ">We will walk though how to modify the Perl script to make it do a few different things that most people would want and then discuss other possible uses for the scripts. We will even discuss how to get the script to traverse directories since that is usually where the biggest issues arise when needing to modify multiple files.</span></p>
<p><span style="font-family: Verdana; ">Beyond just being able to edit this script so that you can use it to parse and edit your files you should learn a little bit about regular expressions which is always something that people dislike. Most programmers that have used regular expressions but have run into issues where regular expressions just grab more than you want them to. In most languages regular expressions do a maximum munch, meaning they take as much as possible instead of stopping at the first spot where stopping would be permitted by the expression. We will discuss how to make the regular expression grab a smaller portion because this will help when we want to modify simple HTML pages and more specifically maybe a &lt;body&gt; tag that has attributes in it.</span></p>
<p><span style="font-family: Verdana; "><strong>Open A File Using Perl</strong></span></p>
<p><span style="font-family: Verdana; ">In Perl there are a lot of ways to do the same thing so if you open your files differently then feel free to continue to do so but we need to discuss how to open files for those who are just reading this to quickly get a rather large number of files modified.</span></p>
<p><span style="font-family: Verdana; color: #ff4500; ">open ( FILE, &#8220;/home/directory/file.txt&#8221; ) or<br />
die &#8220;Cannot open file: $!&#8221;;</span></p>
<p><span style="font-family: Verdana; ">In the code above we see an example of opening a file located in &#8220;/home/directory/file.txt&#8221;, which is a Unix directory structure. To make this work on a windows machine you just have to put in a windows path like &#8220;C:\\my documents\\file.txt&#8221;. While it might seem funny to see the double slashes they are required because a single slash is put in front of special characters so in order for Perl to read a single slash in a string you need to do double slashes. You could always use a forward slash instead of a black slash too and then you would only need one slash but lets not get confused here.</span></p>
<p><span style="font-family: Verdana; ">There is one more type of open that we will want to do and that is an open where we are allowed to then write to the file. While it might seem odd to have to open a file differently when you want to write to it there is a good reason for it. You do not want to open a file that is meant just to be read from and then start writing to it by accident in your code. Since Perl requires you to open the file a little differently as we can see in the code sample below, there will be no chance of us opening a file we only want to read from and then writing to it by accident.</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">open ( OUTFILE, &#8220;&gt;/home/directory/file.txt&#8221;) or<br />
die &#8220;Cannot open file: $!&#8221;;&lt;BR&gt;</span><br />
<span style="font-family: Verdana; ">As you can see all we did was add a greater than sign (&gt;) in front of the path name to make it so we can write to the file. It is very simple to do but again by leaving out the greater than sign we make it so we cannot write to the file and will probably end up saving ourselves from over writing files that we forgot to backup and will take hours to recreate.</span></p>
<p><span style="font-family: Verdana; ">Being that we are good programmers we have to close the file once we are done with it so that we do not run into issues and cause the file to become corrupted. To close the file is simple as can be seen in the code below.</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">close FILE;</span></p>
<p><span style="font-family: Verdana; "><strong>Replace On A Single File<br />
</strong><br />
Here we will hard code our script to edit a single file for certain words. You could set the script up to prompt the user for the file but I figured that was over kill since we would probably have to go in and make some minor changes to the script anyway. If you want the script to prompt you can use the code below though.<br />
</span><br />
<span style="font-family: Courier New; color: #ff4500; ">my $dir;<br />
print &#8220;Please enter dir name: &#8220;;<br />
chomp ( $dir = &lt;STDIN&gt; );<br />
print $dir;</span></p>
<p><span style="font-family: Verdana; ">The code above prints out Please enter dir name: and then we use chomp, which we will discuss later, to remove the linefeed and or newline that comes in when the user hits enter.</span></p>
<p><span style="font-family: Verdana; ">The code below will parse a file that contains a &lt;body &#8230; &gt; tag and we want to remove all the attributes in the body tag because we are starting to use cascading style sheets (CSS) and we no longer want there to be any attributes in the body tag since we will define all of the attributes in our .css file. Our file will start out with &lt;body bgcolor=&#8221;green&#8221;&gt; and we will end up with &lt;body&gt;.</span></p>
<p><span style="font-family: Verdana; ">It would probably not make much sense to use this script to edit just one file because it would be faster to open the HTML document and make the change manually than it would be to edit this script and then run it. We will be building upon this script so it is not a waste of time.</span><br />
<span style="font-family: Courier New; color: #ff4500; ">#!/usr/bin/perl</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">$filename = &#8220;/home/directory/file.txt&#8221;;<br />
open ( FILE, $filename) or die &#8220;Cannot open file: $!&#8221;;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">while ( $line = &lt;FILE&gt; ) {<br />
# i is case insensative<br />
# ([^&gt;]*) match zero or more characters but not &#8216;&gt;&#8217;<br />
$line =~ s/&lt;body([^&gt;]*)&gt;/&lt;body&gt;/i;<br />
push(@outLines, $line);<br />
}</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">close FILE;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">open ( OUTFILE, &#8220;&gt;$filename&#8221; );<br />
print ( OUTFILE @outLines );<br />
close ( OUTFILE );</span></p>
<p><span style="font-family: Verdana; ">You will notice above that we open the file once to read it and then we open the file again to write to it. The reason I did this is because it is possible you would actually output to a different file than the original and if that is the case then the code already exists for you to do so easier than if I had only done this process with one open file statement.</span></p>
<p><span style="font-family: Verdana; ">In the above code you will also notice the regular expression ([^&gt;]*) which stops the regular expression from doing a maximum munch; i.e. it tells the regular expression to stop at the first greater than sign instead of stopping at the last greater than sign in the file. If this were not here, and you can feel free to give this a try, the regular expression code would actually take everything from the body tag all the way to the last greater sign removing everything, in a well formatted HTML document, from the body tag to the closing html tag and replace it will just a simple &lt;body&gt; tag.</span></p>
<p><span style="font-family: Verdana; "><strong>Replace On Multiple Files</strong></span></p>
<p><strong></strong>We are going to use <a href="http://www.perldoc.com/perl5.8.0/lib/File/Find.html" target="_blank">File::Find</a>, a <a href="http://www.perldoc.com/perl5.8.0/lib.html" target="_blank">Perl Module</a>, to parse all the files in a directory and it&#8217;s subdirectories. This module will work on Unix and Windows machines as well as Mac OS machines but Mac users will want to consult the File::Find documents to see a few of the issues that Mac&#8217;s have with it and their work around.</p>
<p><span style="font-family: Verdana; ">This code below will traverse directories but not symbolic links. This means that if there is a real subdirectory in the directory that you tell it to run on then this script will parse all the files in that subdirectory and all the subdirectories but will not follow symbolic links. You can make it follow symbolic links by using the follow attribute but you will want to read the documentation on that.</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">#!/usr/bin/perl</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">use File::Find;<br />
use strict;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">my $directory = &#8220;/home/directory&#8221;;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">find (\&amp;process, $directory);</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">sub process<br />
{<br />
my @outLines;  #Data we are going to output<br />
my $line;      #Data we are reading line by line</span></p>
<p><span style="font-family: Courier New; color: #ff4500; "> #  print &#8220;processing $_ / $File::Find::name\n&#8221;;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; "> # Only parse files that end in .html<br />
if ( $File::Find::name =~ /\.html$/ ) {</span></p>
<p><span style="font-family: Courier New; color: #ff4500; "> open (FILE, $File::Find::name ) or<br />
die &#8220;Cannot open file: $!&#8221;;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; "> print &#8220;\n&#8221; . $File::Find::name . &#8220;\n&#8221;;<br />
while ( $line = &lt;FILE&gt; ) {<br />
$line =~ s/&lt;body([^&gt;]*)&gt;/&lt;body&gt;/i;<br />
push(@outLines, $line);<br />
}<br />
close FILE;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; "> open ( OUTFILE, &#8220;&gt;$File::Find::name&#8221; ) or<br />
die &#8220;Cannot open file: $!&#8221;;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; "> print ( OUTFILE @outLines );<br />
close ( OUTFILE );</span></p>
<p>undef( @outLines );<br />
}<br />
}</p>
<p><span style="font-family: Verdana; ">In the code above we first start out by doing a use File::Find; which allows us to use the find function. We then define my $directory and set it to the path of the directory we want to parse. The last thing we do in the main part of the code is to call the find function which we need to pass the address off the processing function, this is a call back function that will be called with each file and directory found within the main directory. The second argument is the actual directory or directories we want to use.</span></p>
<p><span style="font-family: Verdana; ">The most complex part of this script is the actual processing subroutine which is called with each file and directory found within the main directory. There is no way to tell find to only select certain types of files so this means that our processing code will even try to run on directories and if we try to open a directory, at least in windows, the script will crash. Also we do not want to be parsing image files or other binary files for the body tag first because we could certainly mess them up and secondly we do not want to change them.</span></p>
<p><span style="font-family: Verdana; ">Since we know we only want to parse HTML documents and change the body tags we can easily just add an if statement that says if the file ends with .html then lets parse the file. From here, since we know we have an html file, we open the file and then search the whole file for the body tag. When we find the body tag we replace the body tag and keep searching. To be more efficient we could have stopped our searching but I will leave that little modification up to you.</span></p>
<p><span style="font-family: Verdana; ">The next thing we do is close the current file and then reopen the file in write mode. We then write everything out, if we made a change or not, to the out file. We then clean up by closing the output file and we do an undef, just to be clean, on the @outLines, which is the array that olds all the data we are going to write out.</span></p>
<p><span style="font-family: Verdana; "><strong>Converting From Unix Files To Windows Files<br />
</strong><br />
Lots of people seem to be moving from the Windows word to the Unix word or from the Unix word to the Linux word or maybe from one operating system to another and in between often for many reasons. The problem is that some operating systems do a newline (unix), others do a linefeed + newline (windows), and yet others just do a linefeed (Mac prior to OS X). So when moving files between these operating systems there can be some issues and some weird characters show up and you might not know why. </span></p>
<p><span style="font-family: Verdana; ">The previous script is easily modified to remove the line termination string and add in a new line termination string. If you are moving from a Unix system to a Windows based system you would want to remove all the \n&#8217;s and convert it to windows by adding back on \r\n. This would allow the file to be read in Windows based applications like notepad. If you have ever opened a file in notepad before and saw everything on one line with weird boxes that is because the lines are not terminated correctly and notepad is confused.</span></p>
<p><span style="font-family: Verdana; ">There is a program out there called <a href="http://ccrma-www.stanford.edu/%7Ecraig/utility/flip/" target="_blank">flip</a> that can convert a single file at a time to but when needing to do many files and files in subdirectories it is not as easy to use.</span></p>
<p><span style="font-family: Verdana; ">The code below will go though each line and chomp the line which will remove the terminators at the end, be it \n in unix or \r\n in Windows or just \r on the Mac. We then go in and add in the line terminators that we want to add in. Please note I did not get to test chomp on Windows or Mac so I am assuming that chomp does what I said above without testing. If it does not work please let me know and you can easily just do a replace. I did it with a chomp because it seemed like it would be a lot cleaner code.</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">#!/usr/bin/perl</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">use File::Find;<br />
use strict;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">my $directory = &#8220;/home/directory/&#8221;;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">find (\&amp;process, $directory);</span></p>
<p><span style="font-family: Courier New; color: #ff4500; ">sub process<br />
{<br />
my @outLines;  #Data we are going to output<br />
my $line;      #Data we are reading line by line</span></p>
<p><span style="font-family: Courier New; color: #ff4500; "> #  print &#8220;processing $_ / $File::Find::name\n&#8221;;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; "> # Only parse files that end in .html<br />
if ( $File::Find::name =~ /\.html$/ ) {</span></p>
<p><span style="font-family: Courier New; color: #ff4500; "> open (FILE, $File::Find::name ) or<br />
die &#8220;Cannot open file: $!&#8221;;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; "> print &#8220;\n&#8221; . $File::Find::name . &#8220;\n&#8221;;<br />
while ( $line = &lt;FILE&gt; ) {<br />
chomp ( $line );<br />
push(@outLines, $line . &#8220;\r\n&#8221;);<br />
}<br />
close FILE;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; "> open ( OUTFILE, &#8220;&gt;$File::Find::name&#8221; ) or<br />
die &#8220;Cannot open file: $!&#8221;;</span></p>
<p><span style="font-family: Courier New; color: #ff4500; "> print ( OUTFILE @outLines );<br />
close ( OUTFILE );</span></p>
<p>undef( @outLines );<br />
}<br />
}</p>
<p><span style="font-family: Verdana; ">The code above is just like the code we used to change the body tag so it should be pretty straight forward. I have used scripts like this often to be able to move code that was created on a Unix machine to a Windows machine or the other way around. I have also used this code to move major things like <a href="http://www.perforce.com/" target="_blank">perforce</a> or <a href="http://www.gnu.org/software/cvs/" target="_blank">cvs</a> versioning files from one operating system to another so hopefully this serves to be as useful to you as it has to me.</span></p>
<p><span style="font-family: Verdana; "><strong>Summary</strong></span></p>
<p>The first thing that you should have gotten from this tutorial is how to stop a regular expression from grabbing more information than you wanted it to. There are many regular expression tutorials out there but many over look this so hopefully you now have some idea of how to make a regular expression only grab the exact amount of information you want it to grab and nothing more.</p>
<p><span style="font-family: Verdana; ">The other important thing you should have gathered from this article is how to use File::Find to parse all files in a directory and its subdirectories. The File::Find module works on most operating systems so it is much better than doing a system call to ls or dir which is what most people did before Find::Find was created.</span></p>
<div class="hotlist"><strong>About the Author:Matthew Drouin</strong><br />
Matthew Drouin is a published author with his book entitled Web Hosting and Web Site Development: A Guide to Opportunities. A graduate of the University of Hartford; Matthew received a BS in Computer Sciense and then started to chase all the dot com dreams. His love for many different programming languages has recently lead him to start spending the majority of his time writing open source tutorials which are published on his site <a href="http://www.opensourcetutorials.com/">OpenSourceTutorials.com</a></div>
<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/useful-perl-scripts-with-regular-expressions/">Useful Perl Scripts With Regular Expressions</a></p>
<p>Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/pattern-matching-regular-expressions/' rel='bookmark' title='Pattern matching with Regular Expressions'>Pattern matching with Regular Expressions</a></li>
<li><a href='http://www.hotscripts.com/blog/regex-operators-in-perl/' rel='bookmark' title='Regex Operators in Perl'>Regex Operators in Perl</a></li>
<li><a href='http://www.hotscripts.com/blog/hashes-in-perl/' rel='bookmark' title='Hashes in Perl'>Hashes in Perl</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.hotscripts.com/blog/useful-perl-scripts-with-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding a string with preg_match()</title>
		<link>http://www.hotscripts.com/blog/finding-a-string-with-preg_match/</link>
		<comments>http://www.hotscripts.com/blog/finding-a-string-with-preg_match/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 03:00:53 +0000</pubDate>
		<dc:creator>DEVpapers</dc:creator>
				<category><![CDATA[CGI & Perl]]></category>

		<guid isPermaLink="false">http://www.hotscripts.com/blog/?p=722</guid>
		<description><![CDATA[preg_match() is one of the Perl Compatible Regular Expression (PCRE) functions. This functions give you more power when dealing with string manipulation than normal string functions. preg_match() is used to find one string of text within another, making use of regular expressions. <p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/finding-a-string-with-preg_match/">Finding a string with preg_match()</a></p>

Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/working-with-arrays-in-php/' rel='bookmark' title='Working with Arrays in PHP'>Working with Arrays in PHP</a></li>
<li><a href='http://www.hotscripts.com/blog/regex-operators-in-perl/' rel='bookmark' title='Regex Operators in Perl'>Regex Operators in Perl</a></li>
<li><a href='http://www.hotscripts.com/blog/pattern-matching-regular-expressions/' rel='bookmark' title='Pattern matching with Regular Expressions'>Pattern matching with Regular Expressions</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div><span><span style="font-family: Verdana; "><span style="font-family: Courier New; color: #000000;">preg_match()</span> needs 3 arguments; a regular expression (regex), a source string and an array variable.  <span style="font-family: Courier New;">preg_match()</span> returns 1 if a match is found and 0 if no match is found.  Lets see the following example:</span></p>
<table border="0" cellspacing="2" cellpadding="2" width="100%" bgcolor="#dcdcdc">
<tbody>
<tr>
<td><span style="font-family: Courier New; color: #ff0000; ">$source = &#8220;Michael Jordan is a great player&#8221;;<br />
if ( preg_match( &#8220;/J.r/&#8221;, $source, $array ) )<br />
print $array[0]<br />
// prints Jor</span></td>
</tr>
</tbody>
</table>
<p><span style="font-family: Verdana; ">Here our regex, <span style="font-family: Courier New;">&#8220;/J.r/&#8221;</span> , is saying that we want a <span style="font-family: Courier New;">J;</span> followed by any character (denoted by the <span style="font-family: Courier New;">.);</span> followed by a r. The / are known as delimiters and as their name say delimit the regex.  Another example:</span></p>
<table border="0" cellspacing="2" cellpadding="2" width="100%" bgcolor="#dcdcdc">
<tbody>
<tr>
<td><span style="font-family: Courier New; color: #ff0000; ">$source = &#8220;Michael Jordan is a great player&#8221;;<br />
if ( preg_match( &#8220;/J.*n/&#8221;, $source, $array ) )<br />
print $array[0]<br />
// prints Jordan</span></td>
</tr>
</tbody>
</table>
<p><span style="font-family: Verdana; ">Here we are looking for a J; followed by any character (denoted by the .); followed by any amount of the previous character (denoted by the *); followed by a n.  The combination .* can also be read as “any amount of any character”. </span></p>
<p><span style="font-family: Verdana; ">Sometimes instead of * we would want to use + which means “one or more occurrences of the previous character”.  The difference between both is that * includes the case of zero occurrences of a character. </span></p>
<p><span style="font-family: Verdana; ">Regular expressions are said to be greedy because they will match everything until the last occurrence of the searched character is found.  For example:</span></p>
<table border="0" cellspacing="2" cellpadding="2" width="100%" bgcolor="#dcdcdc">
<tbody>
<tr>
<td><span style="font-family: Courier New; color: #ff0000; ">$text = &#8220;string strong stung&#8221;;<br />
if ( preg_match( &#8220;/s.*g/&#8221;, $text, $array ) )<br />
print $array[0];<br />
// prints string strong stung</span></td>
</tr>
</tbody>
</table>
<p><span style="font-family: Verdana; ">If we only want the match to include until the first occurrence of g, we should add a ?, which means “optional”</span></p>
<table border="0" cellspacing="2" cellpadding="2" width="100%" bgcolor="#dcdcdc">
<tbody>
<tr>
<td><span style=""><span style="font-family: Courier New; color: #ff0000;">$text = &#8220;string strong stung&#8221;;<br />
if ( preg_match( &#8220;/s.*?g/&#8221;, $text, $array ) )<br />
print $array[0];<br />
// prints string</span><span style="font-family: Verdana;"> </span></span></td>
</tr>
</tbody>
</table>
<p><span style="font-family: Verdana; ">We can also make use of generic character types to match only certain characters.  For example:</span></p>
<table border="0" cellspacing="2" cellpadding="2" width="100%" bgcolor="#dcdcdc">
<tbody>
<tr>
<td><span style="font-family: Courier New; color: #ff0000; ">$text = &#8220;Today is 10-15-03&#8243;;<br />
if ( preg_match( &#8220;/\d*-\d*-\d*/&#8221;, $text, $array ) )<br />
print $array[0]<br />
//prints 10-15-03</span></td>
</tr>
</tbody>
</table>
<p><span style="font-family: Verdana; ">On this example the generic character type <span style="font-family: Courier New;">\d</span> matches any decimal digit; it is followed by any amount of the previous character <span style="font-family: Courier New;">*;</span> followed by a <span style="font-family: Courier New;">- ;</span> followed by any decimal digit <span style="font-family: Courier New;">\d</span>; and so on.  Other generic character types are shown on the following table:</span></p>
<blockquote style="margin-right: 0px;" dir="ltr"><p><span style=""><span style="font-family: Verdana;"><strong>Character</strong> </span></span><span style=""><span style="font-family: Verdana;"><strong>Generic Character Type<br />
</strong><span style="font-family: Courier New; color: #ff0000;">\d</span> any decimal digit<br />
<span style="font-family: Courier New; color: #ff0000;">\D</span> any character that is not a decimal digit<br />
<span style="font-family: Courier New; color: #ff0000;">\s</span> any whitespace character<br />
<span style="font-family: Courier New; color: #ff0000;">\S</span> any character that is not a whitespace character<br />
<span style="font-family: Courier New; color: #ff0000;">\w</span> any &#8220;word&#8221; character<br />
<span style="font-family: Courier New; color: #ff0000;">\W</span> any &#8220;non-word&#8221; character</span></span></p></blockquote>
<p><span style="font-family: Verdana; ">You can use them to unleash the power of regular expressions.</span><span style="font-family: Verdana; "><br />
</span></p>
<p></span></div>
<div class="hotlist"><strong>About the Author:</strong>Diego Botello </div>
<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/finding-a-string-with-preg_match/">Finding a string with preg_match()</a></p>
<p>Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/working-with-arrays-in-php/' rel='bookmark' title='Working with Arrays in PHP'>Working with Arrays in PHP</a></li>
<li><a href='http://www.hotscripts.com/blog/regex-operators-in-perl/' rel='bookmark' title='Regex Operators in Perl'>Regex Operators in Perl</a></li>
<li><a href='http://www.hotscripts.com/blog/pattern-matching-regular-expressions/' rel='bookmark' title='Pattern matching with Regular Expressions'>Pattern matching with Regular Expressions</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.hotscripts.com/blog/finding-a-string-with-preg_match/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl And MySQL; Using DBI; Connections</title>
		<link>http://www.hotscripts.com/blog/perl-mysql-dbi/</link>
		<comments>http://www.hotscripts.com/blog/perl-mysql-dbi/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 03:00:53 +0000</pubDate>
		<dc:creator>Ahmad Permessur</dc:creator>
				<category><![CDATA[CGI & Perl]]></category>

		<guid isPermaLink="false">http://www.hotscripts.com/blog/?p=730</guid>
		<description><![CDATA[Perl abstracts databases using DBI, its database independent interface. Writing code using DBI is simple, for a programmer who has used Perl before, but for novices it can be a scary experience. This tutorial series aims at making the transition from flatfiles, or any other type of database to DBI a bit simpler. This article will focus on how to connect to a MySQL database, what it means, and what one can do from there.<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/perl-mysql-dbi/">Perl And MySQL; Using DBI; Connections</a></p>

Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/mysql-database-handling-in-php/' rel='bookmark' title='MySQL Database Handling in PHP'>MySQL Database Handling in PHP</a></li>
<li><a href='http://www.hotscripts.com/blog/loops-in-perl-pt-1/' rel='bookmark' title='Loops in Perl &#8211; Pt. 1'>Loops in Perl &#8211; Pt. 1</a></li>
<li><a href='http://www.hotscripts.com/blog/hot-scripts-forum-digest-sep-20th-2009/' rel='bookmark' title='Hot Scripts Forum Digest &#8211; Sep 20th 2009'>Hot Scripts Forum Digest &#8211; Sep 20th 2009</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p style="margin-right: 0px;" dir="ltr"><span style="font-family: Verdana; ">Tim Bunce, the lead developer of DBI.pm, asserts &#8216;The DBI is a database access module for the Perl programming language. It defines a set of methods, variables, and conventions that provide a consistent database interface, independent of the actual database being used. [..] The DBI is a layer of &#8216;glue&#8217; between an application and one or more database <em>driver</em> modules. It is the driver modules which do most of the real work. The DBI provides a standard interface and framework for the drivers to operate within.&#8217; (</span><a href="http://search.cpan.org/%7Etimb/DBI-1.38/DBI.pm" target="_blank"><span style="font-family: Verdana; ">http://search.cpan.org/~timb/DBI-1.38/DBI.pm</span></a><span style="font-family: Verdana; ">)</span></p>
<p><span style="font-family: Verdana; ">However, DBI attempts to make it simple for programmers by creating almost seamless integration with drivers. It&#8217;s only when databases have different specifications that one actually has to take note of which database they&#8217;re using.  The most notable differences are between transactional databases, and specific functions that may be linked to specific queries, such as fetching the index of an automatically incrementing database.  This tutorial will cover the basics of databases, assuming that you will have access to a server with Perl, MySQL, and DBI.</span></p>
<p><strong><span style="font-family: Verdana; ">Opening The Connection</span></strong></p>
<table border="0" cellspacing="2" cellpadding="2" width="100%" bgcolor="#dcdcdc">
<tbody>
<tr>
<td><span style="font-family: Courier New; color: #ff0000;"> </span><span style="font-family: Courier New; color: #ff0000; ">use DBI;<br />
$dsn      = &#8216;dbi:mysql:dbname=NameOfDatabase&#8217;;<br />
$user     = &#8216;mysqlusername&#8217;;<br />
$password = &#8216;mysqlpassword&#8217;;<br />
$dbh      = DBI-&gt;connect($dsn, $user, $password,<br />
{ RaiseError =&gt; 1, AutoCommit =&gt; 0 });</span></td>
</tr>
</tbody>
</table>
<p><span style="font-family: Verdana; ">What exactly this code mean? Obviously, you&#8217;re connecting to the database, but what are these variables standing for?</span></p>
<p><strong><span style="font-family: Verdana; ">Opening The Connection [Page 2]</span></strong></p>
<p><span style="font-family: Verdana; ">Well, we&#8217;ll begin with the basic connect statement:</span></p>
<table border="0" cellspacing="2" cellpadding="2" width="100%" bgcolor="#dcdcdc">
<tbody>
<tr>
<td><span style="font-family: Courier New; color: #ff0000; ">$dbh = DBI-&gt;connect($dsn, $user, $password,<br />
{ RaiseError =&gt; 1, AutoCommit =&gt; 0 });</span></td>
</tr>
</tbody>
</table>
<p><span style="font-family: Verdana; ">This line establishes a database connection (<span style="font-family: Courier New;">$dbh</span>, which stands for database handle), from which we&#8217;ll able to query the database in the future.  Each parameter that the connect method of the DBI module (which is actually what OO programmers call a constructor) takes is important.  The connect method takes a data source (<span style="font-family: Courier New;">$dsn</span>), MySQL username (<span style="font-family: Courier New;">$user</span>), MySQL password (<span style="font-family: Courier New;">$password</span>), and a hash reference with additional parameters, such as whether to throw errors when the driver has errors, or whether to automatically commit data for transactional databases.</span></p>
<p><strong><span style="font-family: Verdana; ">The Data Source</span></strong></p>
<table border="0" cellspacing="2" cellpadding="2" width="100%" bgcolor="#dcdcdc">
<tbody>
<tr>
<td><span style="font-family: Courier New;"><span style="color: #ff0000;"> <span> $dsn      = &#8216;dbi:mysql:dbname=NameOfDatabase&#8217;;</span></span></span></td>
</tr>
</tbody>
</table>
<p><span style="font-family: Verdana; "><span style="font-family: Courier New;">$dsn</span> must contain &#8216;<code>dbi:</code><span style="font-family: Courier New;"><em>driver_name</em><code>:</code></span>&#8216;, from there the input is database specific. With MySQL &#8216;dbname=NameOfDatabase&#8217; should be all that&#8217;s necessary.  At other times, you may need to specify the host you wish to connect to, if you&#8217;re not trying to connect locally.  In MySQL, as well as most databases, you&#8217;ll have to specify which database you&#8217;re connecting to in <span style="font-family: Courier New;">$dsn</span>, because MySQL access is specified on a per database level, and sometimes even on a per table level (in GRANT tables.)  Assuming you do have access, you still need a username and password to connect.</span></p>
<p><strong><span style="font-family: Verdana; ">Usernames And Passwords</span></strong></p>
<pre><span>  $user     = 'mysqlusername';
  $password = 'mysqlpassword';</span></pre>
<p><span style="font-family: Verdana; ">Although MySQL generally attributes access on a per database level, users are created for all of MySQL.  Therefore, you are entering a MySQL username and password.  MySQL then proceeds to connect you to the specific datasource you requested, and grant you the access privileges your username and password afford you.</span></p>
<p><strong><span style="font-family: Verdana; ">Final Hashref</span></strong></p>
<p><span style="font-family: Verdana; ">Assuming you&#8217;ll be able to connect to the database, you can then modify some attributes in this final hashref.  According to Tim Bunce, the hashref can be used  &#8216;alter the default settings of <code>PrintError</code>, <code>RaiseError</code>, <code>AutoCommit</code>, and other attributes,&#8217; even your mysql username and password (taking precedence over the previously specified ones.)</span></p>
<p><strong><span style="font-family: Verdana; ">I&#8217;m Connected, Now What?</span></strong></p>
<p><span style="font-family: Verdana; ">With DBI, you can retrieve, insert, and delete data from the database.  Look out for future articles on how to do each of these.  If you can&#8217;t wait until more articles come out, check out the more technical DBI documentation (</span><a href="http://search.cpan.org/%7Etimb/DBI-1.38/DBI.pm" target="_blank"><span style="font-family: Verdana; ">http://search.cpan.org/~timb/DBI-1.38/DBI.pm</span></a><span style="font-family: Verdana; ">), perlmonk&#8217;s (</span><a href="http://www.perlmonks.com/" target="_blank"><span style="font-family: Verdana; ">http://www.perlmonks.com</span></a><span style="font-family: Verdana; ">), and try google-ing, too!  Good luck, and we hope to see you when the next article comes out!</span></p>
<div class="hotlist"><strong>Author: </strong>Gyan Kapur</div>
<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/perl-mysql-dbi/">Perl And MySQL; Using DBI; Connections</a></p>
<p>Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/mysql-database-handling-in-php/' rel='bookmark' title='MySQL Database Handling in PHP'>MySQL Database Handling in PHP</a></li>
<li><a href='http://www.hotscripts.com/blog/loops-in-perl-pt-1/' rel='bookmark' title='Loops in Perl &#8211; Pt. 1'>Loops in Perl &#8211; Pt. 1</a></li>
<li><a href='http://www.hotscripts.com/blog/hot-scripts-forum-digest-sep-20th-2009/' rel='bookmark' title='Hot Scripts Forum Digest &#8211; Sep 20th 2009'>Hot Scripts Forum Digest &#8211; Sep 20th 2009</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.hotscripts.com/blog/perl-mysql-dbi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pattern matching with Regular Expressions</title>
		<link>http://www.hotscripts.com/blog/pattern-matching-regular-expressions/</link>
		<comments>http://www.hotscripts.com/blog/pattern-matching-regular-expressions/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 03:00:53 +0000</pubDate>
		<dc:creator>DEVpapers</dc:creator>
				<category><![CDATA[CGI & Perl]]></category>

		<guid isPermaLink="false">http://www.hotscripts.com/blog/?p=736</guid>
		<description><![CDATA[Validating user input is an essential part of any web developers repertoire. Failure to validate well can result in cross browser attacks as well as garbage entering your database or other storage mechanisms. Read more to find out the basics of data validation using regular expressions with Javascript.<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/pattern-matching-regular-expressions/">Pattern matching with Regular Expressions</a></p>

Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/finding-a-string-with-preg_match/' rel='bookmark' title='Finding a string with preg_match()'>Finding a string with preg_match()</a></li>
<li><a href='http://www.hotscripts.com/blog/automatically-hyperlink-urls-and-e-mail-addresses-in-asp-net/' rel='bookmark' title='Automatically Hyperlink URLs and E-Mail Addresses in ASP.NET Pages with C#'>Automatically Hyperlink URLs and E-Mail Addresses in ASP.NET Pages with C#</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div><span> <span style="font-family: Verdana; ">Regular expressions have long been available to UNIX shell programmers, as well as being available in scripting languages like Perl and PHP, but using Javascript 1.2, you can also tap into their power, which makes them especially handy for client-side validation.</p>
<p><span style="font-weight: bold; color: #ff6666;"><span style="color: #000000;">Regular Expressions in Javascript</span></span></p>
<p>The <span style="font-family: Courier New;">RegExp</span> object is the parent to the regular expression object. <span style="font-family: Courier New;">RegExp</span> has a constructor function that instantiates a Regular Expression object much like the Date object instantiates a new date. To a Regular Expression object, you would use the following syntax:</p>
<p></span></p>
<pre><span style="color: #ff0000; ">var myregexp  =  new RegExp( “pattern”, [“switch”] );</span></pre>
<p><span style="font-family: Verdana; ">or, you could use the alternative syntax:</p>
<p></span></p>
<pre><span style="color: #ff0000; ">var myregexp = /pattern/[switch]</span></pre>
<p><span style="font-family: Verdana; ">To use the Regular Expression object to validate user input you must define a pattern string that represents the search criteria. Pattern strings are defined using string literal characters and metacharacters. For example, to determine if a string contained a valid date you could use the following search pattern:</p>
<p></span></p>
<pre style="font-weight: bold;"><span style="">/^\d{1,2}(\-|\/|\.)\d{1,2}\1(\d{2}|\d{4})$/</span></pre>
<p><span style="font-family: Verdana; ">This would successfully match any of the following:</p>
<p>1.1.1980<br />
01-01-1980<br />
1/11/80</p>
<p>(but note that it doesn&#8217;t actually guarantee the date as valid, as you could also enter 32.1.80, so more work must be done!)</p>
<p>Let’s look at the pattern in more detail:</p>
<p><span style="font-weight: bold; font-family: courier new,courier,monospace;"><span style="font-weight: bold;">^</span></span> indicates the beginning of the string.<br />
<span style="font-weight: bold; font-family: courier new,courier,monospace;">\d</span> indicates a digit character and the <span style="font-weight: bold; font-family: courier new,courier,monospace;">{1,2}</span> following it means that there must be one or two consecutive digit characters.<br />
<span style="font-weight: bold; font-family: courier new,courier,monospace;">(\-|\/|\.)</span> says to match either a hyphen, or a forward-slash, or a full-top. The pipe (|) character between the date separators means &#8216;or&#8217; and the back-slash (\) before each character means &#8216;do not treat this character as part of the validation pattern&#8217;, in other words, let it pass through the expression.<br />
<span style="font-weight: bold; font-family: courier new,courier,monospace;">\d{1,2}</span> is as mentioned previously, one or two digits<br />
<span style="font-weight: bold; font-family: courier new,courier,monospace;">\1</span> This example uses backreferencing to ensure that the second date separator matches the first one.<br />
<span style="font-weight: bold; font-family: courier new,courier,monospace;">(\d{2}|\d{4})</span> means similar to the other digit patterns, but this time, match either two or four consecutive digit characters</p>
<p>and finally,</p>
<p><span style="font-weight: bold; font-family: courier new,courier,monospace;">$</span> indicates the end of the string.<br />
<br style="font-weight: bold; color: #ff6666;" /><span style="font-weight: bold; color: #ff6666;"><span style="color: #000000;">Categories Of Regular Expression Pattern Characters</span></span></p>
<p>Pattern-matching characters can be grouped into several categories. For more details of what pattern characters can be used, see here:</p>
<p></span><a href="http:///"><span style="font-family: Verdana; ">http://www.imps-group.co.uk/ref/js_regexp.pdf</span></a></p>
<p><span style="font-family: Verdana;"><span style=""><span style="font-weight: bold; color: #ff6666;"><span style="color: #000000;">Pattern Switches</span></span></p>
<p>In addition to the pattern-matching characters, you can use switches to make the match global or case- insensitive or both. The following is an example of a pattern string definition that uses a switch:</p>
<p><span style="font-family: Courier New;">/\s/g</span></p>
<p>This pattern and switch combination matches all occurrences of a space because it uses the global switch. You can find information of the switches available here:</p>
<p></span></span><a href="http:///"><span style="font-family: Verdana; ">http://www.imps-group.co.uk/ref/js_regexp.pdf</span></a></p>
<p><span style="font-family: Verdana; ">Now that you’ve been introduced to regular expressions and patterns, let’s look at a few examples of common validation functions.</p>
<p><span style="font-weight: bold;">UK Phone Number</span></p>
<p>Assuming the area code and phone are not separate fields, a valid phone number would consist of a 4 digit area code, optional space, 3 digits, and another optional space followed by 4 more digits. The first number of the area code must be a zero. A regular expression to do this might look like this:</p>
<p></span></p>
<pre><span style="color: #ff0000; ">var myregexp  = /^(0{1})([1-9]{3})\s?\d{3}\s?\d{4}$/;</span></pre>
<p><span style="font-family: Verdana;"><span style=""><span style="font-weight: bold;">US Zipcode</span></p>
<p>A valid zip code should consist of either a five digit code or five digits plus four separated by a hyphen. This can be done with a regular expression like this:</p>
<p></span></span></p>
<pre><span style="color: #ff0000; ">var myregexp = /(^\d{5}$)|(^\d{5}-\d{4}$)/</span></pre>
<p><span style="font-family: Verdana;"><span style=""><span style="font-weight: bold;">Integer</span></p>
<p>A valid integer value should contain only digits plus possibly a leading minus sign for negative numbers. A regular expression to do that would look like this:</p>
<p></span></span></p>
<pre><span style="color: #ff0000; ">var myregexp  = /(^-?\d\d*$)/;</span></pre>
<p><span style="font-family: Verdana;"><span style=""><span style="font-weight: bold;">Currency</span></p>
<p>A floating point value plus a currency symbol (£, $ or € in this case), with optional lead minus sign:</p>
<p></span></span></p>
<pre><span style="color: #ff0000; ">var myregexp  = /^-?[\£\$\€][0-9\.\,]+$/;</span></pre>
<p><span style="font-family: Verdana; ">The uses of regular expressions are virtually limitless. Take a look at these:</p>
<p></span></p>
<pre><span style="color: #ff0000; ">var myregexp  = /^.+@.+\..{2,3}$/;</span></pre>
<p><span style="font-family: Verdana; "><strong>A simple email match.</strong></p>
<p></span></p>
<pre><span style="color: #ff0000; ">var myregexp  = /^(http|https|ftp):\/\/((?:[a-zA-Z0-9_-]+\.?)+):?(\d*)/;</span></pre>
<p><span style="font-family: Verdana; ">A URL that requires (http, https, ftp)://, A nice domain, and a decent file/folder string.</span></p>
<p><span style="font-family: Verdana; "><span style="font-weight: bold; color: #ff6666;"><span style="color: #000000;">Testing Regular Expressions using Javascript</span></span></p>
<p>To test a pattern in javascript using the regular expression object, we use the &#8216;test&#8217; method, like this:</p>
<p></span></p>
<pre><span style="color: #ff0000; ">test(string);</span></pre>
<p><span style="font-family: Verdana; ">Here is a sample function, which returns &#8216;true&#8217; on a successful validation, or &#8216;false&#8217; if it spots an error.</span></p>
<p><span style="font-family: Verdana; "> </span></p>
<table border="0" cellspacing="2" cellpadding="2" width="100%" bgcolor="#dcdcdc">
<tbody>
<tr>
<td><span style="color: #ff0000; ">function validate(str) {</span><span style="color: #ff0000; "> var success = true;</span></p>
<p><span style="color: #ff0000; "> var pattern = /^[\sa-zA-Z\,\-]+$/; // a string of letters, plus comma or hyphen</span></p>
<p><span style="color: #ff0000; "> if (!pattern.test(str)) {</span></p>
<p><span style="color: #ff0000; "> success = false;</span></p>
<p><span style="color: #ff0000; "> }</span></p>
<p><span style="color: #ff0000; "> return success;</span></p>
<p><span style="color: #ff0000; ">}</span></td>
</tr>
</tbody>
</table>
<p><span style="font-family: Verdana; ">Here are three example calls to our function:</span></p>
<pre><span style="color: #ff0000; ">validate(“my name is Stuart, Pleased to meet you”);</span></pre>
<p><span style="font-family: Verdana; ">This code snippet would return &#8216;true&#8217;, because it doesn&#8217;t contain any characters other than alphabetical characters, a comma or the space character.</p>
<p></span></p>
<pre><span style="color: #ff0000; ">validate(“I was born in June 1972”);</span></pre>
<p><span style="font-family: Verdana; ">This code would return &#8216;false&#8217; – it&#8217;s got numbers in it!</p>
<p></span></p>
<pre><span style="color: #ff0000; ">validate(“my email is stuart@example.com”);</span></pre>
<p><span style="font-family: Verdana; ">Again, this would return false, as the email address has characters not allowed in our pattern.</p>
<p>Happy coding!</span></span></div>
<div class="hotlist"><strong>Author: </strong>Stuart Bull<br />Freelance developer specialising in Open Source application development frameworks and utilising PHP, MySQL, PostgreSQL, XML, XHTML, CSS and Javascript</div>
<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/pattern-matching-regular-expressions/">Pattern matching with Regular Expressions</a></p>
<p>Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/finding-a-string-with-preg_match/' rel='bookmark' title='Finding a string with preg_match()'>Finding a string with preg_match()</a></li>
<li><a href='http://www.hotscripts.com/blog/automatically-hyperlink-urls-and-e-mail-addresses-in-asp-net/' rel='bookmark' title='Automatically Hyperlink URLs and E-Mail Addresses in ASP.NET Pages with C#'>Automatically Hyperlink URLs and E-Mail Addresses in ASP.NET Pages with C#</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.hotscripts.com/blog/pattern-matching-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is CGI?</title>
		<link>http://www.hotscripts.com/blog/what-is-cgi/</link>
		<comments>http://www.hotscripts.com/blog/what-is-cgi/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 15:49:11 +0000</pubDate>
		<dc:creator>DEVpapers</dc:creator>
				<category><![CDATA[CGI & Perl]]></category>

		<guid isPermaLink="false">http://www.hotscripts.com/blog/?p=738</guid>
		<description><![CDATA[Many people think of CGI as some sort of programming language, and this common misconception is one of my pet peeves. So what exactly is CGI if not a programming language? And what use is it to the web designer?<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/what-is-cgi/">What is CGI?</a></p>

No related posts.]]></description>
			<content:encoded><![CDATA[<div><span></p>
<pre><span style="font-family: Verdana; color: #ff0000; "><strong>The Common Misconception</strong></span></pre>
<p><span style="font-family: Verdana; ">Almost everyone who is new to the Common Gateway Interface (CGI) makes the mistake of thinking of it as a programming language in its own right. CGI is <strong>not</strong> a programming language!</span></p>
<p><span style="font-family: Verdana; ">So what is CGI if it is not a programming language then?</span></p>
<p><span style="font-family: Verdana; ">The Common Gateway Interface is a way of connecting your web pages to other programs that are running on the server which visitors wouldn&#8217;t normally have access to. Most corporate web sites on the Internet today make use of CGI programs or scripts to allow their visitors to browse online catalogues or keep track of their orders in real time.</span></p>
<p><span style="font-family: Verdana; ">HTML pages are static in nature, if you request the same file, then you will get the same page (that is assuming that it has been updated between requests). A CGI generated page on the other hand can look very different each time you load it. This is because the CGI script or program that is used to generate it can dynamically build the page, including details such as the time of the request for example, to make the page different.</span></p>
<p><span style="font-family: Verdana; color: #ff0000; "><strong>CGI Scripts, CGI Programs or CGI Applications?</strong></span></p>
<p><span style="font-family: Verdana; ">The term you use will depend on the nature of the application that you are working on, although many people will tend to use and stick to a single term, there is a difference.</span></p>
<p><span><span style="font-family: Verdana;"><strong>CGI Scripts</strong> &#8211; are interpreted by the server in real-time, and need to be interpreted each and every time they are run. For this reason they can be slower than a CGI program, but they are also easier to maintain as the code is accessible through a text editor.</span></span></p>
<p><span><span style="font-family: Verdana;"><strong>CGI Programs</strong> &#8211; are compiled on the server they are running to help speed up the execution time and to hard code data paths for extra security. However, it can be difficult to compile programs on systems where access is limited, and the program will need to be recompiled for each platform you want to use it on.</span></span></p>
<p><span><span style="font-family: Verdana;"><strong>CGI Applications</strong> &#8211; typically a CGI Application is a collection of CGI scripts or programs that work together to create a web-based interface for a program or database, although a single stand alone program or script can be described as an application if it does more than one type of server interaction.</span></span></p>
<p><span style="font-family: Verdana; color: #ff0000; "><strong>Perl is not CGI</strong></span></p>
<p><span style="font-family: Verdana; ">Perl scripts were often associated with CGI in the late 1990s because the language was well supported by the servers and was very good at text manipulation; which was ideal for building web pages on the fly. Most servers that recognise the .cgi file extension will open the file using the Perl interpreter. The connection between Perl and CGI is so strong that many believe that all CGI programs are written in Perl. This is simply not the case.</span></p>
<p><span style="font-family: Verdana; ">CGI applications can be written in a variety of different languages; the language of choice will often depend on what software is available on the server. Other languages that may be used in the development of CGI programs include Python, C++, Java and Visual Basic, to name but a few. So you don&#8217;t have to learn a new programming language to be able to create your own CGI scripts.</span></p>
<p><span style="font-family: Verdana; color: #ff0000; "><strong>Advances in Web Interface Technology</strong></span></p>
<p><span style="font-family: Verdana; ">Traditional CGI is a rather resource intensive way to interact with a server. The problem arises because of the way that CGI interactions were originally designed; in the case of CGI scripts that need to be interpreted at run time, the language interpreter was loaded each time it was needed. On a lightly used server, this notion of only using it when necessary could actually save server resources; however this method does not scale very well as each HTTP connection would create it&#8217;s own copy of the interpreter in memory where only the one was needed.</span></p>
<p><span style="font-family: Verdana; ">As more and more servers were making use of CGI technology, the problem of scalability soon became an issue. Because most of the earlier CGI scripts were written in Perl, Doug MacEachern created the <code>mod_perl</code> Apache module to embed the Perl interpreter directly into the Apache server thus avoiding the need to start it for each Perl script requested.</span></p>
<p><span style="font-family: Verdana; ">Newer server technologies have tackled the problem of scalability by developing their own Application Programming Interfaces (APIs), which work in the same way as <code>mod_perl</code>. These APIs load all the various language interpreters that are installed into the server&#8217;s memory when it is started, where they will remain in memory ready to use as soon as a request comes in.</span></p>
<p><span style="font-family: Verdana; color: #ff0000; "><strong>Common CGI Equivalents</strong></span></p>
<p><span style="font-family: Verdana; ">Microsoft&#8217;s server API is commonly referred to as the ISAPI filter; it runs in the background and redirects server calls (if they need to be parsed in any way) to run various Dynamic Link Libraries (DLLs) that do the work of parsing the language in use. Active Server Pages are parsed by the asp.dll, which contains information about the various ASP objects and methods that the server can understand. Extra programming interfaces can be added by registering new ASP components (COM objects) in the server&#8217;s memory space using the regsvr32 registration utility.</span></p>
<p><span style="font-family: Verdana; ">PHP: Hypertext Preprocessor (PHP) scripts are growing increasingly popular and so PHP is supported for a wide range of platforms and servers by offering different ways of using the interpreter. The PHP parsing engine is available as an ISAPI filter, Apache module or stand alone CGI program. As you can imagine, the PHP CGI executable loads a copy of the executable in memory for each call to a PHP page, and so is actively discouraged for performance reasons; recommended for use only on servers that use virtual hosts for security reasons. </span></p>
<p><span style="font-family: Verdana; color: #ff0000; "><strong>The Future of CGI</strong></span></p>
<p><span style="font-family: Verdana; ">The way we use our servers will continue to develop, and now that the performance issues surrounding CGI scripts have been highlighted, it seems the trend is to abandon the old style of CGI access in favour of the more robust server managed solutions. My own first CGI scripts were written in Perl, but I abandoned it in favour of ASP because Perl did not integrate so easily with my web pages.</span></p>
<p><span style="font-family: Verdana; ">It seems that the use of Perl for web sites is diminishing in favour of PHP, ASP or JSP; at least, it&#8217;s been quite a while since I saw the more popular sites I visit use Perl to interact directly with their visitors.</span></p>
<p><span style="font-family: Verdana; color: #ff0000; "><strong>Adding Interactivity to Your Site</strong></span></p>
<p><span style="font-family: Verdana; ">If you want to add interactivity to your own site, then I recommend that you use a technology that runs in the server&#8217;s memory space, particularly if you want a more scalable solution.</span></p>
<p><span style="font-family: Verdana; ">My original recommendation for a language to learn was, ironically, Perl. Now, I&#8217;d probably recommend something like PHP, despite not knowing how to write a full-blown script with it myself, it is widely supported and freely available. ASP is probably easier to learn if you are completely new to the programming scene, but ASP hosting is typically more expensive in comparison. Also, now that .NET has been officially released, a lot of the ASP sites are focusing on ASP.NET support and so the days of traditional ASP support are numbered.</span></p>
<p></span></div>
<div class="hotlist"><strong>Author: Rosemarie Wise</strong><br />
The author, Rosemarie Wise, is a self proclaimed &#8220;web enthusiast&#8221; who set up her site, <a href="http://www.websiteowner.info/">Web Site Owner</a>to share her experiences of being a site owner.</div>
<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/what-is-cgi/">What is CGI?</a></p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.hotscripts.com/blog/what-is-cgi/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Randomness In Perl</title>
		<link>http://www.hotscripts.com/blog/randomness-in-perl/</link>
		<comments>http://www.hotscripts.com/blog/randomness-in-perl/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 21:12:24 +0000</pubDate>
		<dc:creator>DEVpapers</dc:creator>
				<category><![CDATA[CGI & Perl]]></category>

		<guid isPermaLink="false">http://www.hotscripts.com/blog/?p=734</guid>
		<description><![CDATA[Perl can be random, too! This article explains how to to use the rand() function in Perl in many contexts, as well as its origins.<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/randomness-in-perl/">Randomness In Perl</a></p>

Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/loops-in-perl-pt-1/' rel='bookmark' title='Loops in Perl &#8211; Pt. 1'>Loops in Perl &#8211; Pt. 1</a></li>
<li><a href='http://www.hotscripts.com/blog/hashes-in-perl/' rel='bookmark' title='Hashes in Perl'>Hashes in Perl</a></li>
<li><a href='http://www.hotscripts.com/blog/regex-operators-in-perl/' rel='bookmark' title='Regex Operators in Perl'>Regex Operators in Perl</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div><span><span style="font-family: Verdana; ">Perl&#8217;s <span style="font-family: Courier New;">rand()</span> function is a little bit more interesting than PHP&#8217;s, because it&#8217;s behavior isn&#8217;t as finely tuned to the needs of website programmers, as PHP&#8217;s.  Furthermore, perl&#8217;s <span style="font-family: Courier New;">rand()</span> function is very flexible.</span></p>
<p><strong><span style="font-family: Verdana; ">Rand Basics</span></strong></p>
<p><span style="font-family: Verdana; ">The rand function returns a decimal number between 0, including 0, and the inputed number, not inclusive.  Therefore, <span style="font-family: Courier New;">rand(7)</span> would return a decimal between 0 and 7.  If there is no input, then rand would return a number between 0 and 1.</span></p>
<p><strong><span style="font-family: Verdana; ">But I Want A Number Between 0 And 50.</span></strong></p>
<p><span style="font-family: Courier New; color: #ff0000; ">print int rand 51;</span></p>
<p><span style="font-family: Verdana; ">Perl&#8217;s <span style="font-family: Courier New;">rand()</span> function isn&#8217;t built to give you integers. However, with the int() function you can make it do your bidding.  However, it&#8217;s important to notice that you have to enter a number one greater than the largest number you&#8217;re looking for.</span></p>
<p><strong><span style="font-family: Verdana; ">Rand And Arrays</span></strong></p>
<p><span style="font-family: Courier New; color: #ff0000; ">my @array = ( &#8216;first possibility&#8217;,'second&#8217;,'third&#8217;);</span></p>
<p><span style="font-family: Courier New; color: #ff0000; ">print $array[int rand scalar @array];</span></p>
<p><span style="font-family: Verdana; color: #000000; ">This code works with almost any input as <span style="font-family: Courier New;">@array</span>.  URLs, images, possible passwords, whatever you need can be in <span style="font-family: Courier New;">@array</span>.  The function is returning the int rand result of the scalar value of <span style="font-family: Courier New;">@array</span>. What&#8217;s important to note is that with arrays, the scalar value is one higher than the highest key value of <span style="font-family: Courier New;">@array</span> (Perl&#8217;s arrays are indexed at 0).</span></p>
<p><strong><span style="font-family: Verdana; ">Truly Random</span></strong></p>
<p><span style="font-family: Verdana; ">Use of Perl&#8217;s <span style="font-family: Courier New;">rand()</span> function is not advised for cryptographers.  The <span style="font-family: Courier New;">rand()</span> function uses a seed to generate its input, and unfortunately that seed is predictable approximately one third of the time.  Check out <span style="font-family: Courier New;">perldoc -f srand</span> and<span style="font-family: Courier New;"> perldoc -f rand</span> for more information on this.  Instead, it&#8217;s advised that for truly random input, one should use the Perl Module <span style="font-family: Courier New;">Math::TrulyRandom</span>.</span></p>
<p></span></div>
<div class="hotlist"><strong>Author: </strong>Gyan Kapur</div>
<p>Post from: <a href="http://www.hotscripts.com/blog">Hot Scripts Blog</a><br/><br/><a href="http://www.hotscripts.com/blog/randomness-in-perl/">Randomness In Perl</a></p>
<p>Related posts:<ol>
<li><a href='http://www.hotscripts.com/blog/loops-in-perl-pt-1/' rel='bookmark' title='Loops in Perl &#8211; Pt. 1'>Loops in Perl &#8211; Pt. 1</a></li>
<li><a href='http://www.hotscripts.com/blog/hashes-in-perl/' rel='bookmark' title='Hashes in Perl'>Hashes in Perl</a></li>
<li><a href='http://www.hotscripts.com/blog/regex-operators-in-perl/' rel='bookmark' title='Regex Operators in Perl'>Regex Operators in Perl</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.hotscripts.com/blog/randomness-in-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

