How to recover Perl/CGI a script
Given clause{article} is called to help to take " fast start " to those who has decided to build Web-applications " the hands " and has chosen as means for their creation programming language Perl (Practical Electric Rubbish Lister). Here you will find data on that where "to take" the Perl-interpreter and as it{him} to establish what ways of debugging of the Perl-program exist, and in end will familiarize with the most elementary Perl-programmkoj.
Let's start with that Perl is language for creation of server scripts (a literary equivalent - the script), i.e. the Perl-program is carried out on the Web-server, instead of on a computer of the user, as, for example JavaScript-scripts or Java-applets. Therefore for check of your creations of one browser will be a little, it is necessary to have access to the Web-server. The most simple way of the sanction of the given problem - to use for debugging the Perl-program the server in Internet on which performance of scripts is authorized. But in this case, - you spend the first terribly earned denezhki, the second - enjoy " speed of connection with Internet "-). I want to offer you some alternative methods of debugging of Perl-programs which, I hope, will relieve you of the above described inconveniences.
So,
Metod1. To establish on the computer the Web-server and to work with it{him} as with the usual Internet-server.
Metod2. To use special programs - debuggers.
Metod3. To redirect results of job of the interpreter to a file (a standard opportunity of any OS).
Each of these ways has merits and demerits. But before to apply one of the above described methods, it is necessary to adjust the Perl-interpreter. First of all it is necessary to load the interpreter from Internet. For OC Windows 9x it is necessary to download a file to the following URL-address: http://www.activeware.com/download/ActivePerl/Windows/5.6/ActivePerl-5.6.0.613.msi. If you use other OS, load page to the address http://www.activeware.com/download, on her choose the corresponding link then "download" the file offered{suggested} for this purpose.
After the termination{ending} of process of loading, start file ActivePerl-5.6.0.613.msi or a corresponding file for your OS. Procedure of installation during which you should answer some questions will begin. In particular, about in what directory it is necessary to establish the interpreter (to establish Perl it is desirable on the following way: disk:usr).
The Web-server (for example, Apache) "learns{finds out}" about what program is necessary for starting for interpretation of a script from the first line of a program code of a script. For example, a line *!/usr/bin/perl means, that the interpreter is in a directory/usr/bin and has a name perl.
Further all set forth above methods of debugging of Perl-programs are in detail considered{examined}.
Method 1
The main advantage of the given method is that job with your server is completely similar to job with the "real" Internet-server. The basic lack consists that you see only final result of performance of a script, and cannot track his{its} performance on steps. Peak efficiency is reached{achieved} at use of the given method in a complex with methods 2 and 3 which are described below.
As the Web-server I recommend to use Apache (it is one of the most widespread, reliable servers, and the main thing, that he free-of-charge). For correct job Apache under Windows 95 it is necessary to establish Winsock 2 (for Windows 98 and Windows NT 4.0 this procedure is not required). Load from a site of firm Microsoft a file W95ws2setup.exe (http://www.microsoft.com/windows/downloads/bin/W95ws2setup.exe) and start it{him}. There will be installation Winsock 2. Now we download a file apache_1_3_12_win32.exe for installation Apache under Windows (http://www.apache.org/dist/binaries/win32/apache_1_3_12_win32.exe). After installation of the server, it{he} is necessary for configuring. For this purpose in any text editor we open a file httpd.conf which is in podpapke Conf folders in which has been established Apache. Further execute the following actions on a configuration of the server:
1. We find line DocumentRoot and the physical way to the root (site) is registered. At me, for example, this way the following " C:/FrontPage Webs/Content/mudconnector / " (notice, that at the instruction{indication} of a way is used Unix a separator, even in Windows environment). The root is for the server a starting point by search of documents. The reference{manipulation} to the root is carried out to the URL-address in which the name of the server (a name of a folder or page is specified only is not underlined). Usually at the use of the term the catalogue lower{omit} a word "virtual". Virtual catalogues represent directories (folder), access to which through Web is carried out on a logic name (pseudonym). Physically these folders can be in any place on your hard disk or on the other computer. At creation to the virtual catalogue the pseudonym which is put in conformity to a physical way to it{him} is appropriated{given}.
2. Hardly there is a line with word Directory below and the way (" C:/FrontPage Webs/Content/mudconnector / ") is registered the same.
3. The line with words Options All speaks that all opportunities of the server including support cgi are switched on.
4. In line with words ScriptAlias/cgi-bin/we specify a way to cgi-bin to the catalogue (in my case this way " C:/Apache/cgi-bin / ").
5. Hardly is lower in line with word Directory the same way "C:/Apache/cgi-bin" is specified
6. We add line AddHandler cgi-script .cgi .pl. Thus we let know to the server, that all faly with expansion .cgi.pl - are cgi executed, i.e. programs.
So, adjustment of the server is completed. If you have made all correctly after start of a file apache.exe on the screen the message such as " Apache/1.3.12 (Win32) running... " will be deduced{removed}. Now load a browser and enter the URL-address http://localhost. The home page of the root Web-site will be loaded in a browser - index.html (on my computer a physical way to this page C:/FrontPage Webs/Content/mudconnector/index.html)
Now we shall check up, how the Perl-program on our server is carried out. For this purpose we shall create unpretentious programmku which text is resulted below:
*!/usr/bin/perl
print " Content-type: text/htmlnn ";
print " Ur works! n ";
In the text editor create a new file and enter the above-stated code of the program. Save the given file, for example, under a name test.pl in a folder cgi-bin which is in a folder in which you have established Apache. After that pass to a browser and enter the URL-address http://localhost/cgi-bin/test.pl. In a browser the page will be loaded, the containing long-awaited message of Ur works! It means, that you have won! Now it is possible to cry " Ur! "
Method 2
Debugger, what this sweet word for the programmer. Certainly, using a debugger, you on the order will faster reach these mean glitches which prevent to make the working program. But give seriously. There are very many programs - debuggers for Perl-programs. All of them have pluss and minuses. We shall consider most popular of them:
Perl Builder (http://www.querybot.com/perlbuilderev.exe) - one of the most powerful and convenient programs - debuggers under W32. From the most useful features of the given program it is possible to allocate: the powerful mechanism of debugging of scripts, an opportunity of viewing of values of the variables, very convenient interface of emulation of Cgi-searches, the pleasant (friendly) user interface. A unique minus, as for us, inhabitants of the CIS, it that the given program commercial
DzSoft Perl Editor (http://www.dzsoft.com/download/dzperl22.zip) - this program also commercial. The small and very convenient program. Features: an opportunity upload-? scripts on the server, a fast insert (an insert of the certain piece of a code pressing, one key), an opportunity of editing of variables of an environment, an opportunity to work in different codings that is very convenient for the Russian-speaking user.
Perl Studio (http://www.perl-studio.com/pstudio.exe) - very powerful tool for creation Perl of scripts, functions of debugging are realized more poorly, than at above mentioned "colleagues", but many functions, scripts useful to the founder are built - in: automation of a spelling of a code (for example, SQL searches, SSI - directives), set of assistants and plotters, the intuitive interface, the good documentation. Besides, the program commercial.
Method 3
The most simple method of job with Perl-scripts. This method, does not demand what additional programs except for Perl - the interpreter and a browser. The essence of a method is very simple: after creation Perl of a script we start the interpreter and we redirect results of job to a file (for example, for a script test.pl - perl test.pl> test.htm). After that in the received file we delete line Content-type: text/htmlnn. Looking through the given file a browser, you receive result of job of a script.
It is recommended "not go in cycles" on any one of these methods, and to try to use the combined approach.
Job with khehshami in Perl
Initialization (clearing) khehsha
The fastest way of clearing is an assignment of the empty list.
Realization
my %hash = ();
The note
Often ask how to initialize the index on khehsh (hash ref). The index is a scalar variable and she is initialized in appropriate way. For example:
my $hash_ref;
my $hash_ref = 0; * zero
Addition of pair key / value in khehsh
In the examples resulted below, inverted commas around of keys can be lowered{omitted}, if keys - identifiers.
KHehsh:
The decision
$hash {' key '} = ' value '; * khehsh
$hash {$key} = $value; * khehsh, with use of a variable
The index on khehsh:
The decision
$href-> {' key '} = ' value '; * the index on khehsh
$href-> {$key} = $value; * the index on khehsh, with use of a variable
Addition of several pairs key / value in khehsh
The decision
These operations are equivalent, simply the second is more readable.
%hash = (' key1 ',' value1 ',' key2 ',' value2 ',' key3 ',' value3 ');
%hash = (
key1 => ' value1 ',
key2 => ' value2 ',
key3 => ' value3 ',
);
Copying khehshej
The decision
my %hash_copy = %hash; * copying khehsha
my $href_copy = $href; * copying of the index on khehsh
Removal{Distance} of one pair key / value
Not looking on that that removal{distance} khehsha and removal{distance} of the index on khehsh, is different the operation, both they are carried out with the help of function delete.
The decision
KHehsh:
delete $hash {$key};
The index on khehsh:
delete $hash_ref-> {$ key};
Perebor all pairs key / value
The example resulted below, prints all pairs key / value.
The decision
Use of function each with a cycle while. Pay attention, that each will touch pairs in the casual order, but the order will coincide with pereborom with the help of functions keys and values.
while (my ($key, $value) = each (%hash)) {
print " $key => $valuen ";
}
For the index on khehsh small difference:
while (my ($key, $value) = each (% $hash_ref)) {
print " $key => $valuen ";
}
The decision
Use of function keys with a cycle for
for my $key (keys %hash) {
my $value = $hash {$key};
print " $key => $valuen ";
}
Example
my $file = $ARGV [0] || "-";
my %from = ();
open FILE, " <$file " or die " Can't open $file: $! ";
while (<FILE>) {
if (/^From: (. *)/) {$from {$1} ++} * we count recurrence of the sender
}
close FILE;
for my $sender (sort keys %from) {
print " $sender: $from {$sender} n ";
}
Reception of the size khehsha
The decision
print " size of hash: ". keys (%hash.) ".n";
Solution
my $i = 0;
$i + = scalar keys % $hash_ref; * a method 1: an obvious scalar context
$i + = keys % $hash_ref; * a method 2: an implicit scalar context
Use of indexes on khehsh
The decision
sub foo
{
my $hash_ref;
$hash_ref-> {' key1 '} = ' value1 ';
$hash_ref-> {' key2 '} = ' value2 ';
$hash_ref-> {' key3 '} = ' value3 ';
return $hash_ref;
}
my $hash_ref = foo ();
print " keys... ", sort keys % $hash_ref, "... n ";
Function building khehsh from khehshej and returning the index on khehsh
The decision
sub foo
{
my ($login, $p, $uid, $gid, $gecos, $dir, $s);
my %HoH = ();
my $file = '/etc/passwd ';
open (PASSWD, " <$file ") or die " Can't open $file: $! ";
while (<PASSWD>) {
($login, $p, $uid, $gid, $gecos, $dir, $s) = split (': ');
$HoH {$login} {' uid '} = $uid;
$HoH {$login} {' gid '} = $gid;
$HoH {$login} {' dir '} = $dir;
}
close PASSWD;
return %HoH;
}
Access to khehshu from khehshej with the help of indexes. A conclusion of values
The decision
my $rHoH = foo ();
my ($uid, $gid, $dir);
for my $login (keys % $rHoH) {
$uid = $rHoH-> {$login}-> {' uid '}; * a method 1. More readable
$gid = $ {$rHoH-> {$login}} {' gid '}; * a method 2
$dir = $ {$ {$rHoH} {$login}} {' dir '}; * a method 3. Less readable
print " uid: $uid, gid: $gid, dir, $dir.n ";
}
The decision
my $rHoH = foo ();
for my $k1 (sort keys % $rHoH) {
print " k1: $k1n ";
for my $k2 (keys % {$rHoH-> {$k1}}) {
print " k2: $k2 $rHoH-> {$k1} {$k2} n ";
}
}
Function building khehsh from kheshej from kheshej and returning the index on khehsh
The decision
sub foo
{
my %HoHoH = ();
while(... {
if (/LOCATION:/) {
...
} elsif (/MODULE:/) {
$HoHoH {$loc} {$module_type} {MODULE_NAME} = $module_name;
} elsif ($ARGS_ALLOWED) {
$HoHoH {$loc} {$module_type} {$arg_name} = $arg_value;
}
}
return %HoHoH;
}
Access to khehshu from khehshej from khehshej with the help of indexes. A conclusion of values.
The decision
my $rHoH = foo ();
for my $k1 (sort keys % $rHoHoH) {
print "$k1n";
for my $k2 (sort keys % {$rHoHoH-> {$k1}}) {
print "t$k2n";
for my $k3 (sort keys % {$rHoHoH-> {$k1}-> {$k2}}) {
print " tt$k3 => $rHoHoH-> {$k1}-> {$k2}-> {$k3} n ";
}
}
}
Conclusion of keys and values from khehsha, the index received with the help
The decision
while (my ($k, $v) = each % $hash_ref) {
print " a key: $k, value: $v.n ";
}
Definition of existence of value in khehshe
The decision
print " Value EXISTS, but can be not opredeleno.n "
if exists $hash {$key};
print " Value is DETERMINED, but can be false.n "
if defined $hash {$key};
print " Value TRUEn "
if $hash {$key};
Example
We admit{allow}, we have executed SQL search which can return the recordings containing value NULL. Before using result of search to us it is necessary to check up, whether the received values are DETERMINED. Pay attention, function sql_fetch_hashref () incorporates to the server of databases, prepares the search, carries out it{him} and receives the index on khehsh with help DBI of function fetchrow_hashref ().
my $answers = ' a, b, c, d, e ';
my $sql = " select max_time, $answers from questions ".
' where question_number =? ';
my $hash_ref = sql_fetch_hashref ($sql, $q);
my @answers = split ', ', $answers;
my $max_time = $hash_ref-> {max_time} || ' 60 ';
my $hash_ref_ans;
for my $letter (@answers) {
$hash_ref_ans-> {$letter} = $hash_ref-> {$letter}
if defined $hash_ref-> {$letter};
}
The cycle for creates khehsh only with CERTAIN{DETERMINED} in the pairs key / value.
Job with files in Perl
The author of clause{article} does not give any guarantees, that the code resulted in her being "transformed" with help Copy/Past will earn. All examples worked at the moment of a spelling of clause{article}.
Initialization (clearing) of a file.
Realization
my @array = ();
Realization
$ *array is an index (serial number) of last element of a file (on unit less, than quantity{amount} of elements in a file as the file begins with zero). Assignment $ *array changes length of a file @array, thus you can destroy (or to clear) all elements of a file between set at assignment and the last. At assignment-1, all elements are destroyed also a file is cleared. I recommend the following decision:
$ *array =-1;
Definition of existence of an element of a file
Realization
print " Value EXISTS, but can be neopredelenno.n "
if exists $array [$index];
print " Value is DETERMINED, but can be false.n "
if defined $array [$index];
print " Zacheniie an element with an index $index TRUE.n "
if $array [$index];
Definition of quantity{amount} of elements in a file.
Realization
If you want to deduce{remove} quantity{amount} of elements in a file, the most simple way:
print " size of array: ". @array. ".n";
Realization
If you want to appropriate{give} the size of a file of a variable simply use a scalar context:
$size = @array;
print " size of array: $size.n ";
Obviously, the scalar context can be received with the help of function scalar
$size = scalar @array;
print " size of array: $size.n ";
Besides there is one more way of reception of quantity{amount} of elements of a file. I recommend to use it{him}:
Realization
$size = $ *array + 1;

|