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

📄 perlintro.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 2 页
字号:
\&\&    print "Scalars begin with a $variables\->{\*(Aqscalar\*(Aq}\->{\*(Aqsigil\*(Aq}\en";.Ve.PPExhaustive information on the topic of references can be found inperlreftut, perllol, perlref and perldsc..Sh "Variable scoping".IX Subsection "Variable scoping"Throughout the previous section all the examples have used the syntax:.PP.Vb 1\&    my $var = "value";.Ve.PPThe \f(CW\*(C`my\*(C'\fR is actually not required; you could just use:.PP.Vb 1\&    $var = "value";.Ve.PPHowever, the above usage will create global variables throughout yourprogram, which is bad programming practice.  \f(CW\*(C`my\*(C'\fR 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..PP.Vb 9\&    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 scope.Ve.PPUsing \f(CW\*(C`my\*(C'\fR in combination with a \f(CW\*(C`use strict;\*(C'\fR at the top ofyour Perl scripts means that the interpreter will pick up certain commonprogramming errors.  For instance, in the example above, the final\&\f(CW\*(C`print $b\*(C'\fR would cause a compile-time error and prevent you fromrunning the program.  Using \f(CW\*(C`strict\*(C'\fR is highly recommended..Sh "Conditional and looping constructs".IX Subsection "Conditional and looping constructs"Perl 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 \s-1CPAN\s0. See the section on modules, below, for moreinformation about modules and \s-1CPAN\s0)..PPThe 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..IP "if" 4.IX Item "if".Vb 7\&    if ( condition ) {\&        ...\&    } elsif ( other condition ) {\&        ...\&    } else {\&        ...\&    }.Ve.SpThere's also a negated version of it:.Sp.Vb 3\&    unless ( condition ) {\&        ...\&    }.Ve.SpThis is provided as a more readable version of \f(CW\*(C`if (!\f(CIcondition\f(CW)\*(C'\fR..SpNote 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-lineconditional blocks more English like:.Sp.Vb 4\&    # the traditional way\&    if ($zippy) {\&        print "Yow!";\&    }\&\&    # the Perlish post\-condition way\&    print "Yow!" if $zippy;\&    print "We have no bananas" unless $bananas;.Ve.IP "while" 4.IX Item "while".Vb 3\&    while ( condition ) {\&        ...\&    }.Ve.SpThere's also a negated version, for the same reason we have \f(CW\*(C`unless\*(C'\fR:.Sp.Vb 3\&    until ( condition ) {\&        ...\&    }.Ve.SpYou can also use \f(CW\*(C`while\*(C'\fR in a post-condition:.Sp.Vb 1\&    print "LA LA LA\en" while 1;          # loops forever.Ve.IP "for" 4.IX Item "for"Exactly like C:.Sp.Vb 3\&    for ($i = 0; $i <= $max; $i++) {\&        ...\&    }.Ve.SpThe C style for loop is rarely needed in Perl since Perl providesthe more friendly list scanning \f(CW\*(C`foreach\*(C'\fR loop..IP "foreach" 4.IX Item "foreach".Vb 3\&    foreach (@array) {\&        print "This element is $_\en";\&    }\&\&    print $list[$_] foreach 0 .. $max;\&\&    # you don\*(Aqt have to use the default $_ either...\&    foreach my $key (keys %hash) {\&        print "The value of $key is $hash{$key}\en";\&    }.Ve.PPFor more detail on looping constructs (and some that weren't mentioned inthis overview) see perlsyn..Sh "Builtin operators and functions".IX Subsection "Builtin operators and functions"Perl comes with a wide selection of builtin functions.  Some of the oneswe've already seen include \f(CW\*(C`print\*(C'\fR, \f(CW\*(C`sort\*(C'\fR and \f(CW\*(C`reverse\*(C'\fR.  A list ofthem is given at the start of perlfunc and you can easily readabout any given function by using \f(CW\*(C`perldoc \-f \f(CIfunctionname\f(CW\*(C'\fR..PPPerl operators are documented in full in perlop, but here are a fewof the most common ones:.IP "Arithmetic" 4.IX Item "Arithmetic".Vb 4\&    +   addition\&    \-   subtraction\&    *   multiplication\&    /   division.Ve.IP "Numeric comparison" 4.IX Item "Numeric comparison".Vb 6\&    ==  equality\&    !=  inequality\&    <   less than\&    >   greater than\&    <=  less than or equal\&    >=  greater than or equal.Ve.IP "String comparison" 4.IX Item "String comparison".Vb 6\&    eq  equality\&    ne  inequality\&    lt  less than\&    gt  greater than\&    le  less than or equal\&    ge  greater than or equal.Ve.Sp(Why do we have separate numeric and string comparisons?  Because we don'thave special variable types, and Perl needs to know whether to sortnumerically (where 99 is less than 100) or alphabetically (where 100 comesbefore 99)..IP "Boolean logic" 4.IX Item "Boolean logic".Vb 3\&    &&  and\&    ||  or\&    !   not.Ve.Sp(\f(CW\*(C`and\*(C'\fR, \f(CW\*(C`or\*(C'\fR and \f(CW\*(C`not\*(C'\fR aren't just in the above table as descriptionsof the operators \*(-- they're also supported as operators in their ownright.  They're more readable than the C\-style operators, but havedifferent precedence to \f(CW\*(C`&&\*(C'\fR and friends.  Check perlop for moredetail.).IP "Miscellaneous" 4.IX Item "Miscellaneous".Vb 4\&    =   assignment\&    .   string concatenation\&    x   string multiplication\&    ..  range operator (creates a list of numbers).Ve.PPMany operators can be combined with a \f(CW\*(C`=\*(C'\fR as follows:.PP.Vb 3\&    $a += 1;        # same as $a = $a + 1\&    $a \-= 1;        # same as $a = $a \- 1\&    $a .= "\en";     # same as $a = $a . "\en";.Ve.Sh "Files and I/O".IX Subsection "Files and I/O"You can open a file for input or output using the \f(CW\*(C`open()\*(C'\fR function.It's documented in extravagant detail in perlfunc and perlopentut,but in short:.PP.Vb 3\&    open(my $in,  "<",  "input.txt")  or die "Can\*(Aqt open input.txt: $!";\&    open(my $out, ">",  "output.txt") or die "Can\*(Aqt open output.txt: $!";\&    open(my $log, ">>", "my.log")     or die "Can\*(Aqt open my.log: $!";.Ve.PPYou can read from an open filehandle using the \f(CW\*(C`<>\*(C'\fR operator.  Inscalar context it reads a single line from the filehandle, and in listcontext it reads the whole file in, assigning each line to an element ofthe list:.PP.Vb 2\&    my $line  = <$in>;\&    my @lines = <$in>;.Ve.PPReading in the whole file at one time is called slurping. It canbe useful but it may be a memory hog. Most text file processingcan be done a line at a time with Perl's looping constructs..PPThe \f(CW\*(C`<>\*(C'\fR operator is most often seen in a \f(CW\*(C`while\*(C'\fR loop:.PP.Vb 3\&    while (<$in>) {     # assigns each line in turn to $_\&        print "Just read in this line: $_";\&    }.Ve.PPWe've already seen how to print to standard output using \f(CW\*(C`print()\*(C'\fR.However, \f(CW\*(C`print()\*(C'\fR can also take an optional first argument specifyingwhich filehandle to print to:.PP.Vb 3\&    print STDERR "This is your final warning.\en";\&    print $out $record;\&    print $log $logmessage;.Ve.PPWhen you're done with your filehandles, you should \f(CW\*(C`close()\*(C'\fR them(though to be honest, Perl will clean up after you if you forget):.PP.Vb 1\&    close $in or die "$in: $!";.Ve.Sh "Regular expressions".IX Subsection "Regular expressions"Perl's regular expression support is both broad and deep, and is thesubject of lengthy documentation in perlrequick, perlretut, andelsewhere.  However, in short:.IP "Simple matching" 4.IX Item "Simple matching".Vb 2\&    if (/foo/)       { ... }  # true if $_ contains "foo"\&    if ($a =~ /foo/) { ... }  # true if $a contains "foo".Ve.SpThe \f(CW\*(C`//\*(C'\fR matching operator is documented in perlop.  It operates on\&\f(CW$_\fR by default, or can be bound to another variable using the \f(CW\*(C`=~\*(C'\fRbinding operator (also documented in perlop)..IP "Simple substitution" 4.IX Item "Simple substitution".Vb 3\&    s/foo/bar/;               # replaces foo with bar in $_\&    $a =~ s/foo/bar/;         # replaces foo with bar in $a\&    $a =~ s/foo/bar/g;        # replaces ALL INSTANCES of foo with bar in $a.Ve.SpThe \f(CW\*(C`s///\*(C'\fR substitution operator is documented in perlop..IP "More complex regular expressions" 4.IX Item "More complex regular expressions"You don't just have to match on fixed strings.  In fact, you can matchon just about anything you could dream of by using more complex regularexpressions.  These are documented at great length in perlre, but forthe meantime, here's a quick cheat sheet:.Sp.Vb 10\&    .                   a single character\&    \es                  a whitespace character (space, tab, newline, ...)\&    \eS                  non\-whitespace character\&    \ed                  a digit (0\-9)\&    \eD                  a non\-digit\&    \ew                  a word character (a\-z, A\-Z, 0\-9, _)\&    \eW                  a non\-word character\&    [aeiou]             matches a single character in the given set\&    [^aeiou]            matches a single character outside the given set\&    (foo|bar|baz)       matches any of the alternatives specified\&\&    ^                   start of string\&    $                   end of string.Ve.SpQuantifiers can be used to specify how many of the previous thing youwant to match on, where \*(L"thing\*(R" means either a literal character, oneof the metacharacters listed above, or a group of characters ormetacharacters in parentheses..Sp.Vb 6\&    *                   zero or more of the previous thing\&    +                   one or more of the previous thing\&    ?                   zero or one of the previous thing\&    {3}                 matches exactly 3 of the previous thing\&    {3,6}               matches between 3 and 6 of the previous thing\&    {3,}                matches 3 or more of the previous thing.Ve.SpSome brief examples:.Sp.Vb 6\&    /^\ed+/              string starts with one or more digits\&    /^$/                nothing in the string (start and end are adjacent)\&    /(\ed\es){3}/         a three digits, each followed by a whitespace\&                        character (eg "3 4 5 ")\&    /(a.)+/             matches a string in which every odd\-numbered letter\&                        is a (eg "abacadaf")\&\&    # This loop reads from STDIN, and prints non\-blank lines:\&    while (<>) {\&        next if /^$/;\&        print;\&    }.Ve.IP "Parentheses for capturing" 4.IX Item "Parentheses for capturing"As well as grouping, parentheses serve a second purpose.  They can beused to capture the results of parts of the regexp match for later use.The results end up in \f(CW$1\fR, \f(CW$2\fR and so on..Sp.Vb 1\&    # a cheap and nasty way to break an email address up into parts\&\&    if ($email =~ /([^@]+)@(.+)/) {\&        print "Username is $1\en";\&        print "Hostname is $2\en";\&    }.Ve.IP "Other regexp features" 4.IX Item "Other regexp features"Perl regexps also support backreferences, lookaheads, and all kinds ofother complex details.  Read all about them in perlrequick,perlretut, and perlre..Sh "Writing subroutines".IX Subsection "Writing subroutines"Writing subroutines is easy:.PP.Vb 5\&    sub logger {\&        my $logmessage = shift;\&        open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";\&        print $logfile $logmessage;\&    }.Ve.PPNow we can use the subroutine just as any other built-in function:.PP.Vb 1\&    logger("We have a logger subroutine!");.Ve.PPWhat's that \f(CW\*(C`shift\*(C'\fR?  Well, the arguments to a subroutine are availableto us as a special array called \f(CW@_\fR (see perlvar for more on that).The default argument to the \f(CW\*(C`shift\*(C'\fR function just happens to be \f(CW@_\fR.So \f(CW\*(C`my $logmessage = shift;\*(C'\fR shifts the first item off the list ofarguments and assigns it to \f(CW$logmessage\fR..PPWe can manipulate \f(CW@_\fR in other ways too:.PP.Vb 2\&    my ($logmessage, $priority) = @_;       # common\&    my $logmessage = $_[0];                 # uncommon, and ugly.Ve.PPSubroutines can also return values:.PP.Vb 5\&    sub square {\&        my $num = shift;\&        my $result = $num * $num;\&        return $result;\&    }.Ve.PPThen use it like:.PP.Vb 1\&    $sq = square(8);.Ve.PPFor more information on writing subroutines, see perlsub..Sh "\s-1OO\s0 Perl".IX Subsection "OO Perl"\&\s-1OO\s0 Perl is relatively simple and is implemented using references whichknow what sort of object they are based on Perl's concept of packages.However, \s-1OO\s0 Perl is largely beyond the scope of this document.Read perlboot, perltoot, perltooc and perlobj..PPAs a beginning Perl programmer, your most common use of \s-1OO\s0 Perl will bein using third-party modules, which are documented below..Sh "Using Perl modules".IX Subsection "Using Perl modules"Perl modules provide a range of features to help you avoid reinventingthe wheel, and can be downloaded from \s-1CPAN\s0 ( http://www.cpan.org/ ).  Anumber of popular modules are included with the Perl distributionitself..PPCategories of modules range from text manipulation to network protocolsto database integration to graphics.  A categorized list of modules isalso available from \s-1CPAN\s0..PPTo learn how to install modules you download from \s-1CPAN\s0, readperlmodinstall.PPTo learn how to use a particular module, use \f(CW\*(C`perldoc \f(CIModule::Name\f(CW\*(C'\fR.Typically you will want to \f(CW\*(C`use \f(CIModule::Name\f(CW\*(C'\fR, which will then giveyou access to exported functions or an \s-1OO\s0 interface to the module..PPperlfaq contains questions and answers related to many commontasks, and often provides suggestions for good \s-1CPAN\s0 modules to use..PPperlmod describes Perl modules in general.  perlmodlib lists themodules which came with your Perl installation..PPIf you feel the urge to write Perl modules, perlnewmod will give yougood advice..SH "AUTHOR".IX Header "AUTHOR"Kirrily \*(L"Skud\*(R" Robert <skud@cpan.org>

⌨️ 快捷键说明

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