[perl/en] how to write to files (#2633)

This commit is contained in:
Dan Book 2017-01-21 06:06:52 -05:00 committed by ven
parent 7402500407
commit ff362fc01f

View File

@ -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: