diff options
author | Levi Bostian <levi.bostian@banno.com> | 2013-11-25 09:42:37 -0600 |
---|---|---|
committer | Levi Bostian <levi.bostian@banno.com> | 2013-11-25 09:42:37 -0600 |
commit | af6701904b459b16cf65709cd8c70fd2f5519457 (patch) | |
tree | 68cb4bf9ead32686f492e68528e9f0761e41c500 /perl.html.markdown | |
parent | df3cc00f5233dac96c0e063d87d3552f493e25f6 (diff) | |
parent | d24c824d388669181eed99c3e94bb25c2914304a (diff) |
Fix conflict bash.
Diffstat (limited to 'perl.html.markdown')
-rw-r--r-- | perl.html.markdown | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/perl.html.markdown b/perl.html.markdown index 024bd851..ad9155e4 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -104,15 +104,44 @@ $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 -Perl modules provide a range of features to help you avoid reinventing the wheel, and can be downloaded from CPAN ( http://www.cpan.org/ ). A number of popular modules are included with the Perl distribution itself. +Perl modules provide a range of features to help you avoid reinventing the wheel, and can be downloaded from CPAN (http://www.cpan.org/). A number of popular modules are included with the Perl distribution itself. perlfaq contains questions and answers related to many common tasks, and often provides suggestions for good CPAN modules to use. #### 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` |