======================================================================
 Modern::Open à¤à¤¿à¤ à¤¸à¤¿à¤                   [NE] à¤¨à¥à¤ªà¤¾à¤²à¥
======================================================================

[ 1. Installation / [NE] à¤¨à¥à¤ªà¤¾à¤²à¥ ]

  à¤¸à¥à¤¥à¤¾à¤ªà¤¨à¤¾ à¤à¤°à¥à¤¨à¥à¤¹à¥à¤¸à¥:
    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 context: à¤à¤¸à¤«à¤² à¤­à¤à¤®à¤¾ 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

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