From: Gabor Szabo Date: Wed, 12 Jul 2006 10:51:03 +0000 (+0300) Subject: perlinro (use $fh filehandler + not to use built in function name in sub example) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=74375ba58c5a8334aafcded09ef8d55752fd90c7;p=p5sagit%2Fp5-mst-13.2.git perlinro (use $fh filehandler + not to use built in function name in sub example) From: "Gabor Szabo" Message-ID: p4raw-id: //depot/perl@28553 --- diff --git a/pod/perlintro.pod b/pod/perlintro.pod index cb115ec..cd48843 100644 --- a/pod/perlintro.pod +++ b/pod/perlintro.pod @@ -359,6 +359,8 @@ the more friendly list scanning C 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 function. It's documented in extravagant detail in L and L, 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 = ; - my @lines = ; + 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 loop: - while () { # 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 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 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, and L. 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? Well, the arguments to a subroutine are available to us as a special array called C<@_> (see L for more on that). The default argument to the C 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. =head2 OO Perl