Include a file: Difference between revisions

→‎{{header|Wren}}: Code partly rewritten.
m (Corrected text.)
(→‎{{header|Wren}}: Code partly rewritten.)
 
(28 intermediate revisions by 16 users not shown)
Line 4:
Demonstrate the language's ability to include source code from other files.
<br><br>
 
;See Also
 
* [[Compiler/Simple file inclusion pre processor]]
<br>
<br>
 
 
=={{header|360 Assembly}}==
The COPY instruction includes source statements from the SYSLIB library.
<langsyntaxhighlight lang="360asm"> COPY member</langsyntaxhighlight>
=={{header|6502 Assembly}}==
There are two different directives for including files: <code>include</code> and <code>incbin</code>. <code>include</code> is for assembly code that will be converted to machine code that the computer can run. <code>incbin</code> is for binary data such as graphics, which the assembler will convert as-is to binary and does not attempt to translate it into machine code.
 
For this example, the file "foo.txt" is a file containing only the following: <code>RTS</code>
 
<syntaxhighlight lang="6502asm">;main.asm
org $8000
include "foo.txt"
;eof</syntaxhighlight>
 
A hexdump of the resulting machine code would look like this:
<pre>
HEXDUMP OF
$8000: 60 00 00 00 00 00 00 00 `.......
</pre>
 
Now compare it to the following:
<syntaxhighlight lang="6502asm">;main.asm
org $8000
incbin "foo.txt"
;eof</syntaxhighlight>
 
<pre>
HEXDUMP OF
$8000: 52 54 53 00 00 00 00 00 RTS.....
</pre>
 
As you can see, when <code>incbin</code> was used the assembler made no attempt to translate the assembly mnemonic into machine instructions. While anything that <code>incbin</code> can do, <code>include</code> can do also, using data blocks, it is often much easier to use <code>incbin</code> if you have a separate graphics creation program that can output graphics data into a raw binary form. Graphics data often contains hundreds of redundant bytes which can be very painful and time-consuming to type by hand.
 
Unlike high-level languages, the location of the <code>include</code> or <code>incbin</code> statement matters. This of course depends on the assembler, but generally speaking, there are a few limitations:
* If you <code>include</code> a macro <i>after</i> that macro is used in your source code, the assembler may raise an error message. If your macros are always before your code this isn't a problem.
 
* When you <code>include</code> or <code>incbin</code> a file, unless your assembler has linker support, the assembler treats the include statement as though you copied and pasted the entire contents of that file at the location in your document where the <code>include</code>/<code>incbin</code> statement is.
 
*<code>include</code>/<code>incbin</code> statements can be nested, i.e. an included file can contain its own included files, but there is a limit with how deep you can go. Nesting includes is never actually required, as the example below demonstrates:
 
<syntaxhighlight lang="6502asm">;main.asm
include "Subroutines.asm"
;eof
 
;Subroutines.asm
include "math.asm"
;eof
 
;math.asm
;eof</syntaxhighlight>
 
The above is assembled identically to:
<syntaxhighlight lang="6502asm">;main.asm
include "Subroutines.asm"
include "math.asm"
;eof</syntaxhighlight>
 
=={{header|68000 Assembly}}==
{{trans|6502 Assembly}}
There are two different directives for including files: <code>include</code> and <code>incbin</code>. <code>include</code> is for assembly code that will be converted to machine code that the computer can run.
<code>incbin</code> is for binary data such as graphics, which the assembler will convert as-is to binary and does not attempt to translate it into machine code.
 
Unless you're using a linker to arrange your various files into an executable, the location of your <code>include</code> and <code>incbin</code> directives ''imply the memory location of the code or data you're including.'' For example:
 
<syntaxhighlight lang="68000devpac">org $00000000
;512 bytes containing anything really
include "myFile.asm" ;assuming no org or align directives are in this file,
;its starting memory location is guaranteed to be $00000200 (512 in decimal)</syntaxhighlight>
 
This can become a problem if you have data that is not intended to be executed directly after a block of code, with nothing preventing "fallthrough." For example:
 
<syntaxhighlight lang="68000devpac">foo:
LEA myData,A0
MOVE.W (A0)+,D0
 
MyData:
include "dataTables.inc" ;a list of defined data constants that aren't supposed to be executed as instructions</syntaxhighlight>
 
Since there was no control flow instruction stopping the program counter from reaching the included file, ''the bytes stored in it will be executed as though they were genuine Motorola 68000 CPU instructions, even if they're not.'' The CPU cannot tell the difference between data and instructions, and thus the programmer must ensure that the program counter never reaches anything that wasn't meant to be executed. The easiest fix for this would be:
<syntaxhighlight lang="68000devpac">foo:
LEA myData,A0
MOVE.W (A0)+,D0
RTS ;return from subroutine, preventing the CPU from executing the data below.
 
MyData:
include "dataTables.inc" ;a list of defined data constants that aren't supposed to be executed as instructions</syntaxhighlight>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
'file constant include : includeConstantesARM64.inc'
/*******************************************/
Line 439 ⟶ 528:
.include "../includeARM64.inc"
 
</syntaxhighlight>
</lang>
 
=={{header|ACL2}}==
 
For files containing only events (definitions and similar; no top-level function calls) which are admissible (note the lack of file extension):
<langsyntaxhighlight Lisplang="lisp">(include-book "filename")</langsyntaxhighlight>
For all other files:
<langsyntaxhighlight Lisplang="lisp">(ld "filename.lisp")</langsyntaxhighlight>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
PROC Main()
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Include_a_file.png Screenshot from Atari 8-bit computer]
<pre>
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
</pre>
 
=={{header|Ada}}==
Some remarks are necessary here.
Ada does not define how the source code is stored in files. The language rather talks about compilation units. A compilation unit "imports" another compilation unit by using context clauses - these have the syntax "with CU1, CU2, ...;". All compilers I know of require in their standard mode exactly one compilation unit per file; also file naming conventions vary. However GNAT e.g. has a mode that can deal with files holding several compilation units and any file name conventions.
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Another_Package; use Ada.Text_IO;
-- the with-clause tells the compiler to include the Text_IO package from the Ada standard
-- and Another_Package. Subprograms from these packages may be called as follows:
Line 457 ⟶ 557:
-- Another_Package.Do_Something("some text");
-- The use-clause allows the program author to write a subprogram call shortly as
-- Put_Line("some text");</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 469 ⟶ 569:
==={{header|ALGOL 68G}}===
In the simplest case a file can be included as follows:
<langsyntaxhighlight lang="algol68">PR read "file.a68" PR</langsyntaxhighlight>
 
But in the Algol68 formal reports - it appears - the intention was to have a more structure approach.
Line 475 ⟶ 575:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.7 algol68g-2.7].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
'''File: prelude/test.a68'''<langsyntaxhighlight lang="algol68"># -*- coding: utf-8 -*- #
BEGIN
# Exception setup code: #
on value error(stand out, (REF FILE f)BOOL: GOTO value error not mended);
# Block setup code: #
printf(($"Prelude test:"l$))</langsyntaxhighlight>'''File: postlude/test.a68'''<langsyntaxhighlight lang="algol68"># -*- coding: utf-8 -*- #
# Block teardown code: #
printf(($"Postlude test."l$))
Line 486 ⟶ 586:
# Exception code: #
value error not mended: SKIP
END</langsyntaxhighlight>'''File: test/include.a68'''<langsyntaxhighlight lang="algol68">#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
 
PR read "prelude/test.a68" PR;
printf($4x"Hello, world!"l$);
PR read "postlude/test.a68" PR</langsyntaxhighlight>
{{out}}
<pre>
Line 547 ⟶ 647:
''' Example of <code>ENVIRON</code> clause '''<br>
A file called ''mylib.a68'':
<langsyntaxhighlight lang="algol68">BEGIN
INT dim = 3; # a constant #
INT a number := 120; # a variable #
Line 555 ⟶ 655:
a number := ENVIRON EXAMPLE2;
print((a number))
END</langsyntaxhighlight>
 
''' Example of <code>USING</code> clause '''<br>
A file called ''usemylib.a68'':
<langsyntaxhighlight lang="algol68">USING EXAMPLE2 FROM "mylib"
BEGIN
MATRIX m2; # example only #
Line 566 ⟶ 666:
ENVIRON EXAMPLE3; # ENVIRONs can be nested #
666
END</langsyntaxhighlight>
 
=={{header|AntLang}}==
AntLang is made for interactive programming, but a way to load files exists.
Even if it is really primitive, i. e. file get's current environment and manipulates it.
<langsyntaxhighlight AntLanglang="antlang">load["script.ant"]</langsyntaxhighlight>
 
=={{header|Applesoft BASIC}}==
Chain PROGRAM TWO to PROGRAM ONE. First create and save PROGRAM TWO. Then, create PROGRAM ONE and run it. PROGRAM ONE runs and then "includes" PROGRAM TWO which is loaded and run using the Binary program CHAIN from the DOS 3.3 System Master. Variables from PROGRAM ONE are not cleared so they can be used in PROGRAM TWO. User defined functions should be redefined in PROGRAM TWO. See "Applesoft: CHAIN and user-defined functions Issues" http://support.apple.com/kb/TA41069
 
<langsyntaxhighlight ApplesoftBASIClang="applesoftbasic}"> 10 REMPROGRAM TWO
20 DEF FN A(X) = X * Y
30 PRINT FN A(2)
 
SAVE PROGRAM TWO</langsyntaxhighlight>
<langsyntaxhighlight ApplesoftBASIClang="applesoftbasic}"> 10 REMPROGRAM ONE
20 Y = 6
30 DEF FN A(X) = X * Y
Line 590 ⟶ 690:
 
SAVE PROGRAM ONE
RUN</langsyntaxhighlight>
 
{{out}}
Line 601 ⟶ 701:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
'file constantes.inc'
/************************************/
Line 1,041 ⟶ 1,141:
.include "./affichage.inc"
 
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">; import a file
do.import {file.art}
 
; import another file
; from relative folder location
do.import relative "another_file.art"</syntaxhighlight>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">
<lang AutoHotkey>
#Include FileOrDirName
#IncludeAgain FileOrDirName
</syntaxhighlight>
</lang>
 
=={{header|AWK}}==
Line 1,053 ⟶ 1,162:
The awk extraction and reporting language does not support the use of include files. However, it is possible to provide the name of more than one source file at the command line:
 
<langsyntaxhighlight lang="sh">awk -f one.awk -f two.awk</langsyntaxhighlight>
 
The functions defined in different source files will be visible from other scripts called from the same command line:
 
<langsyntaxhighlight lang="awk"># one.awk
BEGIN {
sayhello()
Line 1,065 ⟶ 1,174:
function sayhello() {
print "Hello world"
}</langsyntaxhighlight>
 
However, it is not permissible to pass the name of additional source files through a hashbang line, so the following will will not work:
Line 1,074 ⟶ 1,183:
GNU Awk has an <code>@include</code> which can include another awk source file at that point in the code.
 
<langsyntaxhighlight lang="awk">@include "filename.awk"</langsyntaxhighlight>
 
This is a parser-level construct and so must be a literal filename, not a variable or expression. If the filename is not absolute then it's sought in an <code>$AWKPATH</code> list of directories. See [http://www.gnu.org/software/gawk/manual/html_node/Include-Files.html the gawk manual] for more.
Line 1,080 ⟶ 1,189:
=={{header|Axe}}==
This will cause the program called OTHER to be parsed as if it was contained in the source code instead of this line.
<syntaxhighlight lang ="axe">prgmOTHER</langsyntaxhighlight>
 
=={{header|BaCon}}==
''other.bac''
<langsyntaxhighlight lang="freebasic">other = 42</langsyntaxhighlight>
''including.bac''
<langsyntaxhighlight lang="freebasic">' Include a file
INCLUDE "other.bac"
PRINT other</langsyntaxhighlight>
 
{{out}}
Line 1,105 ⟶ 1,214:
Note that this will ''not'' work under QBasic.
 
<langsyntaxhighlight lang="qbasic">REM $INCLUDE: 'file.bi'
'$INCLUDE: 'file.bi'</langsyntaxhighlight>
 
See also: [[#BBC BASIC|BBC BASIC]], [[#Gambas|Gambas]], [[#IWBASIC|IWBASIC]], [[#PowerBASIC|PowerBASIC]], [[#PureBasic|PureBasic]], [[#Run BASIC|Run BASIC]], [[#ZX Spectrum Basic|ZX Spectrum Basic]]
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">
call file2.bat
</syntaxhighlight>
</lang>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang ="bbcbasic"> CALL filepath$</langsyntaxhighlight>
The file is loaded into memory at run-time, executed, and then discarded. It must be in 'tokenised' (internal) .BBC format.
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">get$"<i>module</i>"</langsyntaxhighlight>
 
=={{header|C}} / {{header|C++}}==
Line 1,127 ⟶ 1,236:
In C and C++, inclusion of other files is achieved via a preprocessor. The <code>#include</code> 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.
 
<langsyntaxhighlight lang="c">/* Standard and other library header names are enclosed between chevrons */
#include <stdlib.h>
 
/* User/in-project header names are usually enclosed between double-quotes */
#include "myutil.h"</lang>
</syntaxhighlight>
<syntaxhighlight lang="cpp">/* In C++20,you can use the import statement */
import <iostream>;
</syntaxhighlight>
 
Although it is often conventional and idiomatic for a project to use its own headers in the style described on the second line above, it's also possible to tell most compilers using various flags (e. g. GCC and Clang accept <tt>-I</tt>) to treat an arbitrary directory as a system/library include folder, thereby allowing any contained files to be included using the angle bracket syntax.
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">/* The C# language specification does not give a mechanism for 'including' one source file within another,
* likely because there is no need - all code compiled within one 'assembly' (individual IDE projects
* are usually compiled to separate assemblies) can 'see' all other code within that assembly.
*/</langsyntaxhighlight>
 
=={{header|ChucK}}==
<syntaxhighlight lang="text">Machine.add(me.dir() + "/MyOwnClassesDefinitions.ck");</langsyntaxhighlight>
 
=={{header|Clipper}}==
The inclusion of other files is achieved via a preprocessor. The <code>#include</code> 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.
<langsyntaxhighlight lang="clipper"> #include "inkey.ch" </langsyntaxhighlight>
 
=={{header|Clojure}}==
Just as in Common Lisp:
<langsyntaxhighlight lang="clojure">(load "path/to/file")</langsyntaxhighlight>
 
This would rarely be used for loading code though, since Clojure supports modularisation (like most modern languages) through [http://blog.8thlight.com/colin-jones/2010/12/05/clojure-libs-and-namespaces-require-use-import-and-ns.html namespaces] and code is typically located/loaded via related abstractions. It's probably more often used to load data or used for quick-and-dirty experiments in the [https://en.wikipedia.org/wiki/Read–eval–print_loop REPL].
Line 1,156 ⟶ 1,269:
=={{header|COBOL}}==
In COBOL, code is included from other files by the <code>COPY</code> statement. The files are called copybooks, normally end with the file extension '.cpy' and may contain ''any'' valid COBOL syntax. The <code>COPY</code> statement takes an optional <code>REPLACING</code> clause allows any text within the copybook to be replaced with something else.
<langsyntaxhighlight lang="cobol">COPY "copy.cpy". *> The full stop is mandatory, wherever the COPY is.
COPY "another-copy.cpy" REPLACING foo BY bar
SPACE BY ZERO
==text to replace== BY ==replacement text==.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(load "path/to/file")</langsyntaxhighlight>
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="crystal">require "socket" # includes a file from standard library or /lib relative to current directory
require "./myfile" # includes a file relative to current directory</langsyntaxhighlight>
 
=={{header|D}}==
D has a module system, so usually there is no need of a textual inclusion of a text file:
<syntaxhighlight lang ="d">import std.stdio;</langsyntaxhighlight>
 
To perform a textual inclusion:
<langsyntaxhighlight lang="d">mixin(import("code.txt"));</langsyntaxhighlight>
 
At the expression level, e.g for large ubyte and char arrays:
<syntaxhighlight lang="d">static immutable array = import("data.txt");</syntaxhighlight>
 
=={{header|Delphi}}==
<langsyntaxhighlight Delphilang="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</langsyntaxhighlight>
 
=={{header|Dragon}}==
Line 1,185 ⟶ 1,301:
 
Just this would be enough.
<syntaxhighlight lang="text">func my(){
showln "hello"
//this is program.dgn
}</langsyntaxhighlight>
 
<syntaxhighlight lang="text">
include "program.dgn"
my() // output : hello
</syntaxhighlight>
</lang>
 
=={{header|DWScript}}==
 
In addition to straight inclusion, there is a filtered inclusion, in which the include file goes through a pre-processing filter.
<syntaxhighlight lang="delphi">
<lang Delphi>
{$INCLUDE Common} // Inserts the contents of Common.pas into the current unit
{$I Common} // Same as the previous line, but in a shorter form
Line 1,204 ⟶ 1,320:
{$FILTER Common} // Inserts the contents of Common.pas into the current unit after filtering
{$F Common} // Same as the previous line, but in a shorter form
</syntaxhighlight>
</lang>
 
=={{header|Déjà Vu}}==
<langsyntaxhighlight lang="dejavu">#with the module system:
!import!foo
 
#passing a file name (only works with compiled bytecode files):
!run-file "/path/file.vu"</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
Write this code in: file1.el
<syntaxhighlight lang="emacs lisp">
<lang Emacs Lisp>
(defun sum (ls)
(apply '+ ls) )
</syntaxhighlight>
</lang>
In the directory of file1.el, we write this new code in: file2.el
<syntaxhighlight lang="emacs lisp">
<lang Emacs Lisp>
(add-to-list 'load-path "./")
(load "./file1.el")
(insert (format "%d" (sum (number-sequence 1 100) )))
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 1,231 ⟶ 1,347:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-include("my_header.hrl"). % Includes the file at my_header.erl
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">
<lang Euphoria>
include my_header.e
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">
<lang Factor>
USING: vocaba vocabb... ;
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
<syntaxhighlight lang ="forth">include matrix.fs</langsyntaxhighlight>
 
Other Forth systems have a smarter word, which protects against multiple inclusion. The name varies: '''USES''', '''REQUIRE''', '''NEEDS'''.
 
=={{header|Fortran}}==
<syntaxhighlight lang Fortran="fortran">include ''char-literal-constant''</langsyntaxhighlight>
 
"The interpretation of char-literal-constant is processor dependent. An example of a possible valid interpretation is that char-literal-constant is the name of a file that contains the source text to be included."
Line 1,262 ⟶ 1,378:
=={{header|FreeBASIC}}==
File to be included :
<langsyntaxhighlight lang="freebasic">' person.bi file
Type Person
name As String
Line 1,271 ⟶ 1,387:
Operator Person.Cast() As String
Return "[" + This.name + ", " + Str(This.age) + "]"
End Operator</langsyntaxhighlight>
 
Main file :
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win 64
 
' main.bas file
Line 1,285 ⟶ 1,401:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,291 ⟶ 1,407:
[Methuselah, 969]
</pre>
 
=={{header|Frink}}==
The statement <CODE>use <I>filename</I></CODE> includes a file.
The Frink documentation section [https://frinklang.org/#IncludingOtherFiles Including Other Files] describes how paths are searched for the file.
 
=={{header|Furryscript}}==
Line 1,299 ⟶ 1,419:
=={{header|FutureBasic}}==
FB has powerful tools to include files in a project. Its "include resources" statement allows you to specify any number of files for copying into the built application package's Contents/Resources/ directory.
<langsyntaxhighlight lang="futurebasic">
include resources "SomeImage.png"
include resources "SomeMovie.mpeg"
Line 1,305 ⟶ 1,425:
include resources "SomeIcon.icns"
include resources "Info.plist" //Custom preference file to replace FB's generic app preferences
</syntaxhighlight>
</lang>
Including C or Objective-C headers (i.e. files with the .h extension) or source files (files with the .c or .m extension) requires a different 'include' syntax:
<pre>
Line 1,369 ⟶ 1,489:
 
Here a file is loaded into a variable
<langsyntaxhighlight lang="gambas">Public Sub Form_Open()
Dim sFile As String
 
Line 1,375 ⟶ 1,495:
 
End
</syntaxhighlight>
</lang>
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">Read("file");</langsyntaxhighlight>
 
=={{header|Gnuplot}}==
<langsyntaxhighlight lang="gnuplot">load "filename.gnuplot"</langsyntaxhighlight>
 
This is the same as done for each file named on the command line. Special filename <code>"-"</code> reads from standard input.
 
<langsyntaxhighlight lang="gnuplot">load "-" # read standard input</langsyntaxhighlight>
 
If the system has <code>popen</code> then piped output from another program can be loaded,
 
<langsyntaxhighlight lang="gnuplot">load "< myprogram" # run myprogram, read its output
load "< echo print 123"</langsyntaxhighlight>
 
<code>call</code> is the same as <code>load</code> but takes parameters which are then available to the sub-script as <code>$0</code> through <code>$9</code>
 
<langsyntaxhighlight lang="gnuplot">call "filename.gnuplot" 123 456 "arg3"</langsyntaxhighlight>
 
=={{header|Go}}==
Line 1,400 ⟶ 1,520:
 
Instead one can simply give the other source code file(s) the same package name as the 'main' file, copy them to the same directory and build them all together. For example:
<langsyntaxhighlight lang="go">// main.go
package main
 
Line 1,412 ⟶ 1,532:
hello()
hello2()
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="go">// main2.go
package main
 
Line 1,421 ⟶ 1,541:
func hello2() {
fmt.Println("Hello from main2.go")
}</langsyntaxhighlight>
 
{{out}}
Line 1,433 ⟶ 1,553:
=={{header|Harbour}}==
The inclusion of other files is achieved via a preprocessor. The <code>#include</code> 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.
<langsyntaxhighlight lang="visualfoxpro">#include "inkey.ch"</langsyntaxhighlight>
 
=={{header|Haskell}}==
 
<langsyntaxhighlight Haskelllang="haskell">-- Due to Haskell's module system, textual includes are rarely needed. In
-- general, one will import a module, like so:
import SomeModule
Line 1,443 ⟶ 1,563:
-- Haskell Compiler runs the C preprocessor on source code, so #include may be
-- used:
#include "SomeModule.hs"</langsyntaxhighlight>
 
=={{header|HTML}}==
Line 1,449 ⟶ 1,569:
Current HTML specifications do not provide an include tag, Currently, in order to include content from another file, it is necessary to include content via an iframe. However, this is not supported in some browsers and looks very untidy in other browsers:
 
<langsyntaxhighlight lang="html"><iframe src="foobar.html">
Sorry: Your browser cannot show the included content.</iframe></langsyntaxhighlight>
 
There is an unofficial tag, but this will be ignored by most browsers:
 
<syntaxhighlight lang ="html"><include>foobar.html</include></langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
 
Include another file of source code using the preprocessor statement: <langsyntaxhighlight Iconlang="icon">$include "filename.icn"</langsyntaxhighlight>
 
=={{header|IWBASIC}}==
<langsyntaxhighlight IWBASIClang="iwbasic">$INCLUDE "ishelllink.inc"</langsyntaxhighlight>
 
Further, external library or object files can be specified with the $USE statement, which is a compiler preprocessor command:
 
<langsyntaxhighlight IWBASIClang="iwbasic">$USE "libraries\\mylib.lib"</langsyntaxhighlight>
 
IWBASIC also allows resources, files and data that are compiled with an application and embedded in the executable. However, resources in IWBASIC may be used only for projects, i.e., programs that have more than one source file.
Line 1,471 ⟶ 1,591:
Various resources are loaded as follows:
 
<langsyntaxhighlight IWBASIClang="iwbasic">Success=LOADRESOURCE(ID,Type,Variable)</langsyntaxhighlight>
 
<code>ID</code> is either a numeric or string identifier to the resource, <code>TYPE</code> is a numeric or string type and it stores the info in variable. The standard Windows resource types can be specified and loaded in raw form using the following constants:
 
<langsyntaxhighlight IWBASIClang="iwbasic">@RESCURSOR
@RESBITMAP
@RESICON
Line 1,486 ⟶ 1,606:
@RESGROUPCURSOR
@RESGROUPICON
@RESVERSION</langsyntaxhighlight>
 
=={{header|J}}==
Line 1,492 ⟶ 1,612:
The usual approach for a file named 'myheader.ijs' would be:
 
<syntaxhighlight lang ="j">require 'myheader.ijs'</langsyntaxhighlight>
 
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:
 
<syntaxhighlight lang ="j">load 'myheader.ijs'</langsyntaxhighlight>
 
That said, the raw mechanism here would be <syntaxhighlight lang="j">0!:0<'myheader.ijs'</syntaxhighlight> and this form might be used in the unusual circumstance where the file <tt>myheader.ijs</tt> defined local variables and the scope of those local names was meant be larger than the context of the <code>load</code> verb.
 
=={{header|Java}}==
Line 1,502 ⟶ 1,624:
 
Just this would be enough.
<langsyntaxhighlight Javalang="java">public class Class1 extends Class2
{
//code here
}</langsyntaxhighlight>
 
You could also consider creating an instance of Class2 within Class1, and then using the instance methods.
<langsyntaxhighlight Javalang="java">public class Class1
{
Class2 c2=new Class2();
Line 1,516 ⟶ 1,638:
c2.func2();
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 1,522 ⟶ 1,644:
===Pure JavaScript in browsers with the DOM===
Following example, if loaded in an HTML file, loads the [http://jquery.com/ jQuery] library from a remote site
<langsyntaxhighlight lang="javascript">var s = document.createElement('script');
s.type = 'application/javascript';
 
// path to the desired file
s.src = 'http://code.jquery.com/jquery-1.6.2.js';
document.body.appendChild(s);</langsyntaxhighlight>
Most be noted that it can also request [[HTTP]] source and eval() the source
 
===With jQuery===
{{libheader|jQuery}}
<langsyntaxhighlight lang="javascript">$.getScript("http://example.com/script.js");</langsyntaxhighlight>
 
===With AMD (require.js)===
<langsyntaxhighlight lang="javascript">require(["jquery"], function($) { /* ... */ });</langsyntaxhighlight>
 
===CommonJS style with node.js (or browserify)===
{{libheader|node.js}}
<langsyntaxhighlight lang="javascript">var $ = require('$');</langsyntaxhighlight>
 
===ES6 Modules===
<langsyntaxhighlight lang="javascript">import $ from "jquery";</langsyntaxhighlight>
 
=={{header|jq}}==
Line 1,556 ⟶ 1,678:
 
'''Include_a_file.jq'''
<langsyntaxhighlight lang="jq">include "gort";
 
hello</langsyntaxhighlight>
 
'''gort.jq'''
<syntaxhighlight lang="text">def hello: "Klaatu barada nikto";</langsyntaxhighlight>
 
{{ out }}
<langsyntaxhighlight lang="sh">$ jq -n -c -f Include_a_file.jq
Klaatu barada nikto.</langsyntaxhighlight>
 
=={{header|Jsish}}==
''jsish'' can include other source via '''System.source('filename');'''. Versioned moduled can be included via '''System.require('module', version);'''. Methods in the ''System'' object are automatically exported as top level globals (and the module version argument defaults to 1), so those can be shortened to
 
<langsyntaxhighlight lang="javascript">source('file');
require('module');</langsyntaxhighlight>
 
Compiled code can also be included via '''System.load('shlib');''', but that feature requires a known named init function, Jsi_Init[shlib] to be an exported symbol in the Dynamic Shared Object file.
Line 1,577 ⟶ 1,699:
=={{header|Julia}}==
Julia's <code>include</code> function executes code from an arbitrary file:
<langsyntaxhighlight Julialang="julia">include("foo.jl")</langsyntaxhighlight>
or alternatively <code>include_string</code> executes code in a string as if it were a file (and can optionally accept a filename to use in error messages etcetera).
 
Julia also has a module system:
<syntaxhighlight lang Julia="julia">import MyModule</langsyntaxhighlight>
imports the content of the module <code>MyModule.jl</code> (which should be of the form <code>module MyModule ... end</code>, whose symbols can be accessed as <code>MyModule.variable</code>, or alternatively
<syntaxhighlight lang Julia="julia">using MyModule</langsyntaxhighlight>
will import the module and all of its exported symbols
 
Line 1,592 ⟶ 1,714:
 
For example:
<langsyntaxhighlight lang="scala">fun f() = println("f called")</langsyntaxhighlight>
 
We can now import and invoke this from code in the default package as follows:
 
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import package1.f // import f from package `package1`
Line 1,602 ⟶ 1,724:
fun main(args: Array<String>) {
f() // invoke f without qualification
}</langsyntaxhighlight>
 
{{out}}
Line 1,611 ⟶ 1,733:
=={{header|LabVIEW}}==
In LabVIEW, any VI can be used as a "SubVI" by changing the icon and wiring the terminals to the front panel. This cannot be explained concisely in code; instead, see the [http://zone.ni.com/reference/en-XX/help/371361E-01/lvconcepts/creating_subvis/ documentation]. =={{header|LabVIEW}}==
<langsyntaxhighlight Lassolang="lasso">web_response -> include('my_file.inc')</langsyntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
# Execute and copy variables defined in code.lang only
ln.bindLibrary(code.lang)
 
# Execute and copy translations defined in code.lang only
ln.link(code.lang)
 
# Execute and copy variables and translations defined in code.lang
ln.include(code.lang)
</syntaxhighlight>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">include('myfile.lasso')</langsyntaxhighlight>
 
=={{header|Lingo}}==
 
<langsyntaxhighlight lang="lingo">-- load Lingo code from file
fp = xtra("fileIO").new()
fp.openFile(_movie.path&"someinclude.ls", 1)
Line 1,630 ⟶ 1,764:
 
-- use it instantly in the current script (i.e. the script that contained the above include code)
script("someinclude").foo()</langsyntaxhighlight>
 
=={{header|Logtalk}}==
<langsyntaxhighlight lang="logtalk">
:- object(foo).
 
Line 1,639 ⟶ 1,773:
 
:- end_object.
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
Line 1,645 ⟶ 1,779:
To include a header file myheader.lua:
 
<langsyntaxhighlight lang="lua"> require "myheader" </langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Without use of New in Load we get any cached file with same name. Using load from M2000 command line we always load file, but from code interpreter use a cache to hold it for next load.
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Document A$={
Module Global Beta {
Line 1,673 ⟶ 1,807:
\\ now Beta erased (after return form Checkit)
Print Module(Beta)=False
</syntaxhighlight>
</lang>
 
Running code of a module, as code is inline and not in that module. Now X is a variable in CheckIt
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
\\ we can delete global
Module Global alfa {
Line 1,687 ⟶ 1,821:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|m4}}==
 
<syntaxhighlight lang ="m4">include(filename)</langsyntaxhighlight>
 
=={{header|Maple}}==
For textual inclusion, analogous to the C preprocessor, use the "$include" preprocessor directive. (The preprocessor is not a separate program, however.) This is frequently useful for large project development.
<syntaxhighlight lang Maple="maple">$include <somefile></langsyntaxhighlight>
Or
<langsyntaxhighlight Maplelang="maple">$include "somefile"</langsyntaxhighlight>
It is also possible to read a file, using the "read" statement. This has rather different semantics.
<langsyntaxhighlight Maplelang="maple">read "somefile":</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
 
 
<langsyntaxhighlight Mathematicalang="mathematica"> Get["myfile.m"] </langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
Line 1,710 ⟶ 1,844:
New functions can be included, either by storing a new function in an existing path, or by extending the existing path to a new directory. When two functions have the same name, the function found first in the path takes precedence. The later is shown here:
 
<langsyntaxhighlight MATLABlang="matlab"> % add a new directory at the end of the path
path(path,newdir);
addpath(newdir,'-end'); % same as before
Line 1,716 ⟶ 1,850:
% add a new directory at the beginning
addpath(newdir);
path(newdir,path); % same as before</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">load("c:/.../source.mac")$
 
/* or if source.mac is in Maxima search path (see ??file_search_maxima), simply */
load(source)$</langsyntaxhighlight>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">IMPORT InOut, NumConv, Strings;</langsyntaxhighlight>
 
=={{header|Modula-3}}==
<langsyntaxhighlight lang="modula3">IMPORT IO, Text AS Str;
FROM Str IMPORT T</langsyntaxhighlight>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight Nanoquerylang="nanoquery">import "filename.nq"</langsyntaxhighlight>
 
=={{header|Nemerle}}==
To include classes, static methods etc. from other namespaces, include those namespaces with the <tt>using</tt> keyword
<syntaxhighlight lang Nemerle="nemerle">using System.Console;</langsyntaxhighlight>
<tt>using</tt> is for accessing code that has already been compiled into libraries. Nemerle also allows for creating
<tt>partial</tt> classes (and structs), the source code of which may be split amongst several files as long as the class is
marked as <tt>partial</tt> in each place that part of it is defined. An interesting feature of partial classes in
Nemerle is that some parts of partial classes may be written in C# while others are written in Nemerle.
<langsyntaxhighlight Nemerlelang="nemerle">public partial class Foo : Bar // all parts of a partial class must have same access modifier;
{ // the class that a partial class inherits from only needs to
... // be specified in one location
}</langsyntaxhighlight>
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">;; local file
(load "file.lsp")
 
;; URLs (both http:// and file:// URLs are supported)
(load "http://www.newlisp.org/code/modules/ftp.lsp")</langsyntaxhighlight>
 
=={{header|Nim}}==
As other modular languages, accessing from a module to symbols of other modules is done by importation.<br/>
After <code>import someModule</code> an exported symbol <code>x</code> can be accessed as <code>x</code> and as <code>someModule.x</code>.
<langsyntaxhighlight lang="nim">import someModule
import strutils except parseInt
import strutils as su, sequtils as qu # su.x works
import pure/os, "pure/times" # still strutils.x</langsyntaxhighlight>
 
But Nim provides also a way to include the content of a file, using the <code>include</code> statement:
<syntaxhighlight lang Nim="nim">include someFile</langsyntaxhighlight>
 
The import statement is only allowed at top level whereas the include statement can be used at any level as it simply includes the text (at the appropriate indentation level). Here is an example from the Nim manual:
<langsyntaxhighlight Nimlang="nim"># Module A
echo "Hello World!"</langsyntaxhighlight>
<br/>
<langsyntaxhighlight Nimlang="nim"># Module B
proc main() =
include A
 
main() # => Hello World!</langsyntaxhighlight>
 
=={{header|OASYS Assembler}}==
Line 1,780 ⟶ 1,914:
 
In script mode and in the interactive loop (the toplevel) we can use:
<langsyntaxhighlight lang="ocaml">#use "some_file.ml"</langsyntaxhighlight>
 
In compile mode (compiled to bytecode or compiled to native code) we can use:
<syntaxhighlight lang ="ocaml">include Name_of_a_module</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
In order to load a file with name filename :
<langsyntaxhighlight Oforthlang="oforth">"filename" load</langsyntaxhighlight>
 
In order to load a package with name pack :
<syntaxhighlight lang Oforth="oforth">import: pack</langsyntaxhighlight>
 
=={{header|Ol}}==
Ol has a module system, so usually there is no need of a textual inclusion of a text file.
<langsyntaxhighlight lang="scheme">
(import (otus random!))
</syntaxhighlight>
</lang>
 
You can do a textual inclusion from the global scope using REPL command ",load" (not a part of core language itself, but a REPL extension).
<langsyntaxhighlight lang="scheme">
,load "otus/random!.scm"
</syntaxhighlight>
</lang>
 
=={{header|ooRexx}}==
ooRexx has a package system and no ability for textual inclusion of other text files. Importing of other packages is done via the ::requires directive.
<langsyntaxhighlight ooRexxlang="oorexx"> ::requires "regex.cls"</langsyntaxhighlight>
 
=={{header|OpenEdge/Progress}}==
Curly braces indicate that a file should be included. The file is searched across all PROPATH directory entries.
<syntaxhighlight lang ="progress">{file.i}</langsyntaxhighlight>
 
Arguments can be passed to the file being included:
 
<syntaxhighlight lang ="progress">{file.i super}</langsyntaxhighlight>
 
=={{header|Openscad}}==
 
<langsyntaxhighlight lang="openscad">//Include and run the file foo.scad
include <foo.scad>;
 
//Import modules and functions, but do not execute
use <bar.scad>;</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Line 1,838 ⟶ 1,972:
main.perl:
 
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
do "include.pl"; # Utilize source from another file
sayhello();</langsyntaxhighlight>
 
include.pl:
<langsyntaxhighlight lang="perl">sub sayhello {
print "Hello World!";
}</langsyntaxhighlight>
 
From documentation:<pre>
Line 1,855 ⟶ 1,989:
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
<lang Phix>include arwen.ew</lang>
<!--<syntaxhighlight lang="phix">-->
Phix also supports relative directory includes, for instance if you include "..\demo\arwen\arwen.ew" then anything that arwen.ew includes will be looked for in the appropriate directory.
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<!--</syntaxhighlight>-->
Phix also supports relative directory includes, for instance if you include "..\demo\pGUI\demo.ew" then anything demo.ew includes is looked for in the same location.
 
The following remarks have been copied verbatim from the [[Compiler/Simple_file_inclusion_pre_processor#Phix]] entry:<br>
Phix ships with a bunch of standard files in a builtins directory, most of which it knows how to "autoinclude", but some must be explicitly included ([http://phix.x10.mx/docs/html/include.htm full docs]). You can explicitly specify the builtins directory or not (obviously without it will look in the project directory first), and use the same mechanism for files you have written yourself. There is no limit to the number or depth of files than can be included. Relative directories are honoured, so if you specify a (partial) directory that is where it will look first for any sub-includes. You can also use single line "stub includes" to redirect include statements to different directories/versions. Note that namespaces are not supported by pwa/p2js. You can optionally use double quotes, but may then need to escape backslashes. Includes occur at compile time, as opposed to dynamically.
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #004080;">complex</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">complex</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- also valid</span>
<span style="color: #008080;">include</span> <span style="color: #008000;">"builtins\\complex.e"</span> <span style="color: #000080;font-style:italic;">-- ditto</span>
<!--</syntaxhighlight>-->
If the compiler detects that some file has already been included it does not do it again (from the same directory, two or more files of the same name can be included from different directories). I should perhaps also state that include handling is part of normal compilation/interpretation, as opposed to a separate "preprocessing" step, and that each file is granted a new private scope, and while of course there is only one "global" scope, it will use the implicit include hierarchy to automatically resolve any clashes that might arise to the most appropriate one, aka "if it works standalone it should work exactly the same when included in as part of a larger application".
 
=={{header|PHP}}==
There are different ways to do this in PHP. You can use a basic include:
<langsyntaxhighlight PHPlang="php">include("file.php")</langsyntaxhighlight>
You can be safe about it and make sure it's not included more than once:
<langsyntaxhighlight PHPlang="php">include_once("file.php")</langsyntaxhighlight>
You can crash the code at this point if the include fails for any reason by using require:
<langsyntaxhighlight PHPlang="php">require("file.php")</langsyntaxhighlight>
And you can use the require statement, with the safe _once method:
<langsyntaxhighlight PHPlang="php">require_once("file.php")</langsyntaxhighlight>
 
=={{header|Picat}}==
===cl/1===
<code>cl/1</code> compiles a Picat program to a byte code file (<code>.qi</code>) and load that.
<syntaxhighlight lang="picat">cl("include_file.pi")</syntaxhighlight>
 
The extension (<code>.pi</code>) can be omitted:
<syntaxhighlight lang="picat">cl(include_file)</syntaxhighlight>
 
===load/1===
<code>load/1</code> loads a byte code file. If the byte code files does not exist, the program is first compiled.
<syntaxhighlight lang="picat">load(include_file)</syntaxhighlight>
 
===import===
A Picat module is loaded with <code>import module</code>, which must be placed before any other definitions in the program.
<syntaxhighlight lang="picat">import cp, util.</syntaxhighlight>
 
 
=={{header|PicoLisp}}==
The function '[http://software-lab.de/doc/refL.html#load load]' is used for recursively executing the contents of files.
<langsyntaxhighlight PicoLisplang="picolisp">(load "file1.l" "file2.l" "file3.l")</langsyntaxhighlight>
 
=={{header|Pike}}==
Line 1,880 ⟶ 2,043:
compile() and compile_file() functions can be used.
 
<langsyntaxhighlight Pikelang="pike">#include "foo.txt"</langsyntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang ="pli">%include myfile;</langsyntaxhighlight>
 
=={{header|PL/M}}==
The original PL/M compiler did not have file inclusion, however PL/M 386 and possibly other versions, had a "$INCLUDE" compiler control statement. The "$" had to appear in the first column. Nesting of include files was possible, to a depth of 5 (in PL/M 386).
<syntaxhighlight lang="plm">$INCLUDE (fileName.inc)</syntaxhighlight>
 
=={{header|PowerBASIC}}==
Line 1,890 ⟶ 2,057:
Note also that <code>#INCLUDE</code> and <code>$INCLUDE</code> function identically.
 
<langsyntaxhighlight lang="powerbasic">#INCLUDE "Win32API.inc"
#INCLUDE ONCE "Win32API.inc"</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
<#
A module is a set of related Windows PowerShell functionalities, grouped together as a convenient unit (usually saved in a
Line 1,909 ⟶ 2,076:
#>
. .\MyFunctions.ps1
</syntaxhighlight>
</lang>
 
=={{header|Processing}}==
Line 1,930 ⟶ 2,097:
Processing also supports the `import` keyword for explicitly including Processing / Java 8 library files. This can be used to import standard part of Java 8, for example the Map class:
 
<syntaxhighlight lang ="processing">import java.util.Map;</langsyntaxhighlight>
 
It can also be used to import contributed libraries, which may be installed via PDE Contributions Manager or locally on the Processing Libraries path. For example, if the G4P library is installed, its files are then imported into a sketch with:
 
<syntaxhighlight lang ="processing">import g4p_controls.*;</langsyntaxhighlight>
 
3. Local Java JAR files may be added to the sketch /code subfolder, then imported with `import`. For example, if you have a file code/test.jar:
Line 1,945 ⟶ 2,112:
...and it contains a Java class file `foo/bar/Test.class`, then that can be imported from the file. The specific jar file name does _not_ need to be identified -- any resource in any jar file can be imported so long as you specify the in-jar path and class name in the import statement.
 
<syntaxhighlight lang ="processing">import foo.bar.Test;</langsyntaxhighlight>
 
==={{header|Processing Python mode}}===
Line 1,961 ⟶ 2,128:
One can write:
 
<langsyntaxhighlight lang="python">import one # note there is no .py sufix
# then you may use
# one.one_function()
# object = one.OneClass()</langsyntaxhighlight>
 
otherwise use
 
<syntaxhighlight lang ="python">from one import *</langsyntaxhighlight>
 
or, recommended style:
 
<langsyntaxhighlight lang="python">from one import OneClass, one_function
# then you may use
# one_function()
# object = OneClass()</langsyntaxhighlight>
 
=={{header|Prolog}}==
 
<syntaxhighlight lang Prolog="prolog">consult('filename').</langsyntaxhighlight>
 
=={{header|PureBasic}}==
IncludeFile will include the named source file at the current place in the code.
<langsyntaxhighlight PureBasiclang="purebasic">IncludeFile "Filename"</langsyntaxhighlight>
XIncludeFile is exactly the same except it avoids including the same file several times.
<langsyntaxhighlight PureBasiclang="purebasic">XIncludeFile "Filename"</langsyntaxhighlight>
 
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 [http://www.purebasic.com/documentation/reference/data.html data block].
<langsyntaxhighlight PureBasiclang="purebasic">IncludeBinary "Filename"</langsyntaxhighlight>
 
=={{header|Python}}==
Python supports the use of [http://docs.python.org/library/functions.html#execfile execfile] to allow code from arbitrary files to be executed from a program (without using its modules system).
 
<syntaxhighlight lang Python="python">import mymodule</langsyntaxhighlight>
 
includes the content of mymodule.py
Line 2,000 ⟶ 2,167:
Names in this module can be accessed as attributes:
<syntaxhighlight lang Python="python">mymodule.variable</langsyntaxhighlight>
=={{header|QB64}}==
 
<syntaxhighlight lang="vb">'$INCLUDE:'filename.bas'</syntaxhighlight>
 
=={{header|Quackery}}==
 
To load and compile a file called <code>myfile.qky</code>:
 
<syntaxhighlight lang="quackery">$ "myfile.qky" loadfile</syntaxhighlight>
 
To load and compile a file called <code>myfile.qky</code> whilst loading and compiling another file:
 
<syntaxhighlight lang="quackery">[ $ "myfile.qky" loadfile ] now!</syntaxhighlight>
 
To prevent a file called <code>myfile.qky</code> from being loaded and compiled more than once during a Quackery session (e.g. a library module that may be invoked during compilation of several other files) define a word called <code>myfile.qky</code> in the file <code>myfile.qky</code>. By convention, use this definition as the first line of the file:
 
<syntaxhighlight lang="quackery">[ this ] is myfile.qky</syntaxhighlight>
 
=={{header|R}}==
 
<langsyntaxhighlight Rlang="r">source("filename.R")</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 2,010 ⟶ 2,194:
Including files is usually discouraged in favor of using modules, but it is still possible:
 
<langsyntaxhighlight lang="racket">
#lang racket
(include "other-file.rkt")
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 2,020 ⟶ 2,204:
Raku provides a module system that is based primarily on importation of symbols rather than
on inclusion of textual code:
<syntaxhighlight lang="raku" perl6line>use MyModule;</langsyntaxhighlight>
However, one can evaluate code from a file:
<syntaxhighlight lang="raku" perl6line>require 'myfile.p6';</langsyntaxhighlight>
One can even do that at compile time:
<syntaxhighlight lang="raku" perl6line>BEGIN require 'myfile.p6'</langsyntaxhighlight>
None of these are true inclusion, unless the <tt>require</tt> cheats and modifies the current input string of the parser. To get a true textual inclusion, one could define an unhygienic textual macro like this:
<syntaxhighlight lang="raku" perl6line>macro include(AST $file) { slurp $file.eval }
include('myfile.p6');</langsyntaxhighlight>
 
=={{header|RapidQ}}==
<syntaxhighlight lang="vb">
<lang vb>
$INCLUDE "RAPIDQ.INC"
</syntaxhighlight>
</lang>
 
=={{header|Retro}}==
 
<syntaxhighlight lang Retro="retro">'filename include</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 2,046 ⟶ 2,230:
 
The REXX Compiler for CMS and TSO supports a directive to include program text before compiling the program
<syntaxhighlight lang ="rexx">/*%INCLUDE member */</langsyntaxhighlight>
 
===Including a file at time of execution===
Line 2,052 ⟶ 2,236:
{{works with|ARexx}}
{{works with|REGINA 3.8 and later, with options: AREXX_BIFS and AREXX_SEMANTICS}}
<langsyntaxhighlight lang="rexx">
/* Include a file and INTERPRET it; this code uses ARexx file IO BIFs */
say 'This is a program running.'
Line 2,067 ⟶ 2,251:
say 'The usual program resumes here.'
exit 0
</syntaxhighlight>
</lang>
Note: &nbsp; due to the way most REXX interpreters work, functions and jumps (SIGNALs) inside an INTERPRETED program won't work. &nbsp; Neither are &nbsp; ''labels'' &nbsp; recognized, which would then exclude the use of those subroutines/functions.
 
Line 2,078 ⟶ 2,262:
 
'''Program1.rexx'''
<langsyntaxhighlight lang="rexx">
/* This is program 1 */
say 'This is program 1 writing on standard output.'
Line 2,084 ⟶ 2,268:
say 'Thank you, program 1 is now ending.'
exit 0
</syntaxhighlight>
</lang>
'''Program2.rexx'''
<langsyntaxhighlight lang="rexx">
/* This is program 2 */
say 'This is program 2 writing on standard output.'
say 'We now return to the caller.'
return
</syntaxhighlight>
</lang>
If a REXX interpreter finds a function call, it first looks in the current program for a function or procedure by that name, then it looks in the standard function library (so you may replace the standard functions with your own versions inside a program), then it looks for a program by the same name in the standard paths. This means that including a file in your program is usually not necessary, unless you want them to share global variables.
 
=={{header|Ring}}==
<langsyntaxhighlight Ringlang="ring">Load 'file.ring'</langsyntaxhighlight>
 
=={{header|RPG}}==
Line 2,101 ⟶ 2,285:
{{works with|ILE RPG}}
 
<langsyntaxhighlight lang="rpg"> // fully qualified syntax:
/include library/file,member
 
Line 2,113 ⟶ 2,297:
/copy library/file,member
 
//... farther like "include"</langsyntaxhighlight>
 
=={{header|Ruby}}==
Note that in Ruby, you don't use the file extension. Ruby will first check for a Ruby (.rb) file of the specified name and load it as a source file. If an .rb file is not found it will search for files in .so, .o, .dll or other shared-library formats and load them as Ruby extension. <code>require</code> will search in a series of pre-determined folders, while <code>require_relative</code> behaves the same way but searches in the current folder, or another specified folder.
 
<syntaxhighlight lang Ruby="ruby">require 'file'</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
You don't use the file extension. .bas is assumed.
<langsyntaxhighlight lang="runbasic">run SomeProgram.bas",#include ' this gives it a handle of #include
render #include ' render will RUN the program with handle #include</langsyntaxhighlight>
 
=={{header|Rust}}==
The compiler will include either a 'test.rs' or a 'test/mod.rs' (if the first one doesn't exist) file.
<langsyntaxhighlight lang="rust">mod test;
 
fn main() {
test::some_function();
}</langsyntaxhighlight>
 
Additionally, third-party libraries (called <code>crates</code> in rust) can be declared thusly:
<langsyntaxhighlight lang="rust">extern crate foo;
fn main() {
foo::some_function();
}</langsyntaxhighlight>
 
=={{header|Scala}}==
Line 2,143 ⟶ 2,327:
 
In a Scala REPL[https://docs.scala-lang.org/overviews/repl/overview.html] it's possible to save and load source code.
=={{header|Scheme}}==
<syntaxhighlight lang="scheme">(load "filename")</syntaxhighlight>
 
=={{header|Seed7}}==
Line 2,148 ⟶ 2,334:
Therefore seed7_05.s7i must be included before other language features can be used (only comments can be used before).
The first include directive (the one which includes seed7_05.s7i) is special and it must be introduced with the $ character.
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";</langsyntaxhighlight>
All following include directives don't need a $ to introduce them.
The [http://seed7.sourceforge.net/libraries/float.htm float.s7i]
[http://seed7.sourceforge.net/libraries library] can be included with:
<langsyntaxhighlight lang="seed7"> include "float.s7i";</langsyntaxhighlight>
 
=={{header|Sidef}}==
Include a file in the current namespace:
<syntaxhighlight lang ="ruby">include 'file.sf';</langsyntaxhighlight>
 
Include a file as module (file must exists in '''SIDEF_INC''' as '''Some/Name.sm'''):
<langsyntaxhighlight lang="ruby">include Some::Name;
# variables are available here as: Some::Name::var_name</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
there is no such thing as source-file inclusion in Smalltalk. However, in a REPL or anywhere in code, source code can be loaded with:
<syntaxhighlight lang ="smalltalk">aFilename asFilename readStream fileIn</langsyntaxhighlight>
or:
<syntaxhighlight lang ="smalltalk">Smalltalk fileIn: aFilename</langsyntaxhighlight>
In Smalltalk/X, which supports binary code loading, aFilename may either be sourcecode or a dll containing a precompiled class library.
 
Line 2,172 ⟶ 2,358:
{{Works with|SNOBOL4}}
{{Works with|Spitbol}}
<langsyntaxhighlight SNOBOL4lang="snobol4">-INCLUDE "path/to/filename.inc"</langsyntaxhighlight>
 
=={{header|SPL}}==
<syntaxhighlight lang ="spl">$include.txt</langsyntaxhighlight>
 
=={{header|Standard ML}}==
{{Works with|SML/NJ}}
<langsyntaxhighlight lang="sml">use "path/to/file";</langsyntaxhighlight>
 
=={{header|Tcl}}==
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>):
<langsyntaxhighlight lang="tcl">source "foobar.tcl"</langsyntaxhighlight>
 
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.)
<syntaxhighlight lang ="tcl">package require foobar 1.3</langsyntaxhighlight>
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.
 
Line 2,194 ⟶ 2,380:
 
{{works with|Bourne Shell}}
<langsyntaxhighlight lang="bash">. myfile.sh # Include the contents of myfile.sh </langsyntaxhighlight>
 
==={{header|C Shell}}===
<syntaxhighlight lang ="csh">source myfile.csh</langsyntaxhighlight>
 
==={{header|Bash}}===
<langsyntaxhighlight lang="shell">. myfile.sh
source myfile.sh</langsyntaxhighlight>
 
GNU Bash has both <code>.</code> and the C-Shell style <code>source</code>. See [http://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-source Bash manual on <code>source</code>]
Line 2,207 ⟶ 2,393:
=={{header|Ursa}}==
Ursa can read in and execute another file using the import statement, similar to Python.
<langsyntaxhighlight lang="ursa">import "filename.u"</langsyntaxhighlight>
 
=={{header|Vala}}==
Line 2,216 ⟶ 2,402:
 
Functions can be called then using Gee.<function> calls:
<langsyntaxhighlight lang="vala">var map = new Gee.HashMap<string, int> ();</langsyntaxhighlight>
 
or with a using statement:
<langsyntaxhighlight lang="vala">using Gee;
 
var map = new HashMap<string, int>();</langsyntaxhighlight>
 
=={{header|VBScript}}==
VBScript doesn't come with an explicit include (unless you use the wsf form). Fortunately vbscript has the Execute and ExecuteGlobal commands which allow you to add code dynamically into the local (disappears when the code goes out of scope) or global namespaces. Thus, all you have to do to include code from a file is read the file into memory and ExecuteGlobal on that code. Just pass the filename to this sub and all is golden. If you want an error to occur if the file is not found then just remove the FileExists test.
 
<syntaxhighlight lang="vb">
<lang vb>
Include "D:\include\pad.vbs"
 
Line 2,235 ⟶ 2,421:
if fso.FileExists(file) then ExecuteGlobal fso.OpenTextFile(file).ReadAll
End Sub
</syntaxhighlight>
</lang>
If you use the wsf form you can include a file by
<langsyntaxhighlight lang="vbscript">
<script id="Connections" language="VBScript" src="D:\include\ConnectionStrings.vbs"/>
 
</syntaxhighlight>
</lang>
 
If you use the following form then you can define an environment variable, %INCLUDE% and make your include library more portable as in
 
<langsyntaxhighlight lang="vbscript">
Include "%INCLUDE%\StrFuncs.vbs"
 
Line 2,252 ⟶ 2,438:
ExecuteGlobal(fso.OpenTextFile(wso.ExpandEnvironmentStrings(file)).ReadAll)
End Function
</syntaxhighlight>
</lang>
 
=={{header|Verbexx}}==
<langsyntaxhighlight lang="verbexx">/*******************************************************************************
* /# @INCLUDE file:"filename.filetype"
* - file: is just the filename
Line 2,287 ⟶ 2,473:
Not in preprocessor -- include_counter = 3
Not in preprocessor -- include_counter = 3
Not in preprocessor -- include_counter = 3</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 2,303 ⟶ 2,489:
 
Here's a simple example.
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt // imports the Fmt module and makes the 'Fmt' class available
import "./math" for Int Math // imports the Math module and makes the 'IntMath' class available
 
System.print("The maximum safe integer in Wren is %(Fmt.dc(0, Int.maxSafe)).")</lang>
Fmt.print("The value of 'e' to 6 d.p. is $,f.", Math.e)</syntaxhighlight>
 
{{out}}
<pre>
The maximumvalue safeof integer'e' into Wren6 d.p. is 9,007,199,254,740,9912.718282.
</pre>
 
Line 2,315 ⟶ 2,502:
 
{{works with|FASM on Windows}}
<syntaxhighlight lang ="asm">include 'MyFile.INC'</langsyntaxhighlight>
 
{{works with|nasm}}
<langsyntaxhighlight lang="asm">%include "MyFile.INC"</langsyntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\stdlib;
DateOut(0, GetDate)</langsyntaxhighlight>
 
{{out}}
Line 2,328 ⟶ 2,515:
09-28-12
</pre>
 
=={{header|Z80 Assembly}}==
There are two different directives for including files: <code>include</code> and <code>incbin</code>.
* <code>include</code> is for assembly code that will be converted to machine code that the computer can run.
* <code>incbin</code> is for binary data such as graphics, which the assembler will convert as-is to binary and does not attempt to translate it into machine code.
 
It's important to note that, unlike in high-level languages, WHERE your include/incbin statements are located relative to your other code matters. Placing it incorrectly can cause execution to "fall into" your included files when you didn't intend it to happen.
 
 
What ''not'' to do:
<syntaxhighlight lang="z80">org &1000
include "\source\mathLibrary.asm"
main:
ld a,&42
ret</syntaxhighlight>
 
In assembly, the order you arrange your code is typically the same as its layout in memory, unless you're using a linker. In the above example, execution will begin at the ORG directive and "fall through" into your math Library, rather than starting at main. Unless your program has a header that jumps to main (something that [[C]] generates for you automatically), you're going to have some undesired effects if you don't "trap" the program counter.
 
The right way:
<syntaxhighlight lang="z80">org &1000
main:
ld a,&42
ret
include "\source\mathLibrary.asm"</syntaxhighlight>
 
You'll have to take this with a grain of salt, as it's hardware-dependent (the above was for Amstrad CPC or other similar home computers where your program is CALLed from BASIC.) If you're defining your own ROM header for something like Game Boy things will be a bit different.
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">include(vm.h.zkl, compiler.h.zkl, zkl.h.zkl, opcode.h.zkl);</langsyntaxhighlight>
 
=={{header|ZX Spectrum Basic}}==
Line 2,336 ⟶ 2,549:
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:
 
<langsyntaxhighlight lang="zxbasic">10 GO TO 9950
5000 REM We reserve line numbers 5000 to 8999 for merged code
9000 STOP: REM In case our line numbers are wrong
Line 2,342 ⟶ 2,555:
9955 MERGE "MODULE"
9960 REM Jump to the merged code. Pray it has the right line numbers!
9965 GO TO 5000 </langsyntaxhighlight>
 
 
9,476

edits