Perl: Downloading and Parsing XML

One fairly common task in Perl is downloading and parsing data. For instance, you might want to download sports data and parse it so that you can import it to a database or do some sports ranking calc....

Perl CSV: Reading Comma Separated Values in Perl

A common task in Perl is reading files of comma separated values. While the exact form of the Perl program you use to read such files will naturally depend on exactly what you're trying to achieve, th....

Perl Split: How To Split Strings In Perl

Perl Split: How To Split Strings In PerlYou can split strings into tokens in Perl using the split() function. split PATTERN, TEXT Here's an example; we'll split some text on the pipe | character. my $....

Perl Regex: Regular Expressions In Perl

You can use regular expressions in Perl to check whether some text contains certain strings or patterns of text, or to replace part of a piece of text with something else.Perl contains a powerful regu....

Perl File Open: Creating, Reading and Writing to Files in Perl

Dealing with files in Perl is very easy once you get used to the slightly odd syntax. Here are some examples to get you started. Open a File in Perl To open a file in Perl, just the open() subroutin....

Perl Subroutine -- How To Use Functions in Perl

Let's start by creating a simple subroutine in Perl. The following subroutine takes no parameters and has no return value; all it does it print hello. use strict;use warnings;# Create a greet() subrou....

Perl String: FAQ, Tutorial, Tips and Tricks for Strings in Perl

Strings are really what Perl's all about, so it's very easy to use strings in Perl. Probably the three most important things to remember about strings in Perl are Strings are scalars, so they begin w....

Perl References -- Basic Usage, Subroutines and Complex Data Structures

In Perl, a reference is a scalar (single value) variable that refers to some other variable. A reference may refer to another scalar value, or to an array or a hash or subroutine or whatever.You can c....

Perl Eval -- Using Eval to Run Code and Trap Errors in Perl

You can use eval() in Perl to evaluate code; that is, to run code contained in a string, or to run code that may throw errors.A word of caution: use eval() sparingly. Improper or excessive use of ev....

Perl Mkdir -- How To Create Directories in Perl

mkdir() allows you to create directories in your Perl script. The following program creates a directory called temp. use strict;use warnings;sub main { my $directory = temp; unless(mkdir $directory....

Perl Command Line Options

Here are some of the most common and most useful command line options that you can use when you're running a Perl script. -w This turns on warnings in Perl; for example, using this flag will cause Pe....

Perl Base64 -- How To Do Base64 Encoding in Perl

You can do base64 encoding in Perl using the MIME::Base64 module.For those who don't know, base64 is a way of encoding binary data in ASCII characters; so you can email your favorite program as text,....

Perl Array of Array: Using Multidimensional Arrays in Perl

In Perl you can define, initialize and use an ordinary, one-dimensional array like this: my @fruits = ('apple', 'orange', 'pear');# Display the element at index 1# (second element in the array)print $....

Perl Wget -- Retrieving HTML Pages In Perl

Wget is a command-line program that allows you to retrieve files via HTTP or FTP from a UNIX prompt. People who are familiar with UNIX or Linux often wonder how to use wget in Perl. The simple answe....

Perl Array Sort -- How to Sort Arrays In Perl

Sorting arrays in the Perl programming language is actually very easy -- although the syntax you need to use can look a little strange at first.Let's start with a simple program that sorts an array in....

Perl Argv -- Retrieving Command-Line Arguments in Perl

In Perl, command-line program arguments are available via the @ARGV array.Note that the arguments are placed into @ARGV without regard to their meaning; Perl simply splits on space when populating....

Perl Variables — Declaring and Using Variables in Perl

Declaring Variables in Perl The most important thing to say about variables in Perl, before we get into any further details, is that you should always write use strict and use warnings at the top....

Perl Stdin -- Reading User Input in Perl

Getting user input is easy in Perl, as long as you're happy to wait for the user to hit <return>.The following little program waits for the user to enter a string and hit the return key, then te....

Perl Hashes -- A Guide to Associative Arrays in Perl

Hashes (associative arrays) are an extremely useful data structure in the Perl programming language. A hash is a way of associating one set of values (keys) with another set of values (values); formin....

Perl -- Size of an Array

To find the size of an array in Perl, simply use the array in a scalar context. my @items = (apple, orange, banana);my $size = @items;print Array contains $size items.n; Array contains 3 items. Here....

Perl Replace Substring

Let's say you've got a string in Perl and you want to replace just a part of it. How can you do it? Replacing a Fixed-Length Section of a String in Perl If the bit you want to replace is a fixed range....

Perl File Delete: Deleting Files and Directories in Perl

To delete a file in Perl, use the unlink function. unlink(hello.txt); ... deletes hello.txt.If for some reason unlink doesn't work (which seems to happen under certain circumstances), don't forget....

Perl Split Whitespace

How can we split a string in Perl on whitespace? The simplest way of doing this is to use the split() function, supplying a regular expression that matches whitespace as the first argument. my $string....

Perl Foreach -- Looping With Foreach in Perl

The foreach keyword is probably the easiest and most often used looping construct in Perl. Use it for looping through the items in an array. my @items = (apple, orange, banana);foreach my $item(@ite....

Perl Concatenate: Joining Strings and Arrays in Perl

Concatenating Strings In Perl Concatenating strings in Perl is easy. The only thing to remember is that you have to use the dot operator, not +: my $string1 = Nice;my $string2 = weather;my $string3 =....

Perl Clear Array

How Do I Clear An Array in Perl? To empty an array in Perl, simply define the array to be equal to an empty array: # Here's an array containing stuff.my @stuff = (one, two, three);@stuff = ();# Now i....

Perl Array: Quick Guide to Arrays In Perl

Declaring Perl Arrays Joining Arrays with join() Creating arrays with split() Dumping (Viewing) Perl Arrays Accessing Array Elements Perl Push: Adding Items to Arrays With Push and Unshift Perl....