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

Regular Expressions in Perl

Creating a Simple Perl Program

You are now ready to create and run a first, simple Perl program using Komodo 3.0 and ActivePerl. If you prefer to use a different text editor, I assume that you are familiar with the necessary commands to enter and edit Perl code in that editor.

Try It Out

Creating a Simple Perl Program

1.In Komodo 3.0, select New from the File menu; then select New File.

2.In the New File dialog box(shown in Figure 26-5), select the Perl option, then click the Open button.

Figure 26-5

The file Perl-1.pl will open in Komodo 3.0 with brief template code, as shown in Figure 26-6. The significance of those lines is explained in the How It Works section that follows.

3.Edit the Perl code so that it reads as follows:

#!/usr/bin/perl -w use strict;

my $myString = “Hello world!”;

print “$myString\n”;

print “The preceding line printed the variable \$myString.\n”; print “This program has run without errors.”;

4.Save the code as C:\BRegExpr\Ch26\Simple.pl.

5.In Komodo 3.0, press F5; then press the Return key (accepts the default of no script arguments) to run the code in debug mode.

If this is the first code file you have created in Komodo, following the preceding instruction should be all you need to do. If you have been running other code inside Komodo, you will need to take the extra step of selecting which file to run between pressing the F5 and Return keys. Click the Browse button to select the file you want to run.

663

Chapter 26

Figure 26-6

6.Inspect the results displayed in the Output pane in the lower-right corner of the Komodo screen, as shown in Figure 26-7. Notice that three lines of text are displayed in the Output pane.

The Perl examples in this chapter were tested using ActivePerl 5.8.4. It is likely that the examples will run unchanged on all versions of Perl 5.8, but this has not been tested.

If you have chosen not to use the Komodo 3.0 development environment, you can run the file Simple.pl from the command line. Simply type the following command, and the file will be run, assuming that you installed ActivePerl (which sets up the PATH environment variable) or added the perl engine to your machine’s path:

perl Simple.pl

664

Regular Expressions in Perl

Figure 26-7

Figure 26-8 shows the screen’s appearance after running Simple.pl in a Windows XP command window.

Figure 26-8

How It Works

The first line added to Perl files by Komodo 3.0 is as follows:

#!/usr/bin/perl -w

The # character indicates a Perl comment. All characters from the # character to the end of the line are normally treated as a Perl comment. However, when the #! character sequence constitutes the first two characters of the first line of a program on Unix platforms, it indicates the location of the program that

665

Chapter 26

will run that code. If, like me, you are running Perl on Windows, you can ignore this line. On Unix-like operating systems, you may need to edit that line if the perl program is located in a directory different from that indicated by the first line.

The other line that Komodo 3.0 adds automatically is the strict pragma. This indicates to the perl engine that potentially questionable programming practices should not be allowed. The form in the line that follows indicates that all the options of the strict pragma are applied. Put simply, that means that the perl engine will point out a range of potential problems with your code:

use strict;

There are three more narrowly scoped versions of the strict pragma:

use strict “vars”; Generates an error if you access a variable that wasn’t declared via our or use vars, was localized via my, or wasn’t fully qualified

use strict “refs”; Generates an error if you use symbolic references

use strict “subs”; Generates a compile-time error if you try to use a bareword identifier that is not a subroutine

One of the practical effects of the strict pragma is that you cannot simply use a variable such as $myString without indicating something about the scope of the variable. This example uses my. The my keyword declares the variable to be local to the enclosing block or file.

The following line assigns the string value Hello world! to the $myString variable that has local scope:

my $myString = “Hello world!”;

The print operator is used to print the value held in the $myString variable to the command window and then prints a newline character, as indicated by \n. You may find the inclusion of $myString inside paired double quotes surprising. In Perl, you simply include a variable inside paired quotes when you want to display its value using print:

print “$myString\n”;

That raises the question of how you print the $ sign. You simply precede it with a backslash character so that you can display which variable’s value was printed:

print “The preceding line printed the variable \$myString.\n”;

You can also use the print operator to display literal text:

print “This program has run without errors.”;

Of course, there is much more to Perl than this simple example, but it gives you some notion of how output can be displayed.

666