======================================================================
 Modern::Open ÚÛÙ¹ Ø´ÛÙ¹                         [UR] Ø§Ø±Ø¯Ù
======================================================================

[ 1. Installation / [UR] Ø§Ø±Ø¯Ù ]

  Ø§ÙØ³Ù¹Ø§Ù Ú©Ø±ÛÚº:
    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

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