Perl version

Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

do

Perl 5 version 26.1 documentation
Recently read

do

  • do BLOCK

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

    do BLOCK does not count as a loop, so the loop control statements next, last, or redo cannot be used to leave or restart the block. See perlsyn for alternative strategies.

  • do EXPR

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

    1. # load the exact specified file (./ and ../ special-cased)
    2. do '/foo/stat.pl';
    3. do './stat.pl';
    4. do '../foo/stat.pl';
    5. # search for the named file within @INC
    6. do 'stat.pl';
    7. do 'foo/stat.pl';

    do './stat.pl' is largely like

    1. 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 with do 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.

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

    1. 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:

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

    If do can read the file but cannot compile it, it returns undef and sets an error message in $@ . If do 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 the use and require operators, which also do automatic error checking and raise an exception if there's a problem.

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

    1. # Read in config files: system first, then user.
    2. # Beware of using relative pathnames here.
    3. for $file ("/share/prog/defaults.rc",
    4. "$ENV{HOME}/.someprogrc")
    5. {
    6. unless ($return = do $file) {
    7. warn "couldn't parse $file: $@" if $@;
    8. warn "couldn't do $file: $!" unless defined $return;
    9. warn "couldn't run $file" unless $return;
    10. }
    11. }