мета-данные страницы
Загрузка не удалась. Возможно, проблемы с правами доступа?
Различия
Показаны различия между двумя версиями страницы.
Предыдущая версия справа и слеваПредыдущая версияСледующая версия | Предыдущая версия | ||
perl:task3 [06/03/2010 17:16] – vlasov | perl:task3 [23/10/2010 18:17] (текущий) – vlasov | ||
---|---|---|---|
Строка 1: | Строка 1: | ||
- | < | + | < |
- | <?xml version=" | ||
- | < | ||
< | < | ||
- | < | + | < |
- | <!-- saved from url=(0017)http:// | + | |
- | <script language=" | + | |
- | <script language=" | + | |
- | <script language=" | + | |
- | <link rel=" | + | |
- | < | + | |
- | <link rel=" | + | |
- | <meta http-equiv=" | + | |
- | <link rev=" | + | |
- | </ | + | |
< | < | ||
- | + | <p>Простой файл для удаления тэгов</p> | |
- | <!-- INDEX BEGIN --> | + | |
- | <div name=" | + | |
- | < | + | |
- | < | + | |
- | <p><a name=" | + | |
- | + | ||
- | + | ||
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | </ | + | |
- | + | ||
- | <hr name=" | + | |
- | </ | + | |
- | <!-- INDEX END --> | + | |
- | + | ||
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <p> | + | |
- | </ | + | |
- | <hr /> | + | |
- | < | + | |
- | < | + | |
- | programming language, along with pointers to further documentation. | + | |
- | is intended as a & | + | |
- | language, and provides just enough information for you to be able to | + | |
- | read other peoples' | + | |
- | write your own simple scripts.</ | + | |
- | < | + | |
- | even aim to be entirely accurate. | + | |
- | sacrificed in the goal of getting the general idea across. | + | |
- | < | + | |
- | from the full Perl manual, the table of contents to which can be found | + | |
- | in <a href=" | + | |
- | < | + | |
- | Perl documentation. | + | |
- | command or whatever method you're using to read this document.</ | + | |
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | text manipulation and now used for a wide range of tasks including | + | |
- | system administration, | + | |
- | development, | + | |
- | < | + | |
- | complete) rather than beautiful (tiny, elegant, minimal). | + | |
- | features are that it's easy to use, supports both procedural and | + | |
- | object-oriented (OO) programming, | + | |
- | processing, and has one of the world' | + | |
- | third-party modules.</ | + | |
- | < | + | |
- | no doubt other places. | + | |
- | things to different people, but that lots of people think it's at least | + | |
- | worth writing about.</ | + | |
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | <p>To run a Perl program from the Unix command line:</ | + | |
- | < | + | |
- | perl progname.pl</ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | to be executable first, so < | + | |
- | < | + | |
- | directly the path to your perl executable, like in < | + | |
- | < | + | |
- | Windows and Mac OS, read <a href=" | + | |
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | it is recommended to start every program with the following lines:</ | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | problems in your code. They check different things so you need both. A | + | |
- | potential problem caught by < | + | |
- | immediately when it is encountered, | + | |
- | give a warning (like the command-line switch < | + | |
- | To read more about them check their respective manual pages at <a href=" | + | |
- | and <a href=" | + | |
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | <p>A Perl script or program consists of one or more statements. | + | |
- | statements are simply written in the script in a straightforward | + | |
- | fashion. | + | |
- | that kind.</ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | world"</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | characters such as newlines (<a href=" | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | according to your personal taste. | + | |
- | occasionally to clarify issues of precedence.</ | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | <p>A scalar represents a single value:</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | will automatically convert between them as required. | + | |
- | to pre-declare your variable types, but you have to declare them using | + | |
- | the <a href=" | + | |
- | requirements of < | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | punctuation or line noise. | + | |
- | kinds of purposes, and are documented in <a href=" | + | |
- | need to know about for now is <a href=" | + | |
- | It's used as the default argument to a number of functions in Perl, and | + | |
- | it's set implicitly by certain looping constructs.</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | <p>An array represents a list of values:</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | of an array:</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | are in an array. | + | |
- | expects to find a scalar value (& | + | |
- | of elements in the array:</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | we're getting just a single value out of the array -- you ask for a scalar, | + | |
- | you get a scalar.</ | + | |
- | </ | + | |
- | < | + | |
- | <p>To get multiple values from an array:</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | line arguments to your script) and <a href=" | + | |
- | subroutine). | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | <p>A hash represents a set of key/value pairs:</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | nicely:</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | <p>To get at hash elements:</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <a href=" | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | and loop through them.</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | The most well known of these is <a href=" | + | |
- | variables. | + | |
- | <a href=" | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | you to build lists and hashes within lists and hashes.</ | + | |
- | <p>A reference is a scalar value and can refer to any other Perl data | + | |
- | type. So by storing a reference as the value of an array or hash | + | |
- | element, you can easily create lists and hashes within lists and | + | |
- | hashes. The following example shows a 2 level hash of hash | + | |
- | structure using anonymous hash references.</ | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | <a href=" | + | |
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | program, which is bad programming practice. | + | |
- | scoped variables instead. | + | |
- | (i.e. a bunch of statements surrounded by curly-braces) in which they | + | |
- | are defined.</ | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | your Perl scripts means that the interpreter will pick up certain common | + | |
- | programming errors. | + | |
- | < | + | |
- | running the program. | + | |
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | case/switch (but if you really want it, there is a Switch module in Perl | + | |
- | 5.8 and newer, and on CPAN. See the section on modules, below, for more | + | |
- | information about modules and CPAN).</ | + | |
- | < | + | |
- | the next section for information on comparison and boolean logic operators, | + | |
- | which are commonly used in conditional statements.</ | + | |
- | < | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | line in the block. | + | |
- | conditional blocks more English like:</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | the more friendly list scanning <a href="# | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | this overview) see <a href=" | + | |
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | we've already seen include <a href=" | + | |
- | them is given at the start of <a href=" | + | |
- | about any given function by using < | + | |
- | < | + | |
- | of the most common ones:</ | + | |
- | < | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | + | + | |
- | - | + | |
- | * | + | |
- | / | + | |
- | </ | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | == equality | + | |
- | != inequality | + | |
- | & | + | |
- | & | + | |
- | & | + | |
- | & | + | |
- | </ | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | eq equality | + | |
- | ne inequality | + | |
- | lt less than | + | |
- | gt greater than | + | |
- | le less than or equal | + | |
- | ge greater than or equal</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | have special variable types, and Perl needs to know whether to sort | + | |
- | numerically (where 99 is less than 100) or alphabetically (where 100 comes | + | |
- | before 99).</ | + | |
- | </ | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | & | + | |
- | || or | + | |
- | ! | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | of the operators -- they' | + | |
- | right. | + | |
- | different precedence to < | + | |
- | detail.)</ | + | |
- | </ | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | = | + | |
- | . | + | |
- | x | + | |
- | .. range operator (creates a list of numbers)</ | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | It's documented in extravagant detail in <a href=" | + | |
- | but in short:</ | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | scalar context it reads a single line from the filehandle, and in list | + | |
- | context it reads the whole file in, assigning each line to an element of | + | |
- | the list:</ | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | be useful but it may be a memory hog. Most text file processing | + | |
- | can be done a line at a time with Perl's looping constructs.</ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | However, <a href=" | + | |
- | which filehandle to print to:</ | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | (though to be honest, Perl will clean up after you if you forget):</ | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | subject of lengthy documentation in <a href=" | + | |
- | elsewhere. | + | |
- | < | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <a href=" | + | |
- | binding operator (also documented in <a href=" | + | |
- | </ | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | on just about anything you could dream of by using more complex regular | + | |
- | expressions. | + | |
- | the meantime, here's a quick cheat sheet:</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | . a single character | + | |
- | \s a whitespace character (space, tab, newline, ...) | + | |
- | \S non-whitespace character | + | |
- | \d a digit (0-9) | + | |
- | \D a non-digit | + | |
- | \w a word character (a-z, A-Z, 0-9, _) | + | |
- | \W a non-word character | + | |
- | [aeiou] | + | |
- | [^aeiou] | + | |
- | (foo|bar|baz) | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | ^ start of string | + | |
- | $ end of string</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | want to match on, where & | + | |
- | of the metacharacters listed above, or a group of characters or | + | |
- | metacharacters in parentheses.</ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | character (eg "3 4 5 ") | + | |
- | / | + | |
- | is a (eg " | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | <p>As well as grouping, parentheses serve a second purpose. | + | |
- | used to capture the results of parts of the regexp match for later use. | + | |
- | The results end up in < | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | other complex details. | + | |
- | <a href=" | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | to us as a special array called <a href=" | + | |
- | The default argument to the <a href=" | + | |
- | So < | + | |
- | arguments and assigns it to < | + | |
- | <p>We can manipulate <a href=" | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | <span class=" | + | |
- | </ | + | |
- | < | + | |
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | <p>OO Perl is relatively simple and is implemented using references which | + | |
- | know what sort of object they are based on Perl's concept of packages. | + | |
- | However, OO Perl is largely beyond the scope of this document. | + | |
- | Read <a href=" | + | |
- | <p>As a beginning Perl programmer, your most common use of OO Perl will be | + | |
- | in using third-party modules, which are documented below.</ | + | |
- | <p> | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | the wheel, and can be downloaded from CPAN ( <a href=" | + | |
- | number of popular modules are included with the Perl distribution | + | |
- | itself.</ | + | |
- | < | + | |
- | to database integration to graphics. | + | |
- | also available from CPAN.</ | + | |
- | <p>To learn how to install modules you download from CPAN, read | + | |
- | <a href=" | + | |
- | <p>To learn how to use a particular module, use < | + | |
- | Typically you will want to < | + | |
- | you access to exported functions or an OO interface to the module.</ | + | |
- | < | + | |
- | tasks, and often provides suggestions for good CPAN modules to use.</ | + | |
- | < | + | |
- | modules which came with your Perl installation.</ | + | |
- | <p>If you feel the urge to write Perl modules, <a href=" | + | |
- | good advice.</ | + | |
- | <p> | + | |
- | </ | + | |
- | <hr /> | + | |
- | < | + | |
- | < | + | |
</ | </ | ||
+ | </ | ||
- | </ | ||
</ | </ | ||
- | |||
- |