summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAdam Bard <github@adambard.com>2013-08-27 20:55:30 -0700
committerAdam Bard <github@adambard.com>2013-08-27 20:55:30 -0700
commite1922e70a9f2532ceae94b9346172cfb4248df00 (patch)
tree1665db526cf5292aaa5d58725528f873da41b905
parentc48c23c87191f8f3e560683d6923ce08de63ba8c (diff)
parent9a36a51ccc1cb7d64baac17a067be71a7923afdd (diff)
Merge pull request #271 from korjavin/master
Small imrovement
-rw-r--r--perl.html.markdown35
1 files changed, 33 insertions, 2 deletions
diff --git a/perl.html.markdown b/perl.html.markdown
index 024bd851..18339dde 100644
--- a/perl.html.markdown
+++ b/perl.html.markdown
@@ -104,6 +104,35 @@ $a =~ s/foo/bar/; # replaces foo with bar in $a
$a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a
+#### Files and I/O
+
+# You can open a file for input or output using the "open()" function.
+
+open(my $in, "<", "input.txt") or die "Can't open input.txt: $!";
+open(my $out, ">", "output.txt") or die "Can't open output.txt: $!";
+open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
+
+# You can read from an open filehandle using the "<>" operator. In scalar context it reads a single line from
+# the filehandle, and in list context it reads the whole file in, assigning each line to an element of the list:
+
+my $line = <$in>;
+my @lines = <$in>;
+
+#### Writing subroutines
+
+# Writing subroutines is easy:
+
+sub logger {
+ my $logmessage = shift;
+ open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
+ print $logfile $logmessage;
+}
+
+# Now we can use the subroutine just as any other built-in function:
+
+logger("We have a logger subroutine!");
+
+
```
#### Using Perl modules
@@ -114,5 +143,7 @@ perlfaq contains questions and answers related to many common tasks, and often p
#### Further Reading
-[Learn at www.perl.com](http://www.perl.org/learn.html)
- and perldoc perlintro
+ - [perl-tutorial](http://perl-tutorial.org/)
+ - [Learn at www.perl.com](http://www.perl.org/learn.html)
+ - [perldoc](http://perldoc.perl.org/)
+ - and perl built-in : `perldoc perlintro`