In generally this is a good, if not necessary thing.
It allows sequences to be stored in variables and produced as expressions and be preserved as sequences. For example
A=<[ 1 , 2 , ]>
xquery -f file.query -v sequence_var $A
Note that you can pass a single XdmValue ($A) as a parameter to Xquery.
The problem comes if you want to 'flatten' the sequence to seperate positional params.
Example
A=$(ls) # produces a single sequence of files
echo $A
This looks right but under the hood echo is getting a single argument (argv[1]) which is a sequence.
Suppose I want to delete all these files
posix:rm $A
Ups ... rm now gets 1 argument ... it has to know that if the argument is sequence to iterate over each item like this
for i in $A ; do posix:rm $i ; done
For many commands getting sequences where they expect items is problematic.
But on the other hand being able to preserve sequences is critical.
So What to do ?
I'm working on (and open to suggestion) a syntax which forcibly flattens sequences into positional parameters.
I'm working on (and open to suggestion) a syntax which forcibly flattens sequences into positional parameters.
Something like maybe
$-variable
although I'd like it to work with inline expressions as well
posix:rm $(ls) # How to get this to flatten ?
Comments welcome on this idea
I'm experimenting with a non-invasive partial solution for this problem. Its not a general solution but might be useful enough to be sufficient.
ReplyDeleteI added a -E (or -expand) option to set.
This tells set that it should expand (flatten) all sequences into individual XdmItme's for each positional parameter.
Example
set $(ls) # sets $1 to be a sequence of file names
set -E -- $(ls) # sets $1...$N to be individual file names
I've implemented a more invasive change, checked in but not released yet.
ReplyDeleteThis reverts the default behaviour for sequences to expand to multiple arguments, but allows direct control of each expression by using {expr}.
Example
set $(ls) # sets multiple args
set {$(ls)} # sets 1 arg as a sequence
set <[1,2,3]> # sets 3 args
set {<[1,2,3]>} # sets 1 arg as sequence of 3