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

Re: Question/suggestion on perlfunc.pod example

Thread Previous | Thread Next
From:
Glenn Golden
Date:
May 16, 2015 22:25
Subject:
Re: Question/suggestion on perlfunc.pod example
Message ID:
20150516222444.GB565@huh.zplane.com
Didn't hear from you re diff vs. raw-text, so just took a shot at it with
a diff.  Tried to capture everything we discussed in the thread, and also
poked around with some minor but hopefully value-added wordsmithing near
the top, unrelated to what we talked about.

The reference version is from git, cloned a few days ago.


==========================================================================

*** perlfunc.pod	2015-05-11 14:58:44.652092870 -0600
--- MOD_perlfunc.pod	2015-05-16 16:10:30.758641106 -0600
***************
*** 1571,1612 ****
  
      do 'stat.pl';
  
! is largely like
  
      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;
-         }
-     }
  
  =item dump LABEL
  X<dump> X<core> X<undump>
--- 1571,1614 ----
  
      do 'stat.pl';
  
! is similar to
  
      eval `cat stat.pl`;
  
! 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, whereas 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>

==========================================================================

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