Home Links
Home Page
Use XML in PHP
Compression of the data on PHP
Use mod_perl
Style of coding on PHP
Perl and XML. Library of the programmer
Access to databases under management SUBD POSTGRES95
Parsing on Perl
XMLHttpRequest (AJAX) - sending and processing of answers of http-searches with help JavaScript.
Subsys_JsHttpRequest: pumping of the data without perezagruzki pages (AJAX)
The brief description of regular expressions: POSIX and PCRE
Optimization of searches in MySQL
Wound of treelike structures in Databases (Nested Sets)
Oracle / PHP FAQ
The specification and functions DOM in PHP
Not kehshirovat`!
Report PPP
Useful advice{councils} on optimization of ASP-applications
XML: time has come
 

Parsing on Perl

Bases of the analysis.


Perl it becomes fast the key tool of the usual system administrator and a magic hat of the system programmer.



It is easy to be frightened, however, of 211 pages of the documentation which is applied to last (fifth) release Perl. Perhaps, you already wonder " from what to start? " And " how much all should be known to write programs on Perl? "



Most easy - to see, as someone another solves a simple problem.


Let's take for an example a typical problem  of system administration - assignment to the new user unique ID numbers{rooms}. For this purpose it is necessary to define{determine} greatest of available in your system ID, and to choose the following greater number.




Let's disassemble the problem  which has turned up to us on simple problems{tasks} and their decisions.


[? We'll build up to the task at hand by looking at some simpler problems and their solutions.]



First, we shall see, as the first column of a conclusion of the command is unpacked{printed out}



who, [just for grins].



who | perl-ne ' @F = split; print " $F [0] n "; '


The conclusion who is passed to input Perl. The key-n allows to execute some code, placing each entering line in a variable $ _. The key-e sets a code, and we can (and often we shall be) to combine keys in the shown image.



In our case you we have two expressions: operations split and print. split $ _ contents break into the list of words (meaning a separator between words a blank). The result receives a file @F.



Then operation [? operation. operation - it is bad, because it is similar on the operator, and it is function] print displays value of the first element masstva, completed by translation of a line (n). We shall notice, that access to the first lementu @F occurs through $F [0] because elements are numbered, starting{beginning} from zero (as in files C).



It is possible to save a little on a set if to bear{take out} division in arguments of the command line:



who | perl-ane ' print " $F [0] n " '


Let's notice, that here we have added a key-a which forces Perl to break contents $ _ in @F vtomaticheski the same as in the previous example we have made it obviously.



To type{collect} still hardly less, it is possible to add a key-l which does{makes} two things at once:



Deletes translation of a line from a variable $ _ before she  will be seen with our code (actually his  (code) does not excite, whether there is he (translation) there (in line)), and

Pastes translation of a line back on an output{exit}.


After that our small command - line example will look so:



who | perl-lane ' print $F [0] '


And, to reduce still slightly, we shall replace a key-n with a key-p which allows to print that has turned out in $ _ at the end of a code:



who | perl-lape ' $ _ = $F [0] '


Yes, really, we have won only one symbol. But all this taki one symbol, and, can, it will give the big savings if you will save on a symbol every day the nearest five years. Can and no.



The script equivalent to previous call Perl, will look approximately so:



*!/usr/bin/perl


$ = $/; * from-l


while (<>) {* from-p


   chop; * from-l


   @F = split; * from-a


   $ _ = $F [0]; * argument to-e


   print; * from-p


}


As you see, [] it is possible to set a rather big piece of a code several symbols in the command line.



The variable $ defines{determines} finishing suffix for each operation print, approximately how it is done{made} with variable ORS and awk. By default she is empty, that is the deduced{removed} line will look how is set.



Here we attach to this variable significance $/, a separator of entering recordings (as RS in awk). By default it "n". That is a separator of a conclusion same as the separator of input, and to printed will be added translation of a line.



Let's stop, at last, with who command. We shall pass to a real problem : pass on a file of passwords for reception of the greatest user ID.



The file of passwords differs from a conclusion of who command - here columns are divided{shared} not by blanks, and a colon. There are no problems - shall specify other symbol - separator:



perl-aF:-lne ' print $F [0] '/etc/passwd


And we shall receive the list of users on a standard conclusion. The key-F sets a colon as a separator. We shall notice, that we have put a key-a before-F, that, I think, it is quite logical - a separator of fields not imeeet sense if them to not divide{share}.



If at you Yellow Pages [Yellow Pages] are started, that is, I wanted to say, Network Information Services, to you, probably, it is required to extend passwords from here, instead of from a file to receive something useful:



ypcat passwd | perl-aF:-lne ' print $F [0] '


Here ypcat command gives out a password - like file on a standard conclusion where Perl command joyfully pinchs{licks off} it  as though it was a local file etc/password.



But it is names of users, not user ID. They in the third stolbce, in $F [2] (besides, it is shifted on one because readout begins with zero). It is a little podredaktiruem, and:



perl-aF:-lne ' print $F [2] '/etc/passwd


Now we have list of numbers. Already better. We need to define{determine} the greatest number, and to unpack{print out} the even greater.



For this purpose it is used a scalar variable $max. Initially $max it is not determined, and at comparison with other numbers will look as a zero.


So, job will be to compare number{room} of each user with $max, and to appropriate{give} $max this number{room} if he is more.



perl-aF:-lne ' $max = $F [2] if $max <$F [2]; print $max '/etc/passwd


Here we appropriate{give} $max value if the condition satisfies. In this case a condition



$max <$F [2]


It is calculated on each iteration of a cycle, and if result the true, occurs giving. This unique place in Perl where the logic sequence goes from right to left, instead of from left to right.



Now all this becomes inconveniently long so it is better to develop{unwrap} in a script:



*!/usr/bin/perl


$ = $/;


while (<>) {


   chop;


   @F = split/:/;


   $max = $F [2] if $max <$F [2];


   print $max;


}


Even better. However we need to feed to a script/etc/passwd, that is a little bit burdensome for causing. So we shall open a file/etc/passwd directly in the program.



*!/usr/bin/perl


open (PASSWD, "/etc/passwd");


$ = $/;


while (<PASSWD>) {


   chop;


   @F = split/:/;


   $max = $F [2] if $max <$F [2];


   print $max;


}


open () creates a descriptor [? filehandle. silly, actually, to translate handle as "descriptor"] for reading a file/etc/passwd.



Yours, zheltostranichniki [YP'ers], the decision will be on a pair of symbols dlinneee:



*!/usr/bin/perl


open (PASSWD, " ypcat passwd | ");


$ = $/;


while (<PASSWD>) {


   chop;


   @F = split/:/;


   $max = $F [2] if $max <$F [2];


   print $max;


}


Perl miraculously uses a conclusion of the command as a file. About that, what is it the command, instead of a file, testifies finishing vertical feature. This reminder on a stream [? pipe. it is not confident.] which is used when we write the program in the command line.



On an output{exit} of these last programs there will be a series of numbers with the greatest found user ID. Actually we need to unpack{print out} the latest number{room}. No, once again. Actually number{room}, the greater on unit is necessary for us. How it to make in the program? Simply. We shall simply bear{we shall simply take out} a seal from a cycle:



*!/usr/bin/perl


open (PASSWD, "/etc/passwd"); * or YP equivalent


$ = $/;


while (<PASSWD>) {


   chop;


   @F = split/:/;


   $max = $F [2] if $max <$F [2];


}


print $max + 1;


Do not forget + 1 to receive more, than the past the greatest.



Whew! We can fill this script in a file, transform in ispolnimyj a code, place it  somewhere in $PATH, and when new number{room} of the user is necessary for us, we shall simply call it  in return inverted commas [to specify], and


Let's receive correct value.



Or something about correct number{room}. As appeared, some systems (for example, SunOS on which I tested it), have the user nobody, with very much - very much big ID. If you start this program in the system and receive something like 65535, at you such too is.



So we need to exclude everything from our calculation, that is higher than any threshold. How it to make?



We admit{allow}, $max it is not necessary to establish, if $F [2] exceeds our threshold (we shall say, 30000). That does{makes} if hardly by more complex :



*!/usr/bin/perl


open (PASSWD, "/etc/passwd"); * or YP equivalent


$ = $/;


while (<PASSWD>) {


   chop;


   @F = split/:/;


   $max = $F [2] if $F [2] <30000 and $max <$F [2];


}


print $max + 1;


On it it is possible to stop I (hope). Anyway, in SunOS works.



Eventually zadachka appeared not absolutely tiny, but, at least, we have kept within dozen lines of a code. If the length of command lines of you does not frighten, we can offer such variant:



perl-aF:-lne ' $m = $ F [2] if $F [2] <30000 and $m <$F [2];


END {print $m+1} '/etc/passwd


The interesting moment: the block of expressions END is born{taken out} for limits of a meant cycle, there, where we have put it  in the developed{unwrapped} script.



If you are not familiar with Perl, probably, you need the good book. I can recommend two though I am a little bit biassed because it is involved in a spelling of both.



Learning Perl (O'Reilly and Associates, ISBN 1-56592-042-2) - gentle introduction [? gentle introduction] in language, with examples and the developed{unwrapped} answers. It is the book for those who " is familiar with UNIX, but in any way the guru ". But demands nekotrogo knowledge of dreams of programming.



Programming Perl (O'Reilly and Associates, ISBN 0-937175-64-1) - the huge all-round directory on language, in the co-authorship with founder Perl, Larri Uollom. Here you will find a little the superficial training information, and weight of long practical examples. However it more likely the book for the guru, also can fly by by a head, if you on khakaete UNIX with 1977, as I.