Difference between revisions of "REQUIRE()"

From Lianjapedia
Jump to: navigation, search
(Example)
(See Also)
 
(6 intermediate revisions by 2 users not shown)
Line 6: Line 6:
  
 
==See Also==
 
==See Also==
[[CLOSE PROCEDURE]], [[DO]], [[FUNCTION]], [[INCLUDE()]], [[INCLUDE_ONCE()]], [[LIST PROCEDURE]], [[PARAMETERS]], [[PROCEDURE]], [[RELEASE LIBRARY]], [[REQUIRE_ONCE()]], [[SET LIBRARY]]
+
[[CLOSE PROCEDURE]], [[DO]], [[FUNCTION]], [[INCLUDE()]], [[INCLUDE_ONCE()]], [[LIST PROCEDURE]], [[PARAMETERS]], [[PROCEDURE]], [[RELEASE LIBRARY]], [[REQUIRE_ONCE()]], [[SET LIBRARY]], [[EXECPYTHON()]]
  
 
==Description==
 
==Description==
Line 33: Line 33:
 
// Close all active API procedure library files
 
// Close all active API procedure library files
 
set library to
 
set library to
</code>
 
  
 
+
// dynamically load a module
<code Lang=“recital”>
+
 
local mylib = require(“mylibrary.prg”)
 
local mylib = require(“mylibrary.prg”)
 
Mylib.name()
 
Mylib.name()
 
Mylib.var = “hello world”
 
Mylib.var = “hello world”
 
</code>
 
</code>
 +
 +
==LianjaScript Modules==
 +
 +
LianjaScript Modules are similar to libraries in that they can be dynamically loaded and contain procedures that may be used in your Apps.
 +
 +
Modules however encapsulate all procedures, functions and public variables and make these only visible from an object. This prevents name clashes and is a simple way to wrap code libraries into objects without any special coding syntax.
 +
 +
Modules provide simple (Object-Oriented Programming ) OOP concepts:
 +
 +
* Objects
 +
* Abstraction
 +
* Encapsulation
 +
* Inheritance
 +
* Polymorphism
 +
 +
e.g.
 +
 +
<code lang="recital">
 +
// file: mylib1.prg
 +
public myvar = "Barry"
 +
 +
proc getMyVar()
 +
return myvar
 +
</code>
 +
 +
You dynamically load a module using the require() built-in function:
 +
 +
<code lang="recital">
 +
public mylib = require("mylib1")
 +
</code>
 +
 +
You can now access the public variables and call procs like this:
 +
 +
<code lang="recital">
 +
value = mylib.myvar
 +
value = mylib.getMyVar()
 +
</code>
 +
 +
Modules also handle inheritance:
 +
 +
<code lang="recital">
 +
// file: mylib2.prg
 +
public myvar = "Bill"
 +
 +
proc getMyVar()
 +
return myvar
 +
</code>
 +
 +
Now dynamically load the modules:
 +
 +
<code lang="recital">
 +
public mylib = require("mylib1", "mylib2") // , "mylib3"... etc.
 +
</code>
 +
 +
You can now access the public variables and call procs like this:
 +
 +
<code lang="recital">
 +
value = mylib.myvar
 +
value = mylib.getMyVar()
 +
</code>
 +
 +
Modules can optionally contain an init() initialization proc which is called after the module is loaded:
 +
 +
<code lang="recital">
 +
// file: mylib3
 +
public myvar = "Bill"
 +
 +
proc init()
 +
    myvar = "Tom"
 +
endproc
 +
 +
proc getMyVar()
 +
return myvar
 +
</code>
 +
 +
Note that the filenames specified as arguments to the require() function may contain the special filename prefixes such as lib:/ and thirdparytlibs:/ etc.
  
 
[[Category:Documentation]]
 
[[Category:Documentation]]
Line 46: Line 120:
 
[[Category:C Extensions API]]
 
[[Category:C Extensions API]]
 
[[Category:Libraries]]
 
[[Category:Libraries]]
 +
[[Category:Lianja v6.3]]

Latest revision as of 01:49, 23 April 2023

Purpose

Load a library file built with the Lianja C Extensions API, a Lianja procedure library or a JavaScript (.js), PHP (.php) or Python (.py) file

Syntax

REQUIRE(<expC>)

See Also

CLOSE PROCEDURE, DO, FUNCTION, INCLUDE(), INCLUDE_ONCE(), LIST PROCEDURE, PARAMETERS, PROCEDURE, RELEASE LIBRARY, REQUIRE_ONCE(), SET LIBRARY, EXECPYTHON()

Description

The REQUIRE() function opens the specified, scans the contents of it, and records the names and positions of the procedures defined within it. The <expC> is the name of the library file, which can be a Lianja API or Lianja procedure library file or a JavaScript (.js), PHP (.php) or Python (.py) file. If the file extension is not stated, '.so' is assumed on Linux. To load a Lianja procedure or class library file, the '.prg' or '.dbo' file extension must be included. You can place as many procedures or classes as you want in a procedure library file.

The SET LIBRARY TO command, without any filename specified, closes all active C Extensions library files. A closed library file discards any knowledge of where the procedures within reside. The RELEASE LIBRARY <library filename> command can be used to close an individual C Extensions library file.

The SET PROCEDURE and CLOSE PROCEDURE commands can also be used to handle Lianja procedure library files.

The active procedures and functions can be listed with the LIST or DISPLAY PROCEDURE commands.

In Lianja 6 dynamically loadable modules are now supported.

This provides OO encapsulation for existing code so that loading a library does not "pollute" the namespace and create potential problems due to name clashes.

Use the require() function to dynamically load a library and reference its public variables and procedures/functions in an OO manner.

The filename specified as the argument to the require() function must exist in the Lianja path or be prefixed with a special prefix e.g lib:/ or thirdpartylibs:/

Example

// Open Samples.so API procedure library
require("Samples")
// Close pdf.so API procedure library
release library pdf.so
// Close all active API procedure library files
set library to
 
// dynamically load a module
local mylib = require(“mylibrary.prg)
Mylib.name()
Mylib.var = “hello world”

LianjaScript Modules

LianjaScript Modules are similar to libraries in that they can be dynamically loaded and contain procedures that may be used in your Apps.

Modules however encapsulate all procedures, functions and public variables and make these only visible from an object. This prevents name clashes and is a simple way to wrap code libraries into objects without any special coding syntax.

Modules provide simple (Object-Oriented Programming ) OOP concepts:

  • Objects
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

e.g.

// file: mylib1.prg
public myvar = "Barry"
 
proc getMyVar()
return myvar

You dynamically load a module using the require() built-in function:

public mylib = require("mylib1")

You can now access the public variables and call procs like this:

value = mylib.myvar
value = mylib.getMyVar()

Modules also handle inheritance:

// file: mylib2.prg
public myvar = "Bill"
 
proc getMyVar()
return myvar

Now dynamically load the modules:

public mylib = require("mylib1", "mylib2") // , "mylib3"... etc.

You can now access the public variables and call procs like this:

value = mylib.myvar
value = mylib.getMyVar()

Modules can optionally contain an init() initialization proc which is called after the module is loaded:

// file: mylib3
public myvar = "Bill"
 
proc init()
    myvar = "Tom"
endproc
 
proc getMyVar()
return myvar

Note that the filenames specified as arguments to the require() function may contain the special filename prefixes such as lib:/ and thirdparytlibs:/ etc.