Programming Perl 1

一,
TypeCharacterExampleIs a name for:
Scalar$$centsAn individual value (number or string)
Array@@largeA list of values, keyed by number
Hash%%interestA group of values, keyed by string
Subroutine&&howA callable chunk of Perl code
Typeglob**struckEverything named struck

eg:

1 .$camels = '123';
print $camels + 1, "/n";

2.@home = ("couch", "chair", "table", "stove");

($potato, $lift, $tennis, $pipe) = @home;

If you want to assign to one array element at a time, you could write the earlier assignment as:

$home[0] = "couch"; $home[1] = "chair"; $home[2] = "table"; $home[3] = "stove";

3.These are called list assignments. They logically happen in parallel, so you can swap two variables by saying:
($alpha,$omega) = ($omega,$alpha);

4.%longday = ("Sun", "Sunday", "Mon", "Monday", "Tue", "Tuesday",
            "Wed", "Wednesday", "Thu", "Thursday", "Fri",
"Friday", "Sat", "Saturday");

Because it is sometimes difficult to read a hash that is defined like this, Perl provides the => (equal sign, greater than) sequence as an alternative separator to the comma. Using this syntax (and some creative formatting), it is easier to see which strings are the keys, and which strings are the associated values.

%longday = (
"Sun" => "Sunday",
"Mon" => "Monday",
"Tue" => "Tuesday",
"Wed" => "Wednesday",
"Thu" => "Thursday",
"Fri" => "Friday",
"Sat" => "Saturday",
);

5.Linguistically, the relationship encoded in a hash is genitive or possessive, like the word "of" in English, or like "'s". The wife of Adam is Eve, so we write:
$wife{"Adam"} = "Eve";

二,

Since you can use the open function to create filehandles for various purposes (input, output, piping), you need to be able to specify which behavior you want. As you would do on the UNIX command line, you simply add characters to the filename.

open(SESAME, "filename");               # read from existing file
open(SESAME, "<filename"); # (same thing, explicitly)
open(SESAME, ">filename"); # create file and write to it
open(SESAME, ">>filename"); # append to existing file
open(SESAME, "| output-pipe-command"); # set up an output filter
open(SESAME, "input-pipe-command |"); # set up an input filter

The empty angle operator, <>, will read lines from all the files specified on the command line, or STDIN, if none were specified. (This is standard behavior for many UNIX filter programs.)
print STDOUT "Enter a number: "; # ask for a number

$number = <STDIN>; # input the number

print STDOUT "The number is $number/n"; # print the number

三. operators
Some Binary Arithmetic Operators
ExampleNameResult
$a + $bAdditionSum of $a and $b
$a * $bMultiplicationProduct of $a and $b
$a % $bModulusRemainder of $a divided by $b
$a ** $bExponentiation$a to the power of $b

String Operators

There is also an "addition" operator for strings that does concatenation. Unlike some languages that confuse this with numeric addition, Perl defines a separate operator (.) for string concatenation:

$a = 123;
$b = 456;
print $a + $b; # prints 579
print $a . $b; # prints 123456

There's also a "multiply" operation for strings, also called the repeat operator. Again, it's a separate operator (x) to keep it distinct from numeric multiplication:

$a = 123;
$b = 3;
print $a * $b; # prints 369
print $a x $b; # prints 123123123

print $a . ' is equal to ' . $b . "/n"; # dot operator
print $a, ' is equal to ', $b, "/n"; # list
print "$a is equal to $b/n"; # interpolation

 Logical Operators
ExampleNameResult
$a && $bAnd

$a if $a is false, $b otherwise

$a || $bOr

$a if $a is true, $b otherwise

! $aNotTrue if $a is not true
$a and $bAnd

$a if $a is false, $b otherwise

$a or $bOr

$a if $a is true, $b otherwise

not $aNotTrue if $a is not true

Some Numeric and String Comparison Operators
ComparisonNumericStringReturn Value
Equal==eqTrue if $a is equal to $b
Not equal!=neTrue if $a is not equal to $b
Less than<ltTrue if $a is less than $b
Greater than>gtTrue if $a is greater than $b
Less than or equal<=leTrue if $a not greater than $b
Comparison<=>cmp0 if equal, 1 if $a greater, -1 if $b greater
Some File Test Operators
ExampleNameResult
-e $aExistsTrue if file named in $a exists
-r $aReadableTrue if file named in $a is readable
-w $aWritableTrue if file named in $a is writable
-d $aDirectoryTrue if file named in $a is a directory
-f $aFileTrue if file named in $a is a regular file
-T $aText FileTrue if file named in $a is a text file
if [! -s $filename] : filename exists and is empty

四, Breaking out: next and last

The next and last operators allow you to modify the flow of your loop. It is not at all uncommon to have a special case; you may want to skip it, or you may want to quit when you encounter it. For example, if you are dealing with UNIX accounts, you may want to skip the system accounts (like root or lp). The next operator would allow you to skip to the end of your current loop iteration, and start the next iteration. The last operator would allow you to skip to the end of your block, as if your test condition had returned false. This might be useful if, for example, you are looking for a specific account and want to quit as soon as you find it.

foreach $user (@users) {
if ($user eq "root" or $user eq "lp") {
next;
}
if ($user eq "special") {
print "Found the special account./n";
# do some processing
last;
}
}

It's possible to break out of multi-level loops by labeling your loops and specifying which loop you want to break out of. Together with statement modifiers (another form of conditional we haven't talked about), this can make for very readable loop exits, if you happen to think English is readable:

LINE: while ($line = <ARTICLE>) {
last LINE if $line eq "/n"; # stop on first blank line
next LINE if $line =~ /^#/; # skip comment lines
# your ad here
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值