Posts in the “perl” category

Perl split function - process every word in a file

Problem: You're developing a Perl program, and you need to process ("do something with") every word in a text file within your program.

Solution: How you achieve this in the end depends on your meaning of "every word", but I'm going to go with a very simple definition, where I can use the Perl split function to break up each "word" that is surrounded by whitespace characters.

Where is Perl looking for modules to include?

I started working on a new Unix system yesterday -- an HP-UX system I've never worked on before -- and I quickly realized that I needed some Perl modules installed. While working with another person the question quickly came up, "How do you know where Perl is looking for currently installed modules?"

Perl: How to extract lines from the middle of a text file

A Perl program to extract lines from the middle of a file

I've always been a big fan of the Unix head and tail commands, but several times I have wanted to print a range of lines from the middle of a text file -- not just the beginning or end of a file. Not liking the available solutions, I wrote my own.

Here is the source code for a program named "extract.pl", which prints lines from a text file beginning at the start line you specify and ending at the stop line.

A Perl “read file into array” example

Perl read file FAQ: How can I read a file into an array in Perl (Or, Can you share a Perl read file into array example?)

One of the really cool things about Perl is that it's super-easy to read a file into a Perl array. This is really handy any time you need to read a file for any reason.

A Perl read file into array example

If you want to read a file into a Perl array, you actually only need one line of code, like this:

How to embed data in your Perl program

Here's a sample Perl program that demonstrates how you can include (embed) data inside of your Perl program, right in there next to the source code.

This simple program takes the data after the special __END__ tag, and makes it available to your Perl source code.

#!/usr/bin/perl

while (<main::DATA>)
{
  print $_;
}

__END__
George Washington
Abraham Lincoln
John F. Kennedy

As you can see, you loop through the data with this line of code:

Perl files example - How to open and read data files with Perl

Perl files FAQ: How do I open and read files in Perl?

This article demonstrates how to open and read files in Perl. As you'll see, the creators of Perl thought this was a fundamental activity, and they've made it as easy as possible. (That's one of the things I really like about Perl - many of the routine, daily programming tasks have been made very easy with this language.) If you've used other "structured" languages, such as C or FORTRAN, you'll appreciate how easy it is to open a file and create a loop to read data from the file.

How to print every element in a Perl array

Summary: A Perl for loop (foreach loop) example, showing how to loop over each element in a Perl array.

Many times you'll need to print all the elements of a Perl array, or perhaps the elements of the array that match some filtering criteria. I thought I'd share several examples of how to print the elements of a Perl array here.

Initial setup

In each of the methods shown below, assume that we have an array named @pizzas that has been defined like this:

How to delete a list (collection) of files

Perl FAQ: How do I delete a list of files in a Perl script?

This is very similar to deleting one file in a Perl program. You just use Perl's unlink function, but this time pass it a list of filenames. Let's take a look at a simple example.

A simple example

First, create some sample files in the current directory:

A Perl program to determine RSS readers from an Apache access log file

Perl/RSS FAQ: How many RSS subscribers do I have on my website?

Like many other people with a blog or website, I was curious yesterday about how many RSS readers/subscribers the devdaily website has. You can try to get this information in a variety of ways, but the real information is on your server, in your Apache log files.

To figure out how many RSS subscribers your website has, just go through your Apache log file, find all the records that look like this:

A Perl trim function

Perl string trim FAQ: Is there something like a "trim" function in Perl, similar to the Java trim function, which trims leading and trailing whitespace characters from a string?

A Perl trim function

My Perl skills aren't exactly up to date these days, but in days gone past there was no Perl trim function, and somewhere along the way I wrote one. Here's the source code for my Perl trim function:

A Perl array 'contains' example

Perl array FAQ: How can I test to see if a Perl array already contains a given value? (Also written as, How do I search an array with the Perl grep function?)

I use the Perl grep function to see if a Perl array contains a given entry. For instance, in this Perl code:

A Perl “write to file” example

Perl write to file FAQ: Can you demonstrate an example of how to write to a file in Perl?

Somehow I managed to go all these years without showing a simple Perl "write to file" example. Let's fix that.

Perl “write to file” example

Here's a short snippet of code that shows how to write to a file in Perl:

A Perl getopts example

Perl getopts FAQ: Can you demonstrate how to use the getopts function? (Also written as, "Can you demonstrate how to read Perl command line arguments?")

How to get SOAP::Lite working with the Apache CXF web service

I'm not going to spend too much time showing this Perl SOAP client solution, but if you're having problems using the Perl SOAP::Lite module as a client running against a modern web service, this sample Perl code may help solve the problem.

First, I'd like to show what my original Perl SOAP client code looked like when I was using it as a SOAP client running against several Axis and XFire web services:

Perl 'split' function - how to process text data files

Perl FAQ: How can I split a string in Perl, such as the strings in a pipe-delimited text file?

Many times you need a Perl script that can open a plain text file, and essentially treat that file as a database. Typically these files have variable-length fields and records, and the fields in each record are delimited by some special character, usually a : or | character. When processing these files, you can use the Perl split function, which I’ll demonstrate in two short programs here.

Perl print line example - How to print a specified line from a text file

Have you ever needed a program to print a specific line from a text file? I had this need a long time ago, and I wrote a Perl program to do just that, and I'd like to share it here.

There are certainly more Perl-ish ways to write a program to print a specific line from a text file, but hey, I don't use Perl that much these days, and I can still read this one. :)

Here's the source code for a Perl script I named perl-print-line.pl:

Perl read file example - How to print a range of lines from a text file

Here's a Perl script that demonstrates how to do "something" for each line in a text file that falls between a given line number range, i.e., a starting line number and an ending line number.

In this Perl script all I do is print each line that falls between this "start" and "stop" range, but of course your own Perl program can do anything it needs to within this range. In short, this Perl script does the following:

Perl split function - How to process pipe-delimited text files

When writing Perl programs, many "data" files you end up working with are really plain text files that use some kind of character to act as a field delimiter. For instance, a program I was working with recently reads data from files whose fields are separated by the pipe character ("|").