Manual

- Overview
- Tutorials
- FAQs
- History / Changes
- License

Reference

- Language
- Functions
- Operators
- Special Variables
- Pragmas
- Utilities
- Internals
- Platform Specific

Modules

- A • B • C • D • E
- F • G • H • I • L
- M • N • O • P • S
- T • U • X

News / Blogs

- Perl Buzz
- Perl Iron Man
- Perlsphere
- Planet Perl
- TPF news
- Use Perl

See also

- CPAN
- Perl.org
- Perl.com
- Perl 5 Wiki
- Perl Jobs
- Perl Mongers
- Perl Monks

Contact

- Site designed and maintained by Jon Allen (JJ)
- http://perl.jonallen.info

- View the project page and FAQs

# do

Perl 5 version 10.1 documentation
Go to top

# do

Perl functions A-Z | Perl functions by category | The 'perlfunc' manpage

- 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 SUBROUTINE(LIST)

  This form of subroutine call is deprecated. See perlsub.

- do EXPR

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

          do 'stat.pl';

  is just like

          eval `cat stat.pl`;

  except that it's more efficient and concise, keeps track of the current filename for error messages, searches the @INC directories, and updates `%INC` if the file is found. See "Predefined Names" in perlvar for these variables. It also differs in that code evaluated with `do FILENAME` 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.

  If `do` cannot read the file, it returns undef and sets `$!` to the error. If `do` can read the file but cannot compile it, it returns undef and sets an error message in `$@` . If the file is successfully compiled, `do` returns the value of the last expression evaluated.

  Note that 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:

          # 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;	}    }

Recently read

Tools

- Preferences