Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Regular Expressions 2005.pdf
Скачиваний:
95
Добавлен:
17.08.2013
Размер:
25.42 Mб
Скачать

26

Regular Expressions in Perl

Perl is a powerful and sometimes cryptic scripting language with particular strengths in the realm of text manipulation, system administration, and dynamic Web-content generation. Regular expression support in Perl is a key strength of the language, contributing significant power to Perl’s text manipulation functionality.

Because of its compact syntax, Perl can be a cryptic, sometimes impenetrable programming language for programmers with little or no Perl experience. Combining Perl with regular expressions, with their own potential for compact, cryptic syntax, can be an intimidating experience for the unprepared. However, if the components of the Perl and regular expression syntax are considered in their component parts, the worst of the potential for intimidating compactness can be avoided. By the time you have finished working through the content and examples in this chapter, you should have taken significant steps toward making Perl do useful text manipulation using regular expressions.

In this chapter, you will learn the following:

To obtain a Perl download and install it on Windows

To use Perl for basic regular expression tasks

To use the Perl regular expression operators

What metacharacters are supported in Perl

To use many of the Perl metacharacters

To specify and use regular expression modes in Perl

Obtaining and Installing Perl

This book is focused primarily on the Windows platform. However, Perl is available for use on a wide range of platforms.

To obtain a copy of a current version of Perl for a range of operating system platforms, visit www.perl.com/download.csp or http://www.perl.org/get.html, where currently available

Chapter 26

Perl downloads are listed. If you are using a platform other than Windows, choose the appropriate download from those offered and follow any installation information provided.

The Perl 5.8 download for Windows is available from www.activestate.com/ActivePerl/. ActivePerl is produced by a commercial company, ActiveState.com, but is recommended by Perl.com and is, at the time of this writing, available for free. To download the ActivePerl installer, you will first need to provide a name and e-mail address.

After downloading the ActivePerl MSI installer, simply double-click it and follow the on-screen instructions. If you are using Windows XP or Windows Server 2003 and have administrator rights, installation should be straightforward. If you are using another version of Windows, check the ActivePerl installation information at http://aspn.activestate.com/ASPN/docs/ActivePerl/install.html. On the installation screen, shown in Figure 26-1, leave all options checked unless you have specific reasons to do otherwise.

Figure 26-1

The installation instructions assume that you are installing Perl on a development machine used for testing purposes only. If you intend to install Perl on a production server, you should take the time to fully understand the relevant security issues.

ActivePerl will be installed to C:\Perl (no other option for installation location is given). If you have a previous version of ActivePerl installed it will be overwritten without your being informed. Unless you want to compare individual versions of ActivePerl, this default behavior doesn’t cause any problems, in my experience.

660

Regular Expressions in Perl

The ActivePerl installation includes a wealth of Perl documentation. The ActivePerl User Guide can be opened in a browser at C:\Perl\html\index.html and is shown in the Firefox browser in Figure 26-2. The left frame of the User Guide has several screens of information vertically and is well worth scrolling down. In particular, the left frame contains links to the Perl core documentation. An alternative way to access the ActivePerl User Guide is, on Windows XP, to select Start All Programs ActiveState ActivePerl 5.8 Documentation. The ActivePerl User Guide will open in a browser window.

If you prefer using the perldoc utility rather than the ActivePerl User Guide to explore the documentation, you can simply type the relevant perldoc command at the Windows command line, because the installer adds the needed information to the PATH environment variable. Figure 26-3 shows some of the information from perldoc about the Perl strict pragma accessed using the command perldoc strict at the Windows command line. To navigate forward by a screen, use the spacebar. To navigate forward one line, press the Return key.

Figure 26-2

661

Chapter 26

Figure 26-3

Now that you know where to find the Perl documentation in an ActivePerl installation, you will also want to be sure that Perl has installed correctly. You can check that the perl command is working by entering the command perl -v at the command line, which will display information about the version of Perl that you have installed. Figure 26-4 shows the appearance with ActivePerl 5.8.4.

Figure 26-4

You can create Perl code using any text editor or integrated development environment that you prefer. Examples in this chapter are developed using the ActiveState Komodo 3.0 development environment. A time-limited, free trial download of Komodo is available from www.activestate.com/Products/Komodo. To download the evaluation edition, you must provide a name and e-mail address. Be careful that the e-mail address is entered correctly, because a small executable is needed to create a functioning version of Komodo, and the URL to download the executable is sent to the e-mail address you provide. Komodo 3.0 is available as an MSI installer. In my experience, installation of Komodo has been straightforward and simply a matter of following the on-screen instructions.

662