summaryrefslogtreecommitdiff
path: root/pod/perlintro.pod
diff options
context:
space:
mode:
authorGabor Szabo <szabgab@gmail.com>2006-07-12 13:51:03 +0300
committerH.Merijn Brand <h.m.brand@xs4all.nl>2006-07-12 09:27:59 +0000
commit74375ba58c5a8334aafcded09ef8d55752fd90c7 (patch)
tree5e4d3964622a00f6a2a5f8702240d33c75e48350 /pod/perlintro.pod
parent91dba0be91f1c35e9474de79c0592c0c8d8379dc (diff)
downloadperl-74375ba58c5a8334aafcded09ef8d55752fd90c7.tar.gz
perlinro (use $fh filehandler + not to use built in function name in sub example)
From: "Gabor Szabo" <szabgab@gmail.com> Message-ID: <d8a74af10607120051t10382a7fw95ce094f0f395490@mail.gmail.com> p4raw-id: //depot/perl@28553
Diffstat (limited to 'pod/perlintro.pod')
-rw-r--r--pod/perlintro.pod33
1 files changed, 22 insertions, 11 deletions
diff --git a/pod/perlintro.pod b/pod/perlintro.pod
index cb115ecb75..cd48843f91 100644
--- a/pod/perlintro.pod
+++ b/pod/perlintro.pod
@@ -359,6 +359,8 @@ the more friendly list scanning C<foreach> loop.
print "This element is $_\n";
}
+ print $list[$_] foreach 1 .. $max;
+
# you don't have to use the default $_ either...
foreach my $key (keys %hash) {
print "The value of $key is $hash{$key}\n";
@@ -444,17 +446,17 @@ You can open a file for input or output using the C<open()> function.
It's documented in extravagant detail in L<perlfunc> and L<perlopentut>,
but in short:
- open(INFILE, "input.txt") or die "Can't open input.txt: $!";
- open(OUTFILE, ">output.txt") or die "Can't open output.txt: $!";
- open(LOGFILE, ">>my.log") or die "Can't open logfile: $!";
+ 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 C<< <> >> 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 = <INFILE>;
- my @lines = <INFILE>;
+ my $line = <$in>;
+ my @lines = <$in>;
Reading in the whole file at one time is called slurping. It can
be useful but it may be a memory hog. Most text file processing
@@ -462,7 +464,7 @@ can be done a line at a time with Perl's looping constructs.
The C<< <> >> operator is most often seen in a C<while> loop:
- while (<INFILE>) { # assigns each line in turn to $_
+ while (<$in>) { # assigns each line in turn to $_
print "Just read in this line: $_";
}
@@ -471,13 +473,13 @@ However, C<print()> can also take an optional first argument specifying
which filehandle to print to:
print STDERR "This is your final warning.\n";
- print OUTFILE $record;
- print LOGFILE $logmessage;
+ print $out $record;
+ print $log $logmessage;
When you're done with your filehandles, you should C<close()> them
(though to be honest, Perl will clean up after you if you forget):
- close INFILE;
+ close $in or die "$in: $!";
=head2 Regular expressions
@@ -577,11 +579,16 @@ L<perlretut>, and L<perlre>.
Writing subroutines is easy:
- sub log {
+ sub logger {
my $logmessage = shift;
- print LOGFILE $logmessage;
+ 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!");
+
What's that C<shift>? Well, the arguments to a subroutine are available
to us as a special array called C<@_> (see L<perlvar> for more on that).
The default argument to the C<shift> function just happens to be C<@_>.
@@ -601,6 +608,10 @@ Subroutines can also return values:
return $result;
}
+Then use it like:
+
+ $sq = square(8);
+
For more information on writing subroutines, see L<perlsub>.
=head2 OO Perl