develooper Front page | perl.perl5.porters | Postings from June 2015

Re: Question/suggestion on perlfunc.pod example

Thread Previous | Thread Next
From:
Glenn Golden
Date:
June 4, 2015 11:08
Subject:
Re: Question/suggestion on perlfunc.pod example
Message ID:
20150604110832.GA555@huh.zplane.com
Aristotle Pagaltzis <pagaltzis@gmx.de> [2015-06-01 02:43:31 +0200]:
> 
> Check the super quick patch guide in perldoc perlhack, it’ll give
> you the procedure to produce the preferred form.
> 

OK thanks. Here's the text of the patchfile produced by "git format-patch -1":


---
 pod/perlfunc.pod | 67 ++++++++++++++++++++++++++++----------------------------
 1 file changed, 34 insertions(+), 33 deletions(-)

diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index ba77638..19c734a 100644
--- a/pod/perlfunc.pod
+++ b/pod/perlfunc.pod
@@ -1571,42 +1571,43 @@ file as a Perl script.
 
     do 'stat.pl';
 
-is largely like
+is functionally similar to 
 
     eval `cat stat.pl`;
 
-except that it's more concise, runs no external processes, keeps track of
-the current
-filename for error messages, searches the C<@INC> directories, and updates
-C<%INC> if the file is found.  See L<perlvar/@INC> and L<perlvar/%INC> for
-these variables.  It also differs in that code evaluated with C<do FILENAME>
-cannot see lexicals in the enclosing scope; C<eval STRING> does.  It's the
-same, however, in that it does reparse the file every time you call it,
-so you probably don't want to do this inside a loop.
-
-If C<do> can read the file but cannot compile it, it returns C<undef> and sets
-an error message in C<$@>.  If C<do> cannot read the file, it returns undef
-and sets C<$!> to the error.  Always check C<$@> first, as compilation
-could fail in a way that also sets C<$!>.  If the file is successfully
-compiled, C<do> returns the value of the last expression evaluated.
-
-Inclusion of library modules is better done with the
-C<use> and C<require> operators, which also do automatic error checking
-and raise an exception if there's a problem.
-
-You might like to use C<do> to read in a program configuration
-file.  Manual error checking can be done this way:
-
-    # read in config files: system first, then user
-    for $file ("/share/prog/defaults.rc",
-               "$ENV{HOME}/.someprogrc")
-    {
-        unless ($return = do $file) {
-            warn "couldn't parse $file: $@" if $@;
-            warn "couldn't do $file: $!"    unless defined $return;
-            warn "couldn't run $file"       unless $return;
-        }
-    }
+except that it runs no external processes, keeps track of the current
+filename for error messages, searches the C<@INC> directories, and
+updates C<%INC> if the file is found.  Also, code evaluated with C<do EXPR>
+cannot see lexicals in the enclosing scope; C<eval STRING> does. 
+It's the same, however, in that it re-parses the file every time you call it,
+so you probably don't want to do this inside a performance-critical loop.
+
+If C<do> is able to read the file but cannot compile it, or if an exception
+is thrown during execution, it returns C<undef> and places an error message
+in C<$@>.  If C<do> cannot read the file, it returns undef and sets C<$!>
+to the error.  Always check C<$@> first, as compilation could fail in a
+way that also sets C<$!>, e.g.:
+
+
+    local ($!, $@);
+    my $result = do $file;
+    if ($@)
+       { warn "Couldn't compile $file or exception thrown: $@" }
+    elsif ($! and not defined $result)
+       { warn "Couldn't read $file: $!" }
+
+
+Note that the above is still not entirely reliable: Unless $! has been
+localized within C<$file>, it may wind up being misreported.  The same
+applies to C<$@> for perls prior to 5.14.
+
+If the file is successfully compiled, C<do> returns the value of the last
+expression evaluated.
+
+Inclusion of library modules is better done with the C<use> and C<require>
+operators, which also do automatic error checking and raise an exception if
+there's a problem.
+
 
 =item dump LABEL
 X<dump> X<core> X<undump>
-- 
2.4.1


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About