| Newsgroups |
perl.cvs.perlfaq |
| Message-ID |
<[email protected]> |
cvsuser 02/06/20 21:32:00
Modified: . perlfaq7.pod
Log:
* How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?
+ removed dated excerpt from perldata
+ rewrote filehande examples using more modern scalar variable
versions of them
* How do I skip some return values?
+ added example using a list slice with multiple elements selected
Revision Changes Path
1.9 +19 -30 perlfaq/perlfaq7.pod
Index: perlfaq7.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq7.pod,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -w -r1.8 -r1.9
--- perlfaq7.pod 26 Mar 2002 15:48:32 -0000 1.8
+++ perlfaq7.pod 21 Jun 2002 04:32:00 -0000 1.9
@@ -1,6 +1,6 @@
=head1 NAME
-perlfaq7 - General Perl Language Issues ($Revision: 1.8 $, $Date: 2002/03/26 15:48:32 $)
+perlfaq7 - General Perl Language Issues ($Revision: 1.9 $, $Date: 2002/06/21 04:32:00 $)
=head1 DESCRIPTION
@@ -82,6 +82,11 @@
($dev, $ino, undef, undef, $uid, $gid) = stat($file);
+You can also use a list slice to select only the elements that
+you need:
+
+ ($dev, $ino, $uid, $gid) = ( stat($file) )[0,1,4,5];
+
=head2 How do I temporarily block warnings?
If you are running Perl 5.6.0 or better, the C<use warnings> pragma
@@ -298,37 +303,21 @@
=item Passing Filehandles
-To pass filehandles to subroutines, use the C<*FH> or C<\*FH> notations.
-These are "typeglobs"--see L<perldata/"Typeglobs and Filehandles">
-and especially L<perlsub/"Pass by Reference"> for more information.
+As of Perl 5.6, you can represent filehandles with scalar variables
+which you treat as any other scalar.
-Here's an excerpt:
+ open my $fh, $filename or die "Cannot open $filename! $!";
+ func( $fh );
-If you're passing around filehandles, you could usually just use the bare
-typeglob, like *STDOUT, but typeglobs references would be better because
-they'll still work properly under C<use strict 'refs'>. For example:
-
- splutter(\*STDOUT);
- sub splutter {
- my $fh = shift;
- print $fh "her um well a hmmm\n";
- }
+ sub func {
+ my $passed_fh = shift;
- $rec = get_rec(\*STDIN);
- sub get_rec {
- my $fh = shift;
- return scalar <$fh>;
+ my $line = <$fh>;
}
-If you're planning on generating new filehandles, you could do this:
-
- sub openit {
- my $path = shift;
- local *FH;
- return open (FH, $path) ? *FH : undef;
- }
- $fh = openit('< /etc/motd');
- print <$fh>;
+Before Perl 5.6, you had to use the C<*FH> or C<\*FH> notations.
+These are "typeglobs"--see L<perldata/"Typeglobs and Filehandles">
+and especially L<perlsub/"Pass by Reference"> for more information.
=item Passing Regexes