Include a file: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 88: Line 88:


In gambas, files sre added to the project via the project explorer main window which is a component of the integrated development environment.
In gambas, files sre added to the project via the project explorer main window which is a component of the integrated development environment.

=={{header|GAP}}==
<lang gap>Read("file");</lang>


=={{header|Haskell}}==
=={{header|Haskell}}==

Revision as of 19:08, 28 April 2012

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 demonstrate the language's ability to include source code from other files.

Ada

<lang Ada>with Ada.Text_IO, Another_Package; use Ada.Text_IO;

 -- the with-clause tells the complier to include the Text_IO package from the Ada standard 
 -- and Another_Package. Subprograms from these packages may be called as follows: 
 --               Ada.Text_IO.Put_Line("some text");
 --               Another_Package.Do_Something("some text"); 
 -- The use-clause allows the program author to write a subprogram call shortly as 
 --               Put_Line("some text");</lang>

AutoHotkey

<lang AutoHotkey>

  1. Include FileOrDirName
  2. IncludeAgain FileOrDirName

</lang>

AWK

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:

<lang sh>awk -f one.awk -f two.awk</lang>

It is not permissible to pass the name of additional source files through a hashbang line, so the following will will not work:

#!/usr/bin/awk -f one.awk -f two.awk

Bracmat

get$"module"

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>

C#

<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.
*/

</lang>

D

D has a module system, so usually there is no need of a textual inclusion of a text file: <lang d>import std.stdio;</lang>

To perform a textual inclusion: <lang d>mixin(import("code.txt"));</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>

DWScript

In addition to straight inclusion, there is a filtered inclusion, in which the include file goes through a pre-processing filter. <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 {$INCLUDE_ONCE Common} // Inserts the contents of Common.pas into the current unit only if not included already {$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 </lang>

Erlang

<lang Erlang> -include("my_header.hrl"). % Includes the file at my_header.erl </lang>

Forth

<lang forth>include matrix.fs</lang>

Other Forth systems have a smarter word, which protects against multiple inclusion. The name varies: USES, REQUIRE, NEEDS.

Fortran

<lang Fortran>include char-literal-constant</lang>

"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." See section 3.4 Including source text of the ISO standard working draft (Fortran 2003).

Gambas

In gambas, files sre added to the project via the project explorer main window which is a component of the integrated development environment.

GAP

<lang gap>Read("file");</lang>

Haskell

<lang Haskell>-- Due to Haskell's module system, textual includes are rarely needed. In -- general, one will import a module, like so: import SomeModule -- For actual textual inclusion, alternate methods are available. The Glasgow -- Haskell Compiler runs the C preprocessor on source code, so #include may be -- used:

  1. include "SomeModule.hs"</lang>

HTML

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:

<lang html><iframe src="foobar.html"> Sorry: Your browser cannot show the included content.</iframe></lang>

Icon and Unicon

Include another file of source code using the preprocessor statement: <lang Icon>$include "filename.icn"</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>

JavaScript

Following example, if loaded in an HTML file, loads the jQuery library from a remote site <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);</lang>

With jQuery

Library: jQuery

<lang javascript>$.getScript("http://example.com/script.js");</lang>

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 documentation.

Lua

To include a header file myheader.lua:

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

Mathematica

<lang Mathematica> Get["myfile.m"] </lang>

Matlab / Octave

Matlab and Octave look for functions in *.m and *.mex included in the "path". 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:

<lang MATLAB>

 % add a new directory at the end of the path
 path(path,newdir);  
 addpath(newdir,'-end');  % same as before
 % add a new directory at the beginning
 addpath(newdir);
 path(newdir,path);       % same as before

</lang>

Modula-2

<lang modula2>IMPORT InOut, NumConv, Strings;</lang>

OCaml

In script mode and in the interactive loop (the toplevel) we can use: <lang ocaml>#use "some_file.ml"</lang>

In compile mode (compiled to bytecode or compiled to native code) we can use: <lang ocaml>include Name_of_a_module</lang>

OpenEdge/Progress

Curly braces indicate that a file should be included. The file is searched across all PROPATH directory entries. <lang progress>{file.i}</lang>

Arguments can be passed to the file being included:

<lang progress>{file.i super}</lang>

Openscad

<lang openscad>//Include and run the file foo.scad include <foo.scad>;

//Import modules and functions, but do not execute use <bar.scad>;</lang>

PARI/GP

Files can be loaded in GP with the read, or directly in gp with the metacommand \r.

PARI can use the standard C #include, but note that if using gp2c the embedded GP; commands must be in the original file.

Pascal

See Delphi

Perl

Presumably this task is not about loading libraries or running programs in other languages. For loading another perl program inside perl, use <lang Perl>do "filename";</lang>

From documentation:

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.

PHP

There are different ways to do this in PHP. You can use a basic include: <lang PHP>include("file.php")</lang> You can be safe about it and make sure it's not included more than once: <lang PHP>include_once("file.php")</lang> You can crash the code at this point if the include fails for any reason by using require: <lang PHP>require("file.php")</lang> And you can use the require statement, with the safe _once method: <lang PHP>require_once("file.php")</lang>

PicoLisp

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

PL/I

<lang PL/I> %include myfile; </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).

<lang Python>import mymodule</lang>

includes the content of mymodule.py

Names in this module can be accessed as attributes:

<lang Python>mymodule.variable</lang>

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>

Retro

<lang Retro>include filename.ext</lang>

RPG

Works with: ILE RPG

<lang rpg>

     // fully qualified syntax:
     /include library/file,member
     // most sensible; file found on *libl:
     /include file,member
     // shortest one, the same library and file:
     /include member
     
     // and alternative:
     /copy library/file,member
     //... farther like "include" 

</lang>

Ruby

Note that in Ruby, you don't use the file extension. .rb is assumed. <lang Ruby>require 'file'</lang>

Run BASIC

You don't use the file extension. .bas is assumed. <lang runbasic>run SomeProgram.bas",#include ' this gives it a handle of #include render #include ' render will RUN the program with handle #include</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

With Bourne-compatible shells, the dot operator includes another file.

Works with: Bourne Shell

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

C Shell

<lang csh>source myfile.csh</lang>

Vala

Importing/including is done during compilation. For example, to compile the program called "maps.vala" with the package "gee":

valac maps.vala --pkg gee-1.0

Functions can be called then using Gee.<function> calls: <lang vala> var map = new Gee.HashMap<string, int> (); </lang>

or with a using statement: <lang vala> using Gee;

var map = new HashMap<string, int>(); </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 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 9950 REM Merge in our module 9955 MERGE "MODULE" 9960 REM Jump to the merged code. Pray it has the right line numbers! 9965 GO TO 5000 </lang>