⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perlintro.pod

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 POD
📖 第 1 页 / 共 2 页
字号:
=head1 NAMEperlintro -- a brief introduction and overview of Perl=head1 DESCRIPTIONThis document is intended to give you a quick overview of the Perlprogramming language, along with pointers to further documentation.  Itis intended as a "bootstrap" guide for those who are new to thelanguage, and provides just enough information for you to be able toread other peoples' Perl and understand roughly what it's doing, orwrite your own simple scripts.This introductory document does not aim to be complete.  It does noteven aim to be entirely accurate.  In some cases perfection has beensacrificed in the goal of getting the general idea across.  You areI<strongly> advised to follow this introduction with more informationfrom the full Perl manual, the table of contents to which can be foundin L<perltoc>.Throughout this document you'll see references to other parts of thePerl documentation.  You can read that documentation using the C<perldoc>command or whatever method you're using to read this document.=head2 What is Perl?Perl is a general-purpose programming language originally developed fortext manipulation and now used for a wide range of tasks includingsystem administration, web development, network programming, GUIdevelopment, and more.The language is intended to be practical (easy to use, efficient,complete) rather than beautiful (tiny, elegant, minimal).  Its majorfeatures are that it's easy to use, supports both procedural andobject-oriented (OO) programming, has powerful built-in support for textprocessing, and has one of the world's most impressive collections ofthird-party modules.Different definitions of Perl are given in L<perl>, L<perlfaq1> andno doubt other places.  From this we can determine that Perl is differentthings to different people, but that lots of people think it's at leastworth writing about.=head2 Running Perl programsTo run a Perl program from the Unix command line:    perl progname.plAlternatively, put this as the first line of your script:    #!/usr/bin/env perl... and run the script as C</path/to/script.pl>.  Of course, it'll needto be executable first, so C<chmod 755 script.pl> (under Unix).(This start line assumes you have the B<env> program. You can also putdirectly the path to your perl executable, like in C<#!/usr/bin/perl>).For more information, including instructions for other platforms such asWindows and Mac OS, read L<perlrun>.=head2 Safety netPerl by default is very forgiving. In order to make it more robustit is recommended to start every program with the following lines:    #!/usr/bin/perl    use strict;    use warnings;The two additional lines request from perl to catch various commonproblems in your code. They check different things so you need both. Apotential problem caught by C<use strict;> will cause your code to stopimmediately when it is encountered, while C<use warnings;> will merelygive a warning (like the command-line switch B<-w>) and let your code run.To read more about them check their respective manual pages at L<strict>and L<warnings>.=head2 Basic syntax overviewA Perl script or program consists of one or more statements.  Thesestatements are simply written in the script in a straightforwardfashion.  There is no need to have a C<main()> function or anything ofthat kind.Perl statements end in a semi-colon:    print "Hello, world";Comments start with a hash symbol and run to the end of the line    # This is a commentWhitespace is irrelevant:    print        "Hello, world"        ;... except inside quoted strings:    # this would print with a linebreak in the middle    print "Hello    world";Double quotes or single quotes may be used around literal strings:    print "Hello, world";    print 'Hello, world';However, only double quotes "interpolate" variables and specialcharacters such as newlines (C<\n>):    print "Hello, $name\n";     # works fine    print 'Hello, $name\n';     # prints $name\n literallyNumbers don't need quotes around them:    print 42;You can use parentheses for functions' arguments or omit themaccording to your personal taste.  They are only requiredoccasionally to clarify issues of precedence.    print("Hello, world\n");    print "Hello, world\n";More detailed information about Perl syntax can be found in L<perlsyn>.=head2 Perl variable typesPerl has three main variable types: scalars, arrays, and hashes.=over 4=item ScalarsA scalar represents a single value:    my $animal = "camel";    my $answer = 42;Scalar values can be strings, integers or floating point numbers, and Perlwill automatically convert between them as required.  There is no needto pre-declare your variable types, but you have to declare them usingthe C<my> keyword the first time you use them. (This is one of therequirements of C<use strict;>.)Scalar values can be used in various ways:    print $animal;    print "The animal is $animal\n";    print "The square of $answer is ", $answer * $answer, "\n";There are a number of "magic" scalars with names that look likepunctuation or line noise.  These special variables are used for allkinds of purposes, and are documented in L<perlvar>.  The only one youneed to know about for now is C<$_> which is the "default variable".It's used as the default argument to a number of functions in Perl, andit's set implicitly by certain looping constructs.    print;          # prints contents of $_ by default=item ArraysAn array represents a list of values:    my @animals = ("camel", "llama", "owl");    my @numbers = (23, 42, 69);    my @mixed   = ("camel", 42, 1.23);Arrays are zero-indexed.  Here's how you get at elements in an array:    print $animals[0];              # prints "camel"    print $animals[1];              # prints "llama"The special variable C<$#array> tells you the index of the last elementof an array:    print $mixed[$#mixed];       # last element, prints 1.23You might be tempted to use C<$#array + 1> to tell you how many items thereare in an array.  Don't bother.  As it happens, using C<@array> where Perlexpects to find a scalar value ("in scalar context") will give you the numberof elements in the array:    if (@animals < 5) { ... }The elements we're getting from the array start with a C<$> becausewe're getting just a single value out of the array -- you ask for a scalar,you get a scalar.To get multiple values from an array:    @animals[0,1];                  # gives ("camel", "llama");    @animals[0..2];                 # gives ("camel", "llama", "owl");    @animals[1..$#animals];         # gives all except the first elementThis is called an "array slice".You can do various useful things to lists:    my @sorted    = sort @animals;    my @backwards = reverse @numbers;There are a couple of special arrays too, such as C<@ARGV> (the commandline arguments to your script) and C<@_> (the arguments passed to asubroutine).  These are documented in L<perlvar>.=item HashesA hash represents a set of key/value pairs:    my %fruit_color = ("apple", "red", "banana", "yellow");You can use whitespace and the C<< => >> operator to lay them out morenicely:    my %fruit_color = (        apple  => "red",        banana => "yellow",    );To get at hash elements:    $fruit_color{"apple"};           # gives "red"You can get at lists of keys and values with C<keys()> andC<values()>.    my @fruits = keys %fruit_colors;    my @colors = values %fruit_colors;Hashes have no particular internal order, though you can sort the keysand loop through them.Just like special scalars and arrays, there are also special hashes.The most well known of these is C<%ENV> which contains environmentvariables.  Read all about it (and other special variables) inL<perlvar>.=backScalars, arrays and hashes are documented more fully in L<perldata>.More complex data types can be constructed using references, which allowyou to build lists and hashes within lists and hashes.A reference is a scalar value and can refer to any other Perl datatype. So by storing a reference as the value of an array or hashelement, you can easily create lists and hashes within lists andhashes. The following example shows a 2 level hash of hashstructure using anonymous hash references.    my $variables = {        scalar  =>  {                     description => "single item",                     sigil => '$',                    },        array   =>  {                     description => "ordered list of items",                     sigil => '@',                    },        hash    =>  {                     description => "key/value pairs",                     sigil => '%',                    },    };    print "Scalars begin with a $variables->{'scalar'}->{'sigil'}\n";Exhaustive information on the topic of references can be found inL<perlreftut>, L<perllol>, L<perlref> and L<perldsc>.=head2 Variable scopingThroughout the previous section all the examples have used the syntax:    my $var = "value";The C<my> is actually not required; you could just use:    $var = "value";However, the above usage will create global variables throughout yourprogram, which is bad programming practice.  C<my> creates lexicallyscoped variables instead.  The variables are scoped to the block(i.e. a bunch of statements surrounded by curly-braces) in which theyare defined.    my $x = "foo";    my $some_condition = 1;    if ($some_condition) {        my $y = "bar";        print $x;           # prints "foo"        print $y;           # prints "bar"    }    print $x;               # prints "foo"    print $y;               # prints nothing; $y has fallen out of scopeUsing C<my> in combination with a C<use strict;> at the top ofyour Perl scripts means that the interpreter will pick up certain commonprogramming errors.  For instance, in the example above, the finalC<print $b> would cause a compile-time error and prevent you fromrunning the program.  Using C<strict> is highly recommended.=head2 Conditional and looping constructsPerl has most of the usual conditional and looping constructs except forcase/switch (but if you really want it, there is a Switch module in Perl5.8 and newer, and on CPAN. See the section on modules, below, for moreinformation about modules and CPAN).The conditions can be any Perl expression.  See the list of operators inthe next section for information on comparison and boolean logic operators,which are commonly used in conditional statements.=over 4=item if    if ( condition ) {        ...    } elsif ( other condition ) {        ...    } else {        ...    }There's also a negated version of it:    unless ( condition ) {        ...    }This is provided as a more readable version of C<if (!I<condition>)>.Note that the braces are required in Perl, even if you've only got oneline in the block.  However, there is a clever way of making your one-line

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -