Chinaunix首页 | 论坛 | 博客
  • 博客访问: 855768
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-21 10:06:04

  1. my @skipper_with_name;
  2. {
  3.   my @skipper = qw(blue_shirt hat jacket preserver sunscreen);
  4.   @skipper_with_name = ('The Skipper', \@skipper);
  5. }

We can create such a value directly using the anonymous array constructor, which is yet another use for square brackets:

  1. my $ref_to_skipper_provisions =
  2.   [ qw(blue_shirt hat jacket preserver sunscreen) ];
The square brackets take the value within (evaluated in a list context); establish a new, anonymous array initialized to those values; and (here's the important part) return a reference to that array.
  1. my @skipper_with_name = (
  2.   'The Skipper',
  3.   [ qw(blue_shirt hat jacket preserver sunscreen) ]
  4. );
 if we have this syntax:
  1. my $fruits;
  2. {
  3.   my @secret_variable = ('pineapple', 'papaya', 'mango');
  4.   $fruits = \@secret_variable;
  5. }

we can replace it with:

  1. my $fruits = ['pineapple', 'papaya', 'mango'];

  1. my %gilligan_info = (
  2.   name => 'Gilligan',
  3.   hat => 'White',
  4.   shirt => 'Red',
  5.   position => 'First Mate',
  6. );

  7. my %skipper_info = (
  8.   name => 'Skipper',
  9.   hat => 'Black',
  10.   shirt => 'Blue',
  11.   position => 'Captain',
  12. );

  13. my @crew = (\%gilligan_info, \%skipper_info);

  1. my @crew = (
  2.   {
  3.     name => 'Gilligan',
  4.     hat => 'White',
  5.     shirt => 'Red',
  6.     position => 'First Mate',
  7.   },

  8.   {
  9.     name => 'Skipper',
  10.     hat => 'Black',
  11.     shirt => 'Blue',
  12.     position => 'Captain',
  13.   },
  14. );



阅读(454) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~