Commits

Shlomi Fish  committed 89dc764

Add prepend_to_file_while slurping.

  • Participants
  • Parent commits 6ac44af

Comments (0)

Files changed (2)

             - reading files.
             - appending selected portion from a file to another file.
             - prepending to files.
+                @ DONE [Sun 2013-04-14]
             - overwriting lines in files.
             - writing to a file.
             - show all with IO::All and without it.

File src/topics/files-and-directories/index.html.wml

         }
     );
 
+    print $ex_gen->html_with_title(
+        {
+            title => "Prepending to a File (While Slurping)",
+            id_base => "prepend_to_file_while_slurping",
+            examples =>
+            [
+                {
+                    id => "io_all",
+                    label => "IO-All",
+                    code => <<'EOF',
+use IO::All;
+
+my $filename = 'foo.txt';
+my $text_to_prepend = "[Text to Prepend]\n";
+
+my $fh = io->file($filename);
+
+$fh->print( $text_to_prepend . $fh->slurp() );
+EOF
+
+                },
+                {
+                    id => "core",
+                    label => "Core Perl",
+                    code => <<'EOF',
+my $filename = 'foo.txt';
+my $text_to_prepend = "[Text to Prepend]\n";
+
+open my $in_fh, '<', $filename
+    or die "Could not open file '$filename' for reading! - $!";
+my $contents = do { local $/; <$in_fh>;};
+close($in_fh);
+
+open my $out_fh, '>', $filename
+    or die "Could not open file '$filename' for writing! - $!";
+print {$out_fh} $text_to_prepend . $contents;
+close($out_fh);
+EOF
+                },
+            ],
+        }
+    );
+
 :>