Progress

1/4I would like to.

2/4What your Project need?

3/4Please answer the following questions:

4/4Please fill with your details

Address

Duo magna vocibus electram ad. Sit an amet aeque legimus, paulo mnesarchum et mea, et pri quodsi singulis.

11 Fifth Ave - New York, 45 001238 - United States

Email and website

Duo magna vocibus electram ad. Sit an amet aeque legimus, paulo mnesarchum et mea, et pri quodsi singulis.

Email: support@domain.com
Website: www.quote.com

Telephone

Duo magna vocibus electram ad. Sit an amet aeque legimus, paulo mnesarchum et mea, et pri quodsi singulis.

Tel: +44 543 53433
Fax: +44 543 5322


Blog post

Three Great Uses for Perl on the Web

December 29, 2019stackarr

Most people have heard of the Perl scripting language – it is, after all, known as the “duct tape of the Internet.” Which is a very apt description – Perl is not only useful as a full blown web development language – but it’s also extremely handy tool that can be used by web developers to automate or otherwise streamline all sorts of repetitive tasks that tend to litter the to-do lists of web developers all over the Internet.

Back up your server

This task is a prime example of how Perl is quite useful for system administration tasks. There’s nothing worse than losing data when something untoward happens to your web server. Your hosting company may offer backups – if you’re willing to pay for it. It’s easier to whip up a quick Perl script to do it for you.

Here’s the key to a Perl backup script – using the system command to let the script run commands on the system. For example, to back up all the files in a directory, you can use system “tar -cf /foo/backup.tar /somedir” – making use of the tar command.

Of course, using a Perl script to just run a tar command is sort of counter-productive. There’s lots of ways you can use standard Perl to add flexibility and functionality – set up an array of directories you want to backup, and then use a foreach loop to step through and backup each one. You could also use the Net::FTP module to have your backup script automatically transfer your backup up files to another computer. You can also backup MySQL databases in similar way by using the mysqldump command.

Then, just set up a cron job and have the backup script run whenever you like!

Fix a lot of files quickly

Say you have to go through a large number of static HTML files and make a small change – like change the year date on the copyright notice. Of course if the pages were set up in a CMS this probably wouldn’t be a big deal, but sometimes that’s not an option. With Perl you can use readdir to read a list of filenames from a directory into an array. Then step through the array and open each file. Read the contents of the file into a variable and then use Perl’s excellent regular expressions to fix the offending string. Then write the contents of the file out to a new directory. This is an important – you want to save all your changed files to a new directory so that if there’s a bug in your script, you don’t mangle your live files! If everything looks ok, copy the old files to a backup directory (you can never be too careful) and overwrite them with the fixed files.

Generate data tables quickly

It’s not uncommon for client’s to give their web developers a spreadsheet with data that needs to go on the website. A calendar of events is a pretty common example. Converting this data into an HTML table is a real hassle for this sort of thing ñ and simply using “Save as HTML” from Excel guarantees some hideous code. So just save the file as a tab-delimited file from the spreadsheet program, then write a quick Perl script to convert it into an HTML table. Simply use Perl’s split†command to split each line of the tab delimited file into an array of values. Then loop through the values, and put TD tags around the values. Build up a string of these table cells. Then put TR values around the string, and output the data to a temporary file. Make sure everything looks good, then cut and paste it into your HTML file. The best part? When the client sends you an updated spreadsheet sometime in the future, you can probably just use the same Perl script to generate the updated data table!

Hopefully these give you some ideas of how to use Perl to augment and simplify your web development work. Perl is like a swiss army knife for web developers, it’s almost infinitely flexible, and is a great tool for web developers to add to their toolbox.

Leave a comment

Your email address will not be published. Required fields are marked *

Prev Post
How can we help you?