- 博客(78)
- 收藏
- 关注
原创 Learning Perl: 10.6. Autoincrement and Autodecrement
<br /> 10.6. Autoincrement and Autodecrement<br />You'll often want a scalar variable to count up or down by one. Since these are frequent constructs, there are shortcuts for them like nearly everything else we do frequently.<br />The autoincrement operato
2010-07-21 18:04:00
431
原创 Learning Perl: 10.5. The elsif Clause
<br /> 10.5. The elsif Clause<br />Every so often, you may need to check a number of conditional expressions, one after another, to see which one of them is true. This can be done with the if control structure's elsif clause, as in this example: if ( !
2010-07-21 18:03:00
560
原创 Learning Perl: 10.3. Expression Modifiers
<br /> 10.3. Expression Modifiers<br />For a more compact notation, an expression may be followed by a modifier that controls it. For example, the if modifier works in a way analogous to an if block: print "$n is a negative number./n" if $n < 0;<br />
2010-07-21 18:02:00
486
原创 Learning Perl: 10.4. The Naked Block Control Structure
<br /> 10.4. The Naked Block Control Structure<br />The so-called "naked" block is one without a keyword or condition. That is, suppose you start with a while loop, which looks something like this: while (condition) { body; body; bo
2010-07-21 18:02:00
506
原创 Learning Perl: 10.2. The until Control Structure
<br /> 10.2. The until Control Structure<br />Sometimes, you'll want to reverse the condition of a while loop. To do that, use until: until ($j > $i) { $j *= 2; }<br /><br />This loop runs until the conditional expression returns true. It'
2010-07-21 18:01:00
387
原创 Learning Perl: 9.5. More Powerful Regular Expressions
<br /> 9.5. More Powerful Regular Expressions<br />After reading (almost) three chapters about regular expressions, you know they're a powerful feature in the core of Perl. The Perl developers have added more features, and you'll see some of the most impor
2010-07-21 18:00:00
650
原创 Learning Perl: 10.1. The unless Control Structure
<br /> 10.1. The unless Control Structure<br />In an if control structure, the block of code is executed only when the conditional expression is true. If you want to execute a block of code only when the conditional is false, change if to unless: unless
2010-07-21 18:00:00
487
原创 Learning Perl: 9.2. The split Operator
<br /> 9.2. The split Operator<br />Another operator that uses regular expressions is split, which breaks up a string according to a pattern. This is useful for tab-separated, colon-separated, whitespace-separated, or anything-separated data.[] Anywhere yo
2010-07-21 17:59:00
345
原创 Learning Perl: 9.3. The join Function
<br /> 9.3. The join Function<br />The join function doesn't use patterns but performs the opposite function of split: split breaks up a string into a number of pieces, and join glues together a bunch of pieces to make a single string. The join function lo
2010-07-21 17:59:00
361
原创 Learning Perl: 9.4. m// in List Context
<br /> 9.4. m// in List Context<br />When you use split, the pattern specifies the separator: the part that isn't the useful data. Sometimes it's easier to specify what you want to keep.<br />When a pattern match (m//) is used in a list context, the return
2010-07-21 17:59:00
418
原创 Learning Perl: 8.9. A Pattern Test Program
<br /> 8.9. A Pattern Test Program<br />When in the course of Perl events it becomes necessary for a programmer to write a regular expression, it may be difficult to tell what the pattern will do. It's normal to find that a pattern matches more than you ex
2010-07-21 17:58:00
340
原创 Learning Perl: 9.1. Substitutions with s///
<br /> 9.1. Substitutions with s///<br />If you think of the m// pattern match as being like your word processor's "search" feature, the "search and replace" feature would be Perl's s/// substitution operator. This replaces whichever part of a variable[*]
2010-07-21 17:58:00
451
原创 Learning Perl: 8.7. General Quantifiers
<br /> 8.7. General Quantifiers<br />A quantifier in a pattern means to repeat the preceding item a certain number of times. You've seen three quantifiers: *, +, and ?. But if none of those three suits your needs, use a comma-separated pair of numbers insi
2010-07-21 17:57:00
354
原创 Learning Perl: 8.8. Precedence
<br /> 8.8. Precedence<br />With all of these metacharacters in regular expressions, you may feel you can't keep track of the players without a scorecard. That's the precedence chart, which shows us which parts of the pattern stick together the most tightl
2010-07-21 17:57:00
443
原创 Learning Perl: 8.5. Interpolating into Patterns
<br /> 8.5. Interpolating into Patterns<br />The regular expression is double-quote interpolated as if it were a double-quoted string. This allows us to write a quick grep-like program like this: #!/usr/bin/perl -w my $what = "larry"; while
2010-07-21 17:56:00
360
原创 Learning Perl: 8.6. The Match Variables
<br /> 8.6. The Match Variables<br />So far, when we've put parentheses into patterns, they've been used only for their ability to group parts of a pattern together. But parentheses also trigger the regular expression engine's memory. The memory holds the
2010-07-21 17:56:00
530
原创 Perl Learning: 8.3. Anchors
<br /> 8.3. Anchors<br />By default, if a pattern doesn't match at the start of the string, it can "float" on down the string trying to match somewhere else. But a number of anchors may be used to hold the pattern at a particular point in a string.<br />Th
2010-07-21 17:55:00
367
原创 Learning Perl: 8.4. The Binding Operator, =~
<br /> 8.4. The Binding Operator, =~<br />Matching against $_ is merely the default; the binding operator (=~) tells Perl to match the pattern on the right against the string on the left, instead of matching against $_.[] For example:<br />[] The binding o
2010-07-21 17:55:00
515
原创 Learning Perl: 8.1. Matches with m//
<br /> 8.1. Matches with m//<br />We've been writing patterns in pairs of forward slashes, like /fred/. This is a shortcut for the m// (pattern match) operator. As you saw with the qw// operator, you may choose any pair of delimiters to quote the contents.
2010-07-21 17:54:00
351
原创 Perl Learning: 8.2. Option Modifiers
<br /> 8.2. Option Modifiers<br />Several option modifier letters, sometimes called flags, may be appended as a group right after the ending delimiter of a regular expression to change its behavior from the default.8.2.1. Case-Insensitive Matching with /i<
2010-07-21 17:54:00
457
原创 Learning Perl: 7.2. Using Simple Patterns
<br /> 7.2. Using Simple Patterns<br />To match a pattern (regular expression) against the contents of $_, put the pattern between a pair of forward slashes (/) as we do here: $_ = "yabba dabba doo"; if (/abba/) { print "It matched!/n";
2010-07-21 17:53:00
598
原创 Perl Learning: 7.3. Character Classes
<br /> 7.3. Character Classes<br />A character class, a list of possible characters inside square brackets ([ ]), matches any single character from within the class. It matches one character, but that character may be any of the ones listed.<br />For examp
2010-07-21 17:53:00
481
原创 Learning Perl: 7.1. What Are Regular Expressions?
<br /> 7.1. What Are Regular Expressions?<br />A regular expression, often called a pattern in Perl, is a template that matches or doesn't match a given string.[] An infinite number of possible text strings exist, and a given pattern divides that infinite
2010-07-21 17:52:00
420
原创 Learning Perl: 6.3. Hash Functions
<br /> 6.3. Hash Functions<br />Some useful functions can work on an entire hash simultaneously.6.3.1. The keys and values Functions<br />The keys function yields a list of all the keys in a hash, and the values function gives the corresponding values. If
2010-07-21 17:51:00
418
原创 Learning Perl: 6.4. Typical Use of a Hash
<br /> 6.4. Typical Use of a Hash<br />At this point, a concrete example might help.<br />The Bedrock library uses a Perl program in which a hash tracks how many books each person has checked out: $books{"fred"} = 3; $books{"wilma"} = 1;<br /><br /
2010-07-21 17:51:00
399
原创 Learning Perl:
<br /> 6.2. Hash Element Access<br />To access an element of a hash, use syntax that looks like this: $hash{$some_key}<br /><br />This is similar to what we used for array access, but here we use curly braces instead of square brackets around the subscr
2010-07-21 17:50:00
466
原创 Learning Perl: 6.1. What Is a Hash?
<br /> 6.1. What Is a Hash?<br />A hash is a data structure like an array, in that it can hold any number of values and retrieve these values at will. However, instead of indexing the values by number, as we did with arrays, we'll look up the values by nam
2010-07-21 17:49:00
438
原创 Perl Learning: 5.10. Reopening a Standard Filehandle
<br /> 5.10. Reopening a Standard Filehandle<br />We mentioned earlier that if you were to reopen a filehandle (that is, if you were to open a filehandle FRED when you've got an open filehandle named FRED), the old one would be closed for you automatically
2010-07-21 17:47:00
422
原创 Learning Perl: 5.9. Using Filehandles
<br /> 5.9. Using Filehandles<br />Once a filehandle is open for reading, you can read lines from it the same way you can read from standard input with STDIN. So, for example, to read lines from the Unix password file: if ( ! open PASSWD, "/etc/passwd")
2010-07-21 17:46:00
396
原创 Learning Perl: 5.8. Fatal Errors with die
<br /> 5.8. Fatal Errors with die<br />Let's step aside for a moment. We need some stuff that isn't directly related to (or limited to) I/O but is more about getting out of a program earlier than normal.<br />When a fatal error happens inside Perl (for exa
2010-07-21 17:45:00
391
原创 Learning Perl: 5.6. Filehandles
<br /> 5.6. Filehandles<br />A filehandle is the name in a Perl program for an I/O connection between your Perl process and the outside world. That is, it's the name of a connection and not necessarily the name of a file.<br />Filehandles are named like ot
2010-07-21 17:44:00
401
原创 Learning Perl: 5.7. Opening a Filehandle
5.7. Opening a FilehandleYou've seen that Perl provides three filehandlesSTDIN, STDOUT, and STDERRwhich are automatically open to files or devices established by the program's parent process (probably the shell). When you need other filehandles,
2010-07-21 17:43:00
419
原创 Learning Perl: 5.4. Output to Standard Output
<br /> 5.4. Output to Standard Output<br />The print operator takes a list of values and sends each item (as a string, of course) to standard output in turn, one after another. It doesn't add any extra characters before, after, or in between the items.[*]
2010-07-21 17:38:00
346
原创 Learning Perl: 5.5. Formatted Output with printf
<br /> 5.5. Formatted Output with printf<br />You may wish to have a little more control with your output than print provides. In fact, you may be accustomed to the formatted output of C's printf function. Fear notPerl provides a comparable operation with
2010-07-21 17:38:00
450
原创 Learning Perl: 5.2. Input from the Diamond Operator
<br /> 5.2. Input from the Diamond Operator<br />Another way to read input is with the diamond[*] operator: <>. This is useful for making programs that work like standard Unix[] utilities, with respect to the invocation arguments (which we'll see in a mome
2010-07-21 17:37:00
367
原创 Learning Perl: 5.3. The Invocation Arguments
<br /> 5.3. The Invocation Arguments<br />Technically, the diamond operator isn't looking at the invocation argumentsit works from the @ARGV array. This array is a special array preset by the Perl interpreter as the list of the invocation arguments. In oth
2010-07-21 17:37:00
364
原创 Learning Perl: 5.1. Input from Standard Input
<br /> 5.1. Input from Standard Input<br />Reading from the standard input stream is easy. We've been doing it with the <STDIN> operator.[*] Evaluating this operator in a scalar context gives you the next line of input:<br />[*] What we're calling the line
2010-07-21 17:36:00
370
原创 Learning Perl: 4.10. Non-Scalar Return Values
<br /> 4.10. Non-Scalar Return Values<br />A scalar isn't the only kind of return value a subroutine may have. If you call your subroutine in a list context,[] it can return a list of values.<br />[] You can detect if a subroutine is being evaluated in a s
2010-07-21 17:35:00
353
原创 Learning Perl: 4.9. The return Operator
<br /> 4.9. The return Operator<br />The return operator immediately returns a value from a subroutine: my @names = qw/ fred barney betty dino wilma pebbles bamm-bamm /; my $result = &which_element_is("dino", @names); sub which_element_is {
2010-07-21 17:34:00
303
原创 Learning Perl: 4.8. The use strict Pragma
<br /> 4.8. The use strict Pragma<br />Perl tends to be a permissive language.[] But maybe you want Perl to impose a little discipline; that can be arranged with the use strict pragma.<br />[] Bet you hadn't noticed.<br />A pragma is a hint to a compiler,
2010-07-21 17:33:00
451
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人