- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Scala scripting
Mon, 2009-07-13, 03:09
Hi,
I am experimenting with using Scala instead of gawk or perl for simple commandline scripting. Currently I have this simple script:
find . -name '*.class' | xargs -n 1000 scala -e "argv.toList.map{ x => x.replace('/','.').substring(2,x.lastIndexOf(\".\"))}.foreach(println _)"
This finds all classes in an unpacked jar file and prints them in standard java form. For example "./com/sun/org/apache/xpath/internal/axes/FilterExprWalker.class" becomes "com.sun.org.apache.xpath.internal.axes.FilterExprWalker".
While this worked perfectly, there are two issues I am not 100% satisfied with.
1) change xargs -n1000 to xargs -n1 and this never completes (for rt.jar). Startup is order of magnitutes too slow. Perhabs there are no easy solutions, but I could wish for a simple command line parameter that would make scala cache the compiled script. When used together with -e it could hash the script into a temp filename for caching.
2) I want to get rid of the xargs. Maybe there already is a way but I can't find it. I want to do something like this:
find . -name '*.class' | scala -e "in.toStream.map{ x => x.replace('/','.').substring(2,x.lastIndexOf(\".\"))}.foreach(println _)"
There is readline but it will only read one line at a time. It requires far too much boilerplate to create a stream of input lines to be useable together with the -e option. Any ideas on how to do this?
In general I feel Scala needs a larger toolset to be a better scripting option.
Thanks,
Baldur
I am experimenting with using Scala instead of gawk or perl for simple commandline scripting. Currently I have this simple script:
find . -name '*.class' | xargs -n 1000 scala -e "argv.toList.map{ x => x.replace('/','.').substring(2,x.lastIndexOf(\".\"))}.foreach(println _)"
This finds all classes in an unpacked jar file and prints them in standard java form. For example "./com/sun/org/apache/xpath/internal/axes/FilterExprWalker.class" becomes "com.sun.org.apache.xpath.internal.axes.FilterExprWalker".
While this worked perfectly, there are two issues I am not 100% satisfied with.
1) change xargs -n1000 to xargs -n1 and this never completes (for rt.jar). Startup is order of magnitutes too slow. Perhabs there are no easy solutions, but I could wish for a simple command line parameter that would make scala cache the compiled script. When used together with -e it could hash the script into a temp filename for caching.
2) I want to get rid of the xargs. Maybe there already is a way but I can't find it. I want to do something like this:
find . -name '*.class' | scala -e "in.toStream.map{ x => x.replace('/','.').substring(2,x.lastIndexOf(\".\"))}.foreach(println _)"
There is readline but it will only read one line at a time. It requires far too much boilerplate to create a stream of input lines to be useable together with the -e option. Any ideas on how to do this?
In general I feel Scala needs a larger toolset to be a better scripting option.
Thanks,
Baldur
Mon, 2009-07-13, 08:57
#2
RE: Scala scripting
> In general I feel Scala needs a larger toolset to be a better
> scripting option.
+1 !!
Mon, 2009-07-13, 09:27
#3
Re: Scala scripting
Perhaps PowerShell could be used for some inspiration.
2009/7/13 Detering Dirk :
>> In general I feel Scala needs a larger toolset to be a better
>> scripting option.
>
> +1 !!
>
Mon, 2009-07-13, 16:17
#4
Re: Scala scripting
Scala will never be competitive on startup-time. Maybe on .Net, but, even then, I doubt it.
And, yes, a scala.shell library would be much welcome. If nothing else, replacing a whole shell script with Scala instead of just part of it would overcome the startup-time disadvantage.
On Sun, Jul 12, 2009 at 11:09 PM, Baldur Norddahl <baldur.norddahl@gmail.com> wrote:
--
Daniel C. Sobral
Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
On Sun, Jul 12, 2009 at 11:09 PM, Baldur Norddahl <baldur.norddahl@gmail.com> wrote:
Hi,
I am experimenting with using Scala instead of gawk or perl for simple commandline scripting. Currently I have this simple script:
find . -name '*.class' | xargs -n 1000 scala -e "argv.toList.map{ x => x.replace('/','.').substring(2,x.lastIndexOf(\".\"))}.foreach(println _)"
This finds all classes in an unpacked jar file and prints them in standard java form. For example "./com/sun/org/apache/xpath/internal/axes/FilterExprWalker.class" becomes "com.sun.org.apache.xpath.internal.axes.FilterExprWalker".
While this worked perfectly, there are two issues I am not 100% satisfied with.
1) change xargs -n1000 to xargs -n1 and this never completes (for rt.jar). Startup is order of magnitutes too slow. Perhabs there are no easy solutions, but I could wish for a simple command line parameter that would make scala cache the compiled script. When used together with -e it could hash the script into a temp filename for caching.
2) I want to get rid of the xargs. Maybe there already is a way but I can't find it. I want to do something like this:
find . -name '*.class' | scala -e "in.toStream.map{ x => x.replace('/','.').substring(2,x.lastIndexOf(\".\"))}.foreach(println _)"
There is readline but it will only read one line at a time. It requires far too much boilerplate to create a stream of input lines to be useable together with the -e option. Any ideas on how to do this?
In general I feel Scala needs a larger toolset to be a better scripting option.
Thanks,
Baldur
--
Daniel C. Sobral
Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
Mon, 2009-07-13, 18:27
#5
Re: Scala scripting
You can use something like Nailgun (http://martiansoftware.com/nailgun/index.html) to overcome JVM startup times.
--j
On Mon, Jul 13, 2009 at 7:53 AM, Daniel Sobral <dcsobral@gmail.com> wrote:
--j
On Mon, Jul 13, 2009 at 7:53 AM, Daniel Sobral <dcsobral@gmail.com> wrote:
Scala will never be competitive on startup-time. Maybe on .Net, but, even then, I doubt it. And, yes, a scala.shell library would be much welcome. If nothing else, replacing a whole shell script with Scala instead of just part of it would overcome the startup-time disadvantage.
On Sun, Jul 12, 2009 at 11:09 PM, Baldur Norddahl <baldur.norddahl@gmail.com> wrote:
Hi,
I am experimenting with using Scala instead of gawk or perl for simple commandline scripting. Currently I have this simple script:
find . -name '*.class' | xargs -n 1000 scala -e "argv.toList.map{ x => x.replace('/','.').substring(2,x.lastIndexOf(\".\"))}.foreach(println _)"
This finds all classes in an unpacked jar file and prints them in standard java form. For example "./com/sun/org/apache/xpath/internal/axes/FilterExprWalker.class" becomes "com.sun.org.apache.xpath.internal.axes.FilterExprWalker".
While this worked perfectly, there are two issues I am not 100% satisfied with.
1) change xargs -n1000 to xargs -n1 and this never completes (for rt.jar). Startup is order of magnitutes too slow. Perhabs there are no easy solutions, but I could wish for a simple command line parameter that would make scala cache the compiled script. When used together with -e it could hash the script into a temp filename for caching.
2) I want to get rid of the xargs. Maybe there already is a way but I can't find it. I want to do something like this:
find . -name '*.class' | scala -e "in.toStream.map{ x => x.replace('/','.').substring(2,x.lastIndexOf(\".\"))}.foreach(println _)"
There is readline but it will only read one line at a time. It requires far too much boilerplate to create a stream of input lines to be useable together with the -e option. Any ideas on how to do this?
In general I feel Scala needs a larger toolset to be a better scripting option.
Thanks,
Baldur
--
Daniel C. Sobral
Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
Mon, 2009-07-13, 19:07
#6
Re: Scala scripting
On Mon, Jul 13, 2009 at 4:53 PM, Daniel Sobral <dcsobral@gmail.com> wrote:
Start time without -savecompiled or with -e is about 2.6 seconds on my computer. With -savecompiled it is only about 0.4 seconds. This is still slow but much much better than almost 3 seconds. So I will put up that -savecompiled together with -e should be fixed.
The generated jar file does not seem to be a valid jar. But unzipping it and running this command works:
java -cp /usr/share/java/scala-library.jar:. Main
Now start time is only 0.3 seconds.
I tried to compiled it with gcj but with no success. That is too bad since it would have solved the start time issue.
Never the less - if JVM start time is only responsible for 0.3 seconds, why does it spend 2.3 seconds to compile a simple one line script?
Scala will never be competitive on startup-time. Maybe on .Net, but, even then, I doubt it.
Start time without -savecompiled or with -e is about 2.6 seconds on my computer. With -savecompiled it is only about 0.4 seconds. This is still slow but much much better than almost 3 seconds. So I will put up that -savecompiled together with -e should be fixed.
The generated jar file does not seem to be a valid jar. But unzipping it and running this command works:
java -cp /usr/share/java/scala-library.jar:. Main
Now start time is only 0.3 seconds.
I tried to compiled it with gcj but with no success. That is too bad since it would have solved the start time issue.
Never the less - if JVM start time is only responsible for 0.3 seconds, why does it spend 2.3 seconds to compile a simple one line script?
Baldur Norddahl schrieb:
> find . -name '*.class' | scala -e "in.toStream.map{ x => x.replace('/','.').substring(2,x.lastIndexOf(\".\"))}.foreach(println _)"
>
> There is readline but it will only read one line at a time. It requires
> far too much boilerplate to create a stream of input lines to be useable
> together with the -e option. Any ideas on how to do this?
Not exactly pretty, but less than twice the length of your idea:
find . -name '*.class' | scala -e "var x : String = _; while({x = readLine; x != null}) { println(x.replace('/','.').substring(2,x.lastIndexOf(\".\")))}"
But a library of shell scripting primitives would be nice here, allowing something like
scala -cp Shell.jar -e "Cwd.find(Name('*.class')).map{ x => x.replace('/','.').substring(2,x.lastIndexOf(\".\"))}.foreach(println _)"
A source for inspriation could be http://www.scsh.net/
- Florian