Road to Julia 4: Packages, Files and Modules

Packages

Julia has an inbuilt package manager Pkg which aids in the (un)installation and updating of individual packages. Pkg can be used through a REPL session by pressing ] or script by having the line

using Pkg

Once Pkg has been implemented, using it is fairly straightforward:

 REPLScript
Add a packagepkg> add packagePkg.add("package")
Remove a packagepkg> rm packagePkg.rm("package")
Update a packagepkg> up packagePkg.update("package")
Update all packagespkg> upPkg.update()

Unfortunately, at this point in time there does not appear to be a centralised repository/list of the packages wherein one could search for packages. Consequently, search engines are a necessity when finding appropriate packages – it is my personal hope that this will be resolved in future.

Once installed, a package may then be used within a script by implementing the line

using package

Files

The most important factor in writing projects longer than a few lines is being able to segment it into multiple files, increasing maintainability and readability. In Julia, files can be accessed by a code by using include() with quotations marks around the file name (including extension!), e.g.

include("file.jl")

Modules

Julia has the functionality of modules, each of which introduces its own global scope, which is delimited syntactically by

module ModuleName
...
end

As each module it its own top-level domain, they can contain anything a main file would, e.g. variables, functions, structures etc. Consequently, files may contain multiple modules, each with its own contents, e.g.

module ModuleName1
    include("file1.jl")
    var1 = 1
    var2 = 2
end

module ModuleName2
    include("file1.jl")
    include("file2.jl")
    var1 = 10
    var2 = 100
end

Accessing the contents of a module is conducted by using dot notation (telling the script to look inside the space by giving the module name [NOT THE FILENAME] followed by a full stop and then the name of the variable/function/structure etc), e.g. ModuleName.var1.

Leave a Reply

Your email address will not be published.