5:50 PM

Dynamic listboxes in perl

Ever wanted to know how to automatically generate a listbox full of values from an array, database or some sort? No? Me neither.. but nevertheless.. Here's how!

Firstly this is pretty much common sense so just ignore this if you are not a beginner (or scroll down to see the complete code for an overview). The HTML code for a listbox, you should probably already know.. but if not here it is for you.


<select>
   <option value="1">One</option>
</select>


Note: select is the actual listbox, option are the items in the listbox.. value being what will be posted as a form (useful mostly for the server, unless it's a javascript or client application). The text in the middle is what will actually be shown in the listbox.

First you need your content (the items to display in the actual listbox), this will be read into an array.. For the sake of the tutorial I will be reading the content from a text file.

We open the file using "<" which ensures we are opening to read the file only, if the file fails to open we instantly kill the perl script from processing further with an error message.


open(FILE, "<items.txt"or die("I/O Error.\n"); # Open item.txt with our list of items for the listbox



We then read the file into an array, the array will add each line to a seperate element in the array, though that can be changed using the split and join functions. We set a variable for the value (alternately you can simply use the items from the file.). This value will increase for each line of the array creating a numerical value for each item which can be used to identify the selection.


my @farray = <FILE># Read the file into the array "farray" (based on new line character)
my $value = 1;



To print out the options we use a foreach loop to loop each item in the array, $item will be the text value for each line whilst @farray is the array from which the elements will be looped. We use the chomp function to remove the new line characters from the item ensuring there won't be any unnecessary new lines within the code we are looking to generate. We increase the value variable by 1 when we are done and ready for the next item.


foreach $item (@farray) { # loop each item in the array
   chomp($item); # Remove new line character from item
   print "<option value=\"$value\">" . $item . "</option>\n"# Print the option with value and item
   $value++; # Increase the value by 1, for each item
}



That's pretty much it for the basics, though here is a nifty way to print our HTML or other code within the perl script.


print
 <<EOF;
<html><head></head><body>
<select>
EOF



this will print anything below untill it reaches "EOF" which stands for end of file and is used commonly in programming, though here it is not nessecary.. you can use any word you like.

Here's the full code:


#!/usr/bin/perl
use Strict;
print "Content-type: text/html\n\n";
print <<EOF;   # Print till "EOF" is found
<html><head></head><body>
<select>
EOF


open(FILE, "<items.txt"or die("I/O Error.\n");   # Open text file for reading
my @farray = <FILE>;   # Read text file into array
my $value = 1;
foreach $item (@farray) {   # Loop the array and print each item with HTML data
   chomp($item);
   print "<option value=\"$value\">" . $item . "</option>\n";
   $value++;   # Increase value for next use
}

print <<EOF;   # Print till "EOF" is found
</select>
</body></html>
EOF


And there you have it! A simple way to dynamically create listboxes in perl. It can be quite useful for creating lists for locations, languages etc. etc. Goodnight!~

On a final note.. The next post might be a tutorial or information on HTML encoders, since writing code in blogspot is ULTIMATE FAIL!

5:45 PM

Learning Perl

Just thought I'd post a quick blog while I wait for some porn to finish downloading. have nothing better to do. Okay, here are some tips for learning the web/scripting language Perl.

Perl is a relatively easy language to pick up, especially if you have some prior programming knowledge. The great thing about perl is how you can do so much with so little code. It is very unique and has lots of cool features and an extensive library of modules which can be found and installed very easily thanks to CPAN.

If you are a total beginner you will want to start with a Perl book. There are some free online books, here's a few from the official perl website.

perl.org/beginning-perl (good for the total beginner)
perl.org/impatient-perl (I personally prefer this book based on the fact that it is short and to the point, It's also good for people with prior experience).

You can find a few more at http://www.perl.org/books/library.html
The Perl & LWP book is especially useful if you plan to use perl for downloading/parsing and manipulating website information, though only a sample is available for free.

If video tutorials are your thing, I recommend checking out youtube.com/bermnz who has a wide variety of perl tutorial videos (the biggest collection I have seen for any programming language tutorials). Though some of the code is ill-explained and his style and use of code is sometimes not what is considered "good" or "standard" you can learn much from the videos, remember to take some notes and look up what you don't understand.

The official perl website (kind of) perl.org is also a great resource, check out the documentation here there are a few tutorials and references for most things in the perl language (I use it mostly as a reference when I'm looking for the functionality or an example of a certain standard function/feature of the perl language).

That pretty much sums up what I have to say, this might have been lacking in quality.. I'm no expert but good luck to those that wish to attempt learning perl, you won't regret it! :)

12:17 PM

print "Hello Thur~\n";

Hey, welcome to my blog. I never did one before so it will probably suck, don't say I didn't warn you! If you are wondering what the hell kind of subjects I will blog about since my title is very non descriptive.. I'm a programmer (self proclaimed) haha, I work with a variety of languages but mainly C++, C#, D and Perl so I might post some articles related to them. I'm also a big anime/manga fan so I might well post some news/information whenever I feel it is worth it. Aside from that my next big passion is gaming.. I wouldn't call myself a hardcore gamer but I do enjoy playing Counter-strike: Source and some MMOs.. namely Lineage II when I get the chance (I always get the chance). Well that's about all I have to say right now. I hope I can remember to come back here with another post! Wish me luck.. BAI!