======================================================================
 Modern::Open à¸à¸µà¸à¸ªà¸£à¸¸à¸                 [TH] à¸ à¸²à¸©à¸²à¹à¸à¸¢
======================================================================

[ 1. à¸à¸²à¸£à¸à¸´à¸à¸à¸±à¹à¸à¹à¸¥à¸°à¸à¸²à¸£à¹à¸à¹à¸à¸²à¸ ]

  à¸à¸´à¸à¸à¸±à¹à¸:
    cpan Modern::Open

  à¹à¸à¹à¹à¸à¸ªà¸à¸£à¸´à¸à¸à¹:
    use Modern::Open;

  à¸à¸¥: à¹à¸à¸à¸à¸µà¹ open(), opendir(), sysopen(), pipe(), socket(),
       à¹à¸¥à¸° accept() à¸à¹à¸§à¸¢à¹à¸§à¸­à¸£à¹à¸à¸±à¸ autovivification + autodie
       à¸à¸³à¸à¸²à¸à¸à¸ Perl 5.005_03 à¹à¸¥à¸°à¹à¸§à¸­à¸£à¹à¸à¸±à¸à¹à¸«à¸¡à¹à¸à¸§à¹à¸²à¸à¸±à¹à¸à¸«à¸¡à¸
       หมายเหตุ: socket() รองรับ autovivification แต่ไม่รองรับ autodie

[ 2. open() -- à¹à¸à¸ 2 à¸­à¸²à¸£à¹à¸à¸´à¸§à¹à¸¡à¸à¸à¹ ]

  my $fh;
  open($fh, "< file.txt");   # à¸­à¹à¸²à¸
  open($fh, "> file.txt");   # à¹à¸à¸µà¸¢à¸ (à¹à¸à¸µà¸¢à¸à¸à¸±à¸)
  open($fh, ">> file.txt");  # à¸à¹à¸­à¸à¹à¸²à¸¢
  open($fh, "+< file.txt");  # à¸­à¹à¸²à¸/à¹à¸à¸µà¸¢à¸
  open($fh, "cmd |");        # à¸­à¹à¸²à¸à¸à¸²à¸ pipe à¸à¸³à¸ªà¸±à¹à¸
  open($fh, "| cmd");        # à¹à¸à¸µà¸¢à¸à¹à¸à¸¢à¸±à¸ pipe à¸à¸³à¸ªà¸±à¹à¸

  while (my $line = readline($fh)) { ... }
  print $fh "text\n";
  close($fh);

[ 3. open() -- à¹à¸à¸ 3 à¸­à¸²à¸£à¹à¸à¸´à¸§à¹à¸¡à¸à¸à¹ ]

  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");        # à¸­à¹à¸²à¸à¸à¸²à¸ pipe à¸à¸³à¸ªà¸±à¹à¸
  open($fh, '|-', "cmd");        # à¹à¸à¸µà¸¢à¸à¹à¸à¸¢à¸±à¸ pipe à¸à¸³à¸ªà¸±à¹à¸

[ 4. opendir() ]

  my $dh;
  opendir($dh, "/path/to/dir");

  while (my $entry = readdir($dh)) {
      next if $entry eq '.' or $entry eq '..';
      print "$entry\n";
  }
  closedir($dh);

[ 5. sysopen() ]

  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);
  sysopen($fh, "file.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);

[ 6. 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;
  }

[ 7. 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);

[ 8. à¸à¸¤à¸à¸´à¸à¸£à¸£à¸¡ autodie ]

  # void context: die à¹à¸¡à¸·à¹à¸­à¸¥à¹à¸¡à¹à¸«à¸¥à¸§
  open($fh, "< no_such_file.txt");   # dies: Can't open(...)

  # à¸£à¸±à¸à¸à¹à¸²à¸à¸·à¸: à¸à¸·à¸ undef/0 à¹à¸¡à¸·à¹à¸­à¸¥à¹à¸¡à¹à¸«à¸¥à¸§
  my $rc = open($fh, "< file.txt");
  unless ($rc) { warn "open à¸¥à¹à¸¡à¹à¸«à¸¥à¸§: $!" }

  # Bareword:
  open(FILE, "< file.txt");   # dies: Bare handle no longer supported

[ 9. handle à¸à¸µà¹à¸ªà¹à¸à¸à¸·à¸ ]

  readline($fh)        # à¸­à¹à¸²à¸à¸«à¸à¸¶à¹à¸à¸à¸£à¸£à¸à¸±à¸
  read($fh, $buf, $n)  # à¸­à¹à¸²à¸ N à¹à¸à¸à¹
  print $fh "..."      # à¹à¸à¸µà¸¢à¸
  binmode($fh)         # à¹à¸«à¸¡à¸à¹à¸à¸à¸²à¸£à¸µ
  seek($fh, 0, 0)      # à¹à¸à¸à¸µà¹à¸à¸¸à¸à¹à¸£à¸´à¹à¸¡à¸à¹à¸
  tell($fh)            # à¸à¸³à¹à¸«à¸à¹à¸à¸à¸±à¸à¸à¸¸à¸à¸±à¸
  close($fh)           # à¸à¸´à¸
  eof($fh)             # à¸à¸£à¸§à¸à¸ªà¸­à¸à¸à¸¸à¸à¸ªà¸´à¹à¸à¸ªà¸¸à¸à¹à¸à¸¥à¹

[ 10. à¸à¸§à¸²à¸¡à¹à¸à¹à¸²à¸à¸±à¸à¹à¸à¹ ]

  à¹à¸§à¸­à¸£à¹à¸à¸±à¸ Perl : 5.005_03 à¸à¸¶à¹à¸à¹à¸ (à¸£à¸§à¸¡à¸à¸¶à¸ 5.42)
  à¹à¸à¸¥à¸à¸à¸­à¸£à¹à¸¡    : Unix, Linux, macOS, Windows (à¸à¸±à¸à¸à¸²à¸£ CRLF à¸­à¸±à¸à¹à¸à¸¡à¸±à¸à¸´)
  à¸à¸¶à¹à¸à¸à¸±à¸      : Fcntl (à¹à¸¡à¸à¸¹à¸¥à¸«à¸¥à¸±à¸)

[ 11. Official resources ]

  Modern::Open (MetaCPAN):
    https://metacpan.org/dist/Modern-Open

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

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