Movatterモバイル変換


[0]ホーム

URL:


functions /do
(source,CPAN)
You are viewing the version of this documentation from Perl 5.34.0.View the latest version
#do BLOCK

Not really a function. Returns the value of the last command in the sequence of commands indicated by BLOCK. When modified by thewhile oruntil loop modifier, executes the BLOCK once before testing the loop condition. (On other statements the loop modifiers test the conditional first.)

do BLOCK doesnot count as a loop, so the loop control statementsnext,last, orredo cannot be used to leave or restart the block. Seeperlsyn for alternative strategies.

#do EXPR

Uses the value of EXPR as a filename and executes the contents of the file as a Perl script:

# load the exact specified file (./ and ../ special-cased)do '/foo/stat.pl';do './stat.pl';do '../foo/stat.pl';# search for the named file within @INCdo 'stat.pl';do 'foo/stat.pl';

do './stat.pl' is largely like

eval `cat stat.pl`;

except that it's more concise, runs no external processes, and keeps track of the current filename for error messages. It also differs in that code evaluated withdo FILE cannot see lexicals in the enclosing scope;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.

Usingdo with a relative path (except for./ and../), like

do 'foo/stat.pl';

will search the@INC directories, and update%INC if the file is found. See"@INC" in perlvar and"%INC" in perlvar for these variables. In particular, note that whilst historically@INC contained '.' (the current directory) making these two cases equivalent, that is no longer necessarily the case, as '.' is not included in@INC by default in perl versions 5.26.0 onwards. Instead, perl will now warn:

do "stat.pl" failed, '.' is no longer in @INC;did you mean do "./stat.pl"?

Ifdo can read the file but cannot compile it, it returnsundef and sets an error message in$@. Ifdo cannot read the file, it returns undef and sets$! to the error. Always check$@ first, as compilation could fail in a way that also sets$!. If the file is successfully compiled,do returns the value of the last expression evaluated.

Inclusion of library modules is better done with theuse andrequire operators, which also do automatic error checking and raise an exception if there's a problem.

You might like to usedo to read in a program configuration file. Manual error checking can be done this way:

# Read in config files: system first, then user.# Beware of using relative pathnames here.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;    }}

Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.

The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.


[8]ページ先頭

©2009-2025 Movatter.jp