summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDan Book <grinnz@gmail.com>2017-01-21 06:06:52 -0500
committerven <vendethiel@hotmail.fr>2017-01-21 12:06:52 +0100
commitff362fc01fa798727c8423d20f093e25702af52b (patch)
tree74ed87f0942491d3b61116a0dd60798daf71e200
parent7402500407bf41931440be7d1e992cd8ff406403 (diff)
[perl/en] how to write to files (#2633)
-rw-r--r--perl.html.markdown9
1 files changed, 9 insertions, 0 deletions
diff --git a/perl.html.markdown b/perl.html.markdown
index 3cbd2801..908f300b 100644
--- a/perl.html.markdown
+++ b/perl.html.markdown
@@ -170,8 +170,11 @@ $x =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $x
# You can open a file for input or output using the "open()" function.
+# For reading:
open(my $in, "<", "input.txt") or die "Can't open input.txt: $!";
+# For writing (clears file if it exists):
open(my $out, ">", "output.txt") or die "Can't open output.txt: $!";
+# For writing (appends to end of file):
open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
# You can read from an open filehandle using the "<>" operator. In
@@ -182,6 +185,12 @@ open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
my $line = <$in>;
my @lines = <$in>;
+# You can write to an open filehandle using the standard "print"
+# function.
+
+print $out @lines;
+print $log $msg, "\n";
+
#### Writing subroutines
# Writing subroutines is easy: