======================================================================
 Modern::Open Ð¥ÑÑÑÑÐ°Ð³                        [MN] ÐÐ¾Ð½Ð³Ð¾Ð»
======================================================================

[ 1. Installation / [MN] ÐÐ¾Ð½Ð³Ð¾Ð» ]

  Ð¡ÑÑÐ»Ð³Ð°Ñ:
    cpan Modern::Open

  Ð¡ÐºÑÐ¸Ð¿ÑÑÐ´ Ð°ÑÐ¸Ð³Ð»Ð°Ñ:
    use Modern::Open;

  Ò®Ð¹Ð»ÑÐ»ÑÐ»: open(), opendir(), sysopen(), pipe(), socket(),
            accept() ÑÑÐ½ÐºÑÑÑÐ´ÑÐ³ autovivification + autodie ÑÑÐ²Ð¸Ð»Ð±Ð°ÑÐ°Ð°Ñ ÑÐ¾Ð»Ð¸Ð½Ð¾.
            Perl 5.005_03 Ð±Ð¾Ð»Ð¾Ð½ ÑÒ¯Ò¯Ð½ÑÑÑ ÑÐ¾Ð¹ÑÑ Ð±Ò¯Ñ ÑÑÐ²Ð¸Ð»Ð±Ð°ÑÑ Ð°Ð¶Ð¸Ð»Ð»Ð°Ð´Ð°Ð³.
            Тэмдэглэл: socket() нь autovivification-г дэмждэг ч autodie хийдэггүй.

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

  my $fh;
  open($fh, "< file.txt");   # ÑÐ½ÑÐ¸Ñ
  open($fh, "> file.txt");   # Ð±Ð¸ÑÐ¸Ñ (Ð´Ð°ÑÐ¶ Ð±Ð¸ÑÐ¸Ñ)
  open($fh, ">> file.txt");  # Ð½ÑÐ¼ÑÑ
  open($fh, "+< file.txt");  # ÑÐ½ÑÐ¸Ñ/Ð±Ð¸ÑÐ¸Ñ
  open($fh, "cmd |");        # ÐºÐ¾Ð¼Ð°Ð½Ð´ÑÐ½ ÑÐ¾Ð¾Ð»Ð¾Ð¹Ð³Ð¾Ð¾Ñ ÑÐ½ÑÐ¸Ñ
  open($fh, "| cmd");        # ÐºÐ¾Ð¼Ð°Ð½Ð´ÑÐ½ ÑÐ¾Ð¾Ð»Ð¾Ð¹Ð´ Ð±Ð¸ÑÐ¸Ñ

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

[ 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() ]

  my $dh;
  opendir($dh, "/path/to/dir");
  while (my $e = readdir($dh)) {
      next if $e eq '.' or $e eq '..';
      print "$e\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 ÐºÐ¾Ð½ÑÐµÐºÑÑ: Ð°Ð¼Ð¶Ð¸Ð»ÑÐ³Ò¯Ð¹ Ð±Ð¾Ð»Ð²Ð¾Ð» die
  open($fh, "< no_such_file.txt");   # dies: Can't open(...)

  # ÐÑÑÐ°Ñ ÑÑÐ³Ð°ÑÐ°Ð¹: Ð°Ð¼Ð¶Ð¸Ð»ÑÐ³Ò¯Ð¹ Ð±Ð¾Ð»Ð²Ð¾Ð» undef/0
  my $rc = open($fh, "< file.txt");
  unless ($rc) { warn "open Ð°Ð¼Ð¶Ð¸Ð»ÑÐ³Ò¯Ð¹: $!" }

  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. Compatibility ]

  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

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