Perl概述

@home = ("couch", "chair", "table", "stove");  数组

数组.一个数组是多个标量的有序列表

因为数组是有序的,所以你可以在它上面做很多很有用操作.例如堆栈操作 push 和 pop

散列,散列是一组无序标量

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

$longday{"Wed"}


$wife{"Jacob"} = ["Leah", "Rachel", "Bilhah", "Zilpah"]; # 正确
这个语句创建了一个未命名的数组,并将这个数组的引用放入散列的元素 $wife{“Jacob”} 中




1.2.4 复杂数据结构

[root@wx03 1]# cat a2.pl 
$wife{"Jacob"} = ["Leah", "Rachel", "Bilhah", "Zilpah"];
print $wife{"Jacob"}->[1];
print $wife{"Jacob"}[1];
print "\n";
use Data::Dumper;
$str=Dumper($wife{"Jacob"});
print "\$str is $str\n";
[root@wx03 1]# perl a2.pl 
RachelRachel
$str is $VAR1 = [
          'Leah',
          'Rachel',
          'Bilhah',
          'Zilpah'
        ];



[root@wx03 1]# cat a4.pl 



[root@wx03 1]# cat t2.pl 
use  LWP::UserAgent;
 
my $ua = LWP::UserAgent->new;
$ua->timeout(99);
$ua->env_proxy;
$ua->agent("Mozilla/147258369.0");

my $response = $ua->get('http://120.55.118.6:3000/api/auto_publish/publish?env=uat&app=zjzc-hy-core-02&ip=121.40.x91.x1');
use Data::Dumper;
$str=Dumper($response);
print $str;
print "\n";

[root@wx03 1]# perl t2.pl 
$VAR1 = bless( {
                 '_request' => bless( {
                                        '_headers' => bless( {
                                                               'user-agent' => 'Mozilla/147258369.0'
                                                             }, 'HTTP::Headers' ),
                                        '_method' => 'GET',
                                        '_content' => '',
                                        '_uri' => bless( do{\(my $o = 'http://120.55.118.6:3000/api/auto_publish/publish?env=uat&app=zjzc-hy-core-02&ip=121.40.1x1.xx')}, 'URI::http' )
                                      }, 'HTTP::Request' ),
                 '_content' => 'Can\'t connect to 120.55.1x8.x:3000

Connection refused at /usr/local/perl/lib/site_perl/5.22.1/LWP/Protocol/http.pm line 47.
',
                 '_headers' => bless( {
                                        '::std_case' => {
                                                          'client-warning' => 'Client-Warning',
                                                          'client-date' => 'Client-Date'
                                                        },
                                        'client-warning' => 'Internal response',
                                        'client-date' => 'Sun, 15 May 2016 14:15:42 GMT',
                                        'content-type' => 'text/plain'
                                      }, 'HTTP::Headers' ),
                 '_rc' => 500,
                 '_msg' => 'Can\'t connect to 120.55.118.6:3000'
               }, 'HTTP::Response' );



$kids_of_wife{"Jacob"} = {
"Leah" => ["Reuben", "Simeon", "Levi", "Judah", "Issachar", "Zebulun"],
"Rachel" => ["Joseph", "Benjamin"],
"Bilhah" => ["Dan", "Naphtali"],
"Zilpah" => ["Gad", "Asher"],};
print $kids_of_wife{"Jacob"}->{Rachel}->[1];

[root@wx03 1]# perl a4.pl 
Benjamin[root@wx03 1]# 


1.6.3.4 跳出控制结构: next 和 last

[root@wx03 1]# cat t3.pl 
@users=qw/a b c d e f g/;
foreach $user (@users){
   if ($user eq c) {next;}
   else {print "\$user is $user\n";};

   if ($user eq d){last;};
};
   
[root@wx03 1]# perl t3.pl 
$user is a
$user is b
$user is d

next 操作符运行你跳至本地循环的结束,开始下一个循环

last 操作符允许你跳至整个循环的结束



1.2.5 简单数据结构



$wife{"Jacob"} Jacob键对应的值是一个数组引用



[root@wx03 1]# cat a3.pl 
$ua=["Leah", "Rachel", "Bilhah", "Zilpah"];
print $ua->[0];
print "\n";

[root@wx03 1]# perl a3.pl 
Leah

posted @ 2016-05-15 22:43  czcb  阅读(140)  评论(0编辑  收藏  举报