Change 28553 by [EMAIL PROTECTED] on 2006/07/12 09:27:59

        Subject: [PATCH] perlinro (use $fh filehandler + not to use built in 
function name in sub example)
        From: "Gabor Szabo" <[EMAIL PROTECTED]>
        Date: Wed, 12 Jul 2006 10:51:03 +0300
        Message-ID: <[EMAIL PROTECTED]>

Affected files ...

... //depot/perl/pod/perlintro.pod#10 edit

Differences ...

==== //depot/perl/pod/perlintro.pod#10 (text) ====
Index: perl/pod/perlintro.pod
--- perl/pod/perlintro.pod#9~23276~     2004-09-07 05:57:58.000000000 -0700
+++ perl/pod/perlintro.pod      2006-07-12 02:27:59.000000000 -0700
@@ -359,6 +359,8 @@
         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 @@
 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 @@
 
 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 @@
 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 @@
 
 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 @@
         return $result;
     }
 
+Then use it like:
+
+    $sq = square(8);
+
 For more information on writing subroutines, see L<perlsub>.
 
 =head2 OO Perl
End of Patch.

Reply via email to