Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Optional trailing block in a w ...Terminate this program (Berikutnya)
Keywords related to control flow of your perl program

Turn a BLOCK into a TERM

Daftar Isi

  • do BLOCK

    Not really a function. Returns the value of the last command in thesequence of commands indicated by BLOCK. When modified by the while oruntil loop modifier, executes the BLOCK once before testing the loopcondition. (On other statements the loop modifiers test the conditionalfirst.)

    do BLOCK does not count as a loop, so the loop control statementsnext, 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. SUBROUTINE can be a bareword,a scalar variable or a subroutine beginning with &.

  • do EXPR

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

    1. do 'stat.pl';

    is just like

    1. eval `cat stat.pl`;

    except that it's more efficient and concise, keeps track of the currentfilename for error messages, searches the @INC directories, and updates%INC if the file is found. See @INC in perlvar and %INC in perlvar forthese variables. It also differs in that code evaluated with do FILENAMEcannot see lexicals in the enclosing scope; eval STRING does. It's thesame, 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 can read the file but cannot compile it, it returns undef and setsan error message in $@. If do cannot read the file, it returns undefand sets $! to the error. Always check $@ first, as compilationcould fail in a way that also sets $!. If the file is successfullycompiled, do returns the value of the last expression evaluated.

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

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

    1. # read in config files: system first, then user
    2. for $file ("/share/prog/defaults.rc",
    3. "$ENV{HOME}/.someprogrc")
    4. {
    5. unless ($return = do $file) {
    6. warn "couldn't parse $file: $@" if $@;
    7. warn "couldn't do $file: $!" unless defined $return;
    8. warn "couldn't run $file" unless $return;
    9. }
    10. }
 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Optional trailing block in a w ...Terminate this program (Berikutnya)