New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove IO::File example from perlfunc #12862
Comments
From @epaCreated by @epaPerfunc contains some example code for 'read_myfile_munged'. Perl Info
|
From @jkeenanOn Tue Mar 19 10:42:44 2013, eda@waniasset.com wrote:
Would you be able to provide a patch? Thank you very much. |
The RT System itself - Status changed from 'new' to 'open' |
From @epaInline Patchdiff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index 7088a9e..a249af2 100644
--- a/pod/perlfunc.pod
+++ b/pod/perlfunc.pod
@@ -3865,12 +3865,6 @@ FILEHANDLE is an expression, its value is the real filehandle. (This is
considered a symbolic reference, so C<use strict "refs"> should I<not> be
in effect.)
-If EXPR is omitted, the global (package) scalar variable of the same
-name as the FILEHANDLE contains the filename. (Note that lexical
-variables--those declared with C<my> or C<state>--will not work for this
-purpose; so if you're using C<my> or C<state>, specify EXPR in your
-call to open.)
-
If three (or more) arguments are specified, the open mode (including
optional encoding) in the second argument are distinct from the filename in
the third. If MODE is C<< < >> or nothing, the file is opened for input.
@@ -3953,6 +3947,31 @@ where you want to format a suitable error message (but there are
modules that can help with that problem)) always check
the return value from opening a file.
+The filehandle will be closed when its reference count reaches
+zero. If it is a lexically scoped variable declared with C<my>,
+that usually means the end of the enclosing scope. However,
+this automatic close does not check for errors, so it is better
+to explicitly close filehandles, especially those used for writing:
+
+ close($handle)
+ || warn "close failed: $!";
+
+An older style is to use a bareword as the filehandle, as
+
+ open(FH, "<", "input.txt")
+ or die "cannot open < input.txt: $!";
+
+Then you can use FH as the filenandle, in C<< close FH >> and C<< <FH> >> and so on.
+Note that it's a global variable, so this form is not recommeded in new code.
+As a shortcut a one-argument call takes the filename from the global scalar variable
+of the same name as the filehandle:
+
+ $ARTICLE = 100;
+ open(ARTICLE) or die "Can't find article $ARTICLE: $!\n";
+
+Here $ARTICLE must be a global (package) scalar variable - not one
+declared with C<my> or C<state>.
+
As a special case the three-argument form with a read/write mode and the third
argument being C<undef>:
@@ -3977,10 +3996,6 @@ To (re)open C<STDOUT> or C<STDERR> as an in-memory file, close it first:
General examples:
- $ARTICLE = 100;
- open(ARTICLE) or die "Can't find article $ARTICLE: $!\n";
- while (<ARTICLE>) {...
-
open(LOG, ">>/usr/spool/news/twitlog"); # (log is reserved)
# if the open fails, output is discarded
@@ -4225,34 +4240,6 @@ interpretation. For example:
seek(HANDLE, 0, 0);
print "File contains: ", <HANDLE>;
-Using the constructor from the C<IO::Handle> package (or one of its
-subclasses, such as C<IO::File> or C<IO::Socket>), you can generate anonymous
-filehandles that have the scope of the variables used to hold them, then
-automatically (but silently) close once their reference counts become
-zero, typically at scope exit:
-
- use IO::File;
- #...
- sub read_myfile_munged {
- my $ALL = shift;
- # or just leave it undef to autoviv
- my $handle = IO::File->new;
- open($handle, "<", "myfile") or die "myfile: $!";
- $first = <$handle>
- or return (); # Automatically closed here.
- mung($first) or die "mung failed"; # Or here.
- return (first, <$handle>) if $ALL; # Or here.
- return $first; # Or here.
- }
-
-B<WARNING:> The previous example has a bug because the automatic
-close that happens when the refcount on C<handle> reaches zero does not
-properly detect and report failures. I<Always> close the handle
-yourself and inspect the return value.
-
- close($handle)
- || warn "close failed: $!";
-
See L</seek> for some details about mixing reading and writing.
Portability issues: L<perlport/open>.
|
From @b2gillsLooks good to me. |
From @tamiasOn Fri, Mar 22, 2013 at 10:45:28AM +0000, Ed Avis wrote:
"recommended"
How about wrapping that paragraph so the lines are under 80 characters, as Ronald |
From @epaPlease do change the spelling of recommended and do any needed word-wrapping -- ______________________________________________________________________ |
From @tonycozOn Wed Mar 27 03:22:13 2013, eda@waniasset.com wrote:
I've attached an updated version of the patch that applies to blead, Tony |
From @tonycoz0001-perl-117223-Remove-IO-File-example-from-perlfunc.patchFrom 78d8752222b627b5c033e2861a981c17fbd43d1b Mon Sep 17 00:00:00 2001
From: Ed Avis <eda@waniasset.com>
Date: Fri, 5 Jul 2013 15:03:41 +1000
Subject: [PATCH] [perl #117223] Remove IO::File example from perlfunc
updated to apply to blead, minor spelling and word wrap fixes by Tony
Cook.
---
AUTHORS | 1 +
pod/perlfunc.pod | 65 +++++++++++++++++++++++-------------------------------
2 files changed, 28 insertions(+), 38 deletions(-)
diff --git a/AUTHORS b/AUTHORS
index 01b6e1d..20bddda 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -345,6 +345,7 @@ Drew Stephens <drewgstephens@gmail.com>
Duke Leto <jonathan@leto.net>
Duncan Findlay <duncf@debian.org>
E. Choroba <choroba@weed.(none)>
+Ed Avis <eda@waniasset.com>
Ed Mooring <mooring@Lynx.COM>
Ed Santiago <esm@pobox.com>
Eddy Tan <eddy.net@gmail.com>
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index 167eaef..82ac7d9 100644
--- a/pod/perlfunc.pod
+++ b/pod/perlfunc.pod
@@ -3907,12 +3907,6 @@ FILEHANDLE is an expression, its value is the real filehandle. (This is
considered a symbolic reference, so C<use strict "refs"> should I<not> be
in effect.)
-If EXPR is omitted, the global (package) scalar variable of the same
-name as the FILEHANDLE contains the filename. (Note that lexical
-variables--those declared with C<my> or C<state>--will not work for this
-purpose; so if you're using C<my> or C<state>, specify EXPR in your
-call to open.)
-
If three (or more) arguments are specified, the open mode (including
optional encoding) in the second argument are distinct from the filename in
the third. If MODE is C<< < >> or nothing, the file is opened for input.
@@ -3995,6 +3989,33 @@ where you want to format a suitable error message (but there are
modules that can help with that problem)) always check
the return value from opening a file.
+The filehandle will be closed when its reference count reaches zero.
+If it is a lexically scoped variable declared with C<my>, that usually
+means the end of the enclosing scope. However, this automatic close
+does not check for errors, so it is better to explicitly close
+filehandles, especially those used for writing:
+
+ close($handle)
+ || warn "close failed: $!";
+
+An older style is to use a bareword as the filehandle, as
+
+ open(FH, "<", "input.txt")
+ or die "cannot open < input.txt: $!";
+
+Then you can use C<FH> as the filenandle, in C<< close FH >> and C<<
+<FH> >> and so on. Note that it's a global variable, so this form is
+not recommended in new code.
+
+As a shortcut a one-argument call takes the filename from the global
+scalar variable of the same name as the filehandle:
+
+ $ARTICLE = 100;
+ open(ARTICLE) or die "Can't find article $ARTICLE: $!\n";
+
+Here C<$ARTICLE> must be a global (package) scalar variable - not one
+declared with C<my> or C<state>.
+
As a special case the three-argument form with a read/write mode and the third
argument being C<undef>:
@@ -4019,10 +4040,6 @@ To (re)open C<STDOUT> or C<STDERR> as an in-memory file, close it first:
General examples:
- $ARTICLE = 100;
- open(ARTICLE) or die "Can't find article $ARTICLE: $!\n";
- while (<ARTICLE>) {...
-
open(LOG, ">>/usr/spool/news/twitlog"); # (log is reserved)
# if the open fails, output is discarded
@@ -4267,34 +4284,6 @@ interpretation. For example:
seek(HANDLE, 0, 0);
print "File contains: ", <HANDLE>;
-Using the constructor from the C<IO::Handle> package (or one of its
-subclasses, such as C<IO::File> or C<IO::Socket>), you can generate anonymous
-filehandles that have the scope of the variables used to hold them, then
-automatically (but silently) close once their reference counts become
-zero, typically at scope exit:
-
- use IO::File;
- #...
- sub read_myfile_munged {
- my $ALL = shift;
- # or just leave it undef to autoviv
- my $handle = IO::File->new;
- open($handle, "<", "myfile") or die "myfile: $!";
- $first = <$handle>
- or return (); # Automatically closed here.
- mung($first) or die "mung failed"; # Or here.
- return (first, <$handle>) if $ALL; # Or here.
- return $first; # Or here.
- }
-
-B<WARNING:> The previous example has a bug because the automatic
-close that happens when the refcount on C<handle> reaches zero does not
-properly detect and report failures. I<Always> close the handle
-yourself and inspect the return value.
-
- close($handle)
- || warn "close failed: $!";
-
See L</seek> for some details about mixing reading and writing.
Portability issues: L<perlport/open>.
--
1.7.10.4
|
From @cpansproutOn Thu Jul 04 22:12:03 2013, tonyc wrote:
It looks good to me. (I tend to disagree with Ed Avis on many issues, so I am surprised how -- Father Chrysostomos |
From @jkeenanOn 7/5/13 1:12 AM, Tony Cook via RT wrote:
One spelling correction: |
From @tonycozOn Fri Jul 05 15:57:00 2013, jkeen@verizon.net wrote:
Applied as 1578dcc with this fix. Tony |
@tonycoz - Status changed from 'open' to 'resolved' |
From @khwilliamsonI'm reopening this so that the issues raised in can be resolved -- |
@khwilliamson - Status changed from 'resolved' to 'open' |
From @epaThanks for looking at this. However, I think the behaviour of a single-argument open() call is still documented, just with different wording. -- ______________________________________________________________________ |
From @jkeenanOn Mon Oct 13 10:10:53 2014, khw wrote:
I've read that blog post a couple of times. It has been revised in light of the comments posted to it. Ed Avis pointed to the section of 'perldoc -f open' that addressed the concerns raised in the blog post. So I think this RT can be closed again. -- |
From @dcollinsnOn Sun Oct 19 16:14:17 2014, jkeenan wrote:
Agreed, and one-arg open() is documented in blead ("As a shortcut a one-argument call takes the filename from the global scalar variable of the same name as the filehandle:") Un-warnocking and closing resolved. -- |
@dcollinsn - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#117223 (status was 'resolved')
Searchable as RT117223$
The text was updated successfully, but these errors were encountered: