======================================================================
 Lembar Contekan Modern::Open                       [ID] Bahasa Indonesia
======================================================================

[ 1. Instalasi dan penggunaan ]

  Instalasi:
    cpan Modern::Open

  Gunakan dalam skrip Anda:
    use Modern::Open;

  Efek: menggantikan open(), opendir(), sysopen(), pipe(), socket(),
        dan accept() dengan versi autovivification + autodie.
        Bekerja di Perl 5.005_03 dan semua versi berikutnya.
        Catatan: socket() mendukung autovivifikasi tetapi tidak autodie.

[ 2. open() -- bentuk 2 argumen ]

  my $fh;
  open($fh, "< file.txt");   # baca
  open($fh, "> file.txt");   # tulis (timpa)
  open($fh, ">> file.txt");  # tambahkan
  open($fh, "+< file.txt");  # baca/tulis
  open($fh, "cmd |");        # baca dari pipa perintah
  open($fh, "| cmd");        # tulis ke pipa perintah

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

[ 3. open() -- bentuk 3 argumen ]

  my $fh;
  open($fh, '<',  "file.txt");   # baca
  open($fh, '>',  "file.txt");   # tulis (timpa)
  open($fh, '>>', "file.txt");   # tambahkan
  open($fh, '+<', "file.txt");   # baca/tulis
  open($fh, '+>', "file.txt");   # baca/tulis (timpa)
  open($fh, '-|', "cmd");        # baca dari pipa perintah
  open($fh, '|-', "cmd");        # tulis ke pipa perintah

[ 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 "Halo dari proses anak\n";
      close($writer);
      exit 0;
  }

[ 7. socket() dan accept() ]

  use Socket qw(AF_INET SOCK_STREAM sockaddr_in inet_aton);

  my $server;
  socket($server, AF_INET, SOCK_STREAM, 0);
  # Catatan: socket() tidak autodie; periksa nilai kembaliannya secara manual.

  my $client;
  accept($client, $server);

[ 8. Perilaku autodie ]

  # Konteks void: die jika gagal
  open($fh, "< no_such_file.txt");   # dies: Can't open(...)

  # Dengan nilai kembalian: undef/0 jika gagal
  my $rc = open($fh, "< file.txt");
  unless ($rc) { warn "open gagal: $!" }

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

[ 9. Handle yang dikembalikan ]

  readline($fh)        # baca satu baris
  read($fh, $buf, $n)  # baca N byte
  print $fh "..."      # tulis
  binmode($fh)         # mode biner
  seek($fh, 0, 0)      # ke awal
  tell($fh)            # posisi saat ini
  close($fh)           # tutup
  eof($fh)             # periksa akhir file

[ 10. Kompatibilitas ]

  Versi Perl  : 5.005_03 ke atas (termasuk 5.42)
  Platform    : Unix, Linux, macOS, Windows (CRLF ditangani otomatis)
  Dependensi  : Fcntl (modul inti)

[ 11. Official resources ]

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

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

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