views:

132

answers:

3

Hey all,

I am currently looking at using Scala scripts to control the life-cycle of a MySQL database instead of using MS-DOS scripts (I am on Windows XP).

I want to have a configuration script which only holds configuration information, and 1 or more management scripts which use the configuration information to perform various operations such as start, stop, show status, etc .....

Is it possible to write a Scala script which includes/imports/references another Scala script?

I had a look at the -i option of the scala interpreter, but this launches an interactive session which is not what I want.

+5  A: 

According to Scala man, script pre-loading only works for interactive mode.

As a workaround, you can exit the interactive mode after running the script. Here's the code of child.bat (script that includes another generic one):

::#!
@echo off
call scala -i genetic.bat %0 
goto :eof
::!#
def childFunc="child"

println(geneticFunc)
println(childFunc)
exit;

genericFunc is defined at genetic.bat

The output of child.bat:

>child.bat
Loading genetic.bat...
...    
geneticFunc: java.lang.String
Loading child.bat...
...
childFunc: java.lang.String
generic
child
Vasil Remeniuk
Thanks. Interactive mode does not help me much. As a workaround, I will probably use XML to store the configuration information and instead of having more than 1 scripts, I would just write a single script to which one can pass command-line options to perform various operations.
Guillaume
Oops, did not see your update as I posted my comment. I will have a look at your method.
Guillaume
The only problem with this approach is that when the script is loaded in interactive mode, interpreter cannot recognize the batch header, and prints some errors to the console. However, those warnings can be ignored, as the script eventually runs fine.
Vasil Remeniuk
A: 

One option would be to have a script which concatenates two files together and then launches it, something like:

@echo off
type config.scala > temp.scala
type code.scala >> temp.scala
scala temp.scala
del temp.scala

or similar. Then you keep the two seperate as you wished.

MatthieuF
+3  A: 

I'd use Process and call the other Scala script just like any other command.

Daniel
I feel like I've only barely cracked the surface of Scala's richness, so when I read the OP's question I expected to learn Scala adds something of its own beyond java.lang.ProcessBuilder... but I haven't found anything. Is this the case?
Rodney Gitzel
@Rodney Yes. It is used extensively by SBT, which has a more up-to-date version of it. This version is out of date, and it probably doesn't even compile under Scala 2.8. I think there were plans to update it, but you'd better talk to its developer about it.
Daniel
For my use case, Process would be very useful to execute MySQL scripts. I had actually written simple Scala code to do that, although my solution is nowhere as complete as Process. However, what I am looking for is the ability to use variables and methods defined in a scala script from another Scala script. I can't see how Process would help me here. Something like the Sling Scala scripting engine seems quite useful.
Guillaume