Include a file: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Tcl: Added implementation)
(→‎{{header|Tcl}}: Describe the other way of doing it)
Line 62: Line 62:
The built-in <code>source</code> command does exactly inclusion of code into the currently executing scope, subject to minor requirements of being well-formed Tcl script that is sourced in the first place (and the ability to introspect via <code>info script</code>):
The built-in <code>source</code> command does exactly inclusion of code into the currently executing scope, subject to minor requirements of being well-formed Tcl script that is sourced in the first place (and the ability to introspect via <code>info script</code>):
<lang tcl>source "foobar.tcl"</lang>
<lang tcl>source "foobar.tcl"</lang>

Note that it is more usually considered good practice to arrange code into ''packages'' that can be loaded in with more regular semantics (including version handling, only-once semantics, integration of code written in other languages such as [[C]], etc.)
<lang tcl>package require foobar 1.3</lang>
In the case of packages that are implemented using Tcl code, these will actually be incorporated into the program using the <code>source</code> command, though this is formally an implementation detail of those packages.


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==

Revision as of 13:35, 31 May 2011

Task
Include a file
You are encouraged to solve this task according to the task description, using any language you may know.

The task is to demontrate the languages ability to include source code from other files.

C

In C, inclusion of other files is achieved via a preprocessor. The #include preprocessor directive tells the compiler to incorporate code from the included file. This is normally used near the top of a source file and is usually used to tell the compiler to include header files for the function libraries.

<lang c> /* Standard library header names are enclosed using chevron enclosures */

#include <stdlib.h>
/* User library header names are enclosed using doublequotes */
#include "mylib.h" </lang>

Delphi

<lang Delphi>uses SysUtils; // Lets you use the contents of SysUtils.pas from the current unit

{$Include Common} // Inserts the contents of Common.pas into the current unit {$I Common} // Same as the previous line, but in a shorter form</lang>


J

The usual approach for a file named 'myheader.ijs' would be:

<lang j>require 'myheader.ijs'</lang>

However, this has "include once" semantics, and if the requirement is to include the file even if it has been included earlier you would instead use:

<lang j>load 'myheader.ijs'</lang>

Lua

To include a header file myheader.lua:

<lang lua> require "myheader" </lang>

PicoLisp

The function 'load' is used for recursively executing the contents of files. <lang PicoLisp>(load "file1.l" "file2.l" "file3.l")</lang>

PureBasic

IncludeFile will include the named source file at the current place in the code. <lang PureBasic>IncludeFile "Filename"</lang> XIncludeFile is exactly the same except it avoids including the same file several times. <lang PureBasic>XIncludeFile "Filename"</lang>

IncludeBinary will include a named file of any type at the current place in the code. IncludeBinary don't have to, but should preferably be done inside a data block. <lang PureBasic>IncludeBinary "Filename"</lang>

Python

Python supports the use of execfile to allow code from arbitrary files to be executed from a program (without using its modules system).

QBasic

QBasic supports the use of include files via the $INCLUDE directive. Note that the include directive is prefixed with an apostrophe dollar and that the name of the file for inclusion is enclosed in single quotation mark symbols.

<lang qbasic> '$INCLUDE: 'foobar.bi' </lang>

Tcl

The built-in source command does exactly inclusion of code into the currently executing scope, subject to minor requirements of being well-formed Tcl script that is sourced in the first place (and the ability to introspect via info script): <lang tcl>source "foobar.tcl"</lang>

Note that it is more usually considered good practice to arrange code into packages that can be loaded in with more regular semantics (including version handling, only-once semantics, integration of code written in other languages such as C, etc.) <lang tcl>package require foobar 1.3</lang> In the case of packages that are implemented using Tcl code, these will actually be incorporated into the program using the source command, though this is formally an implementation detail of those packages.

UNIX Shell

Inclusion of another file is achieved by using a dot inclusion operator:

<lang sh> . myfile.sh # Include the contents of myfile.sh </lang>

ZX Spectrum Basic

It is possible to include the contents of another program using the merge command. However, line numbers that coincide with those of the original program shall be overwritten, so it is best to reserve a block of line numbers for merged code:

<lang zxbasic>10 GOTO 9950 5000 REM We reserve line numbers 5000 to 8999 for merged code 9000 STOP: REM In case our line numbers are wrong 9950 REM Merge in our module 9955 MERGE "MODULE" 9960 REM Jump to the merged code. Pray it has the right line numbers! 9965 GOTO 5000 </lang>