======================================================================
 Modern::Open à¤à¥à¤ à¤¶à¥à¤                   [HI] à¤¹à¤¿à¤¨à¥à¤¦à¥
======================================================================

[ 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 |");        # à¤à¤®à¤¾à¤à¤¡ à¤ªà¤¾à¤à¤ª à¤¸à¥ à¤ªà¤¢à¤¼à¥à¤
  open($fh, "| cmd");        # à¤à¤®à¤¾à¤à¤¡ à¤ªà¤¾à¤à¤ª à¤®à¥à¤ à¤²à¤¿à¤à¥à¤

  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");        # à¤à¤®à¤¾à¤à¤¡ à¤ªà¤¾à¤à¤ª à¤¸à¥ à¤ªà¤¢à¤¼à¥à¤
  open($fh, '|-', "cmd");        # à¤à¤®à¤¾à¤à¤¡ à¤ªà¤¾à¤à¤ª à¤®à¥à¤ à¤²à¤¿à¤à¥à¤

[ 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 à¤¸à¤à¤¦à¤°à¥à¤­: à¤µà¤¿à¤«à¤²à¤¤à¤¾ à¤ªà¤° 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. à¤µà¤¾à¤ªà¤¸ à¤à¤¿à¤¯à¤¾ à¤à¤¯à¤¾ à¤¹à¥à¤à¤¡à¤² ]

  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

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