value = eval( "foo=2; bar=3; someMethod();" ); |
value = eval( "foo=2; bar=3; someMethod();"); print( foo ); // 2 |
value = eval( foo=2; bar=3; someMethod(); ); // or value = eval( foo=2; bar=3; someMethod(); ); |
import bsh.regex; ... String myString = "My name is Pat Niemeyer"; regex( myString/s/Pat/Patrick/g myString/My name is (\w+) (\w+)/ firstName = $1 lastName = $2 ); print( firstName ); // Patrick |
// Implement a subset of Bourne Shell functionality String myString = "Pat Niemeyer"; InputStream myStream = url.openStream(); File myFile = ...; sh( cat stream > foo.txt lines=`grep "foo" myFile` echo $myString | someApplication ); print(lines); // foo this foo that... |
// SQL - Implement specialized SQL syntax rowset = sql( open db://somedatabase select * from Foo where Bar ); for ( row : rowset ) print( row ); |
// Simple multi-line "here" document String myString = doc( This is multi-line text with a platform specific line ending... Foo! ); String myString = doc( "\n", Maybe this form specifies the line ending for long lines of text like this... Foo! ); // Lists could work like this as well... List myList = list( { 1, 2, { "foo", "bar" } } ); |
// XML and XPath Document xmlDoc = xml( <Library> <Book name="Learning Java" category="foo"> <stuff/> </Book> </Library> ); String myText = ...; category = xpath( myText/Library/Book[name="Learning Java"]/@category ); |
// Simple "command" style user entry without parens or semicolons commands( print 2+2 someMethod arg1 arg2 ); |
// Completely crazy things... // Implement a subset of awk for BeanShell, invoking BeanShell methods someMethod( arg ) { ... } result = bawk( /Name/ { names++; someMethod( $1 ); } END { print "the end" } ); |
// Nesting should work... list=list(); sh( cd /files foreach f in *.txt do eval( String path = f.getCanonicalPath(); list.add( new URL(path) ); ); done; ); |