Reputation: 2568
I am having package names in an array, trying to execute "using <PACKAGE_NAME>" with the following code in deps.jl:
#! /opt/julia/julia-1.1.0/bin/Julia
packages =["MbedTLS","HTTP"]
for package in packages
try
("using "package)
cath err
println("$err")
end
end
executing, $/home/julia/deps.jl, throws error "cannot juxtapose string literal". Please help me!
Upvotes: 0
Views: 124
Reputation: 5583
You can use symbols and @eval
macro to do this. @eval
macro runs the given expression at the top level.
packages = [:MbedTLS, :HTTP] # use symbols instead of strings
for package in packages
try
@eval(using $package)
catch err
println("$err")
end
end
If you need to use strings for some reason, you can first convert it to a Symbol
and use @eval
with the results.
packages = ["MbedTLS", "HTTP"]
for package in packages
try
@eval(using $(Symbol(package)))
catch err
println("$err")
end
end
"using "package
tries to create the string literal "using" to juxtapose it to variable package
(i.e. like 5
in x=3; b = 5x
). String literal juxtaposition is a syntax error in Julia 1.0. Even if it wasn't a syntax error, I do not recall that it would be used for running expressions.
Please see Metaprogramming section in Julia documentation for more about creating and manipulating code from within Julia.
Upvotes: 3