======================================================================
 Perl7::Handy éæ¥è¡¨                             [TW] ä¸­æï¼ç¹é«ï¼
======================================================================

[ 1. Installation ]

  å®è£:
    cpan Perl7::Handy

  å¨è³æ¬ä¸­ä½¿ç¨:
    use Perl7::Handy;

  ææ (Perl 5.010001 åæ´æ°çæ¬):
    * use strict               -- å¼·å¶è®æ¸å®£å
    * use warnings             -- åç¨è­¦å
    * no bareword::filehandles -- ç¦æ­¢ open(FILE,...) å½¢å¼
    * no multidimensional      -- åç¨ $hash{a,b} æ¨¡æ¬
    * use feature qw(signatures)  (Perl 5.020+)
    * no feature qw(indirect)     (Perl 5.031009+)

  ææ (Perl 5.005_03 -- 5.008):
    * use strict
    * open/opendir/sysopen/pipe/socket/accept èªåè®æ¸å»ºç«
    # socket() 失敗時不會 autodie（在 void context 中僅回傳假值）
    * åºæ¼ tie ç $; é·é±ï¼ç¦æ­¢å¤ç¶­é£åæ¨¡æ¬ï¼

[ 2. open() -- autovivification ]

  my $fh;
  open($fh, "< file.txt");   # è®å
  open($fh, "> file.txt");   # å¯«å¥ï¼æ¸ç©ºï¼
  open($fh, ">> file.txt");  # éå 
  open($fh, "+< file.txt");  # è®å¯«
  while (my $line = readline($fh)) { ... }
  print $fh "text\n";
  close($fh);

  # è£¸å­å¥æè¢«æçµ:
  open(FILE, "< file.txt");   # dies: Use of bareword handle in open

[ 3. open() -- 3-arg ]

  my $fh;
  open($fh, '<',  "file.txt");   # è®å
  open($fh, '>',  "file.txt");   # å¯«å¥ï¼æ¸ç©ºï¼
  open($fh, '>>', "file.txt");   # éå 
  open($fh, '+<', "file.txt");   # è®å¯«
  open($fh, '+>', "file.txt");   # è®å¯«ï¼æ¸ç©ºï¼
  open($fh, '-|', "cmd");        # å¾å½ä»¤ç®¡éè®å
  open($fh, '|-', "cmd");        # å¯«å¥å½ä»¤ç®¡é

[ 4. opendir() / sysopen() ]

  my $dh;
  opendir($dh, "/path/to/dir");
  while (my $e = readdir($dh)) {
      next if $e eq '.' or $e eq '..';
      print "$e\n";
  }
  closedir($dh);

  use Fcntl qw(O_RDONLY O_WRONLY O_CREAT O_TRUNC);
  my $fh;
  sysopen($fh, "file.txt", O_RDONLY);
  sysopen($fh, "file.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);

[ 5. pipe() ]

  my($reader, $writer);
  pipe($reader, $writer);
  if (my $pid = fork()) {
      close($writer);
      while (my $line = readline($reader)) { print $line }
      close($reader);
  } else {
      close($reader);
      print $writer "ä¾èªå­è¡ç¨çè¨æ¯\n";
      close($writer);
      exit 0;
  }

[ 6. socket() / accept() ]

  use Socket qw(AF_INET SOCK_STREAM sockaddr_in inet_aton);
  my $server;
  socket($server, AF_INET, SOCK_STREAM, 0);
  # 注意：socket() 失敗時不會 autodie，需明確檢查回傳值。
  my $client;
  accept($client, $server);

[ 7. no bareword::filehandles ]

  # è£¸å­å¥æè¢«æçµ:
  open(FILE, "< file.txt");    # error: Bareword filehandle
  print FILE "hello\n";        # error: Bareword filehandle

  # ä½¿ç¨è©å½è®æ¸:
  my $fh;
  open($fh, "< file.txt");
  print $fh "hello\n";
  close($fh);

[ 8. no multidimensional ]

  # å¤ç¶­éæ¹æ¨¡æ¬è¢«åç¨:
  my %h;
  $h{1,2} = "val";    # error: Use of multidimensional array emulation

  # ä½¿ç¨æç¢ºçéµ:
  $h{"$x,$y"} = "val";

[ 9. Signatures (Perl 5.020+) ]

  # èªååç¨ç°½ç« ; æå¶å¯¦é©æ§è­¦å
  use Perl7::Handy;

  sub greet($name) { return "Hello, $name!" }
  sub add($x, $y)  { return $x + $y }

[ 10. no indirect (Perl 5.031009+) ]

  # éæ¥ç©ä»¶èªæ³è¢«åç¨:
  my $obj = new MyClass;   # error: indirect syntax

  # ä½¿ç¨ç´æ¥èªæ³:
  my $obj = MyClass->new;

[ 11. CVE-2016-1238 ]

  # Perl7::Handy å¾ @INC ä¸­ç§»é¤ç®åç®éï¼é²æ­¢ç¸å°è·¯å¾æ³¨å¥ã
  # BEGIN { pop @INC if $INC[-1] eq '.' }

[ 12. ç¸å®¹æ§ç©é£ ]

  Feature              5.005_03  5.006-009  5.010+  5.020+  5.031009+
  -------------------  --------  ---------  ------  ------  ---------
  strict               yes       yes        yes     yes     yes
  warnings             no        yes        yes     yes     yes
  autovivif. open()    yes       yes        yes*    yes*    yes*
  no bareword FH       emulated  emulated   yes     yes     yes
  no multidim.         tie       tie        yes     yes     yes
  signatures           no        no         no      yes     yes
  no indirect          no        no         no      no      yes

[ 13. Official resources ]

  Perl7::Handy (MetaCPAN):
    https://metacpan.org/dist/Perl7-Handy

  bareword::filehandles:
    https://metacpan.org/dist/bareword-filehandles

  INABA Hitoshi (ina) on CPAN:
    https://metacpan.org/author/INA

======================================================================
