Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Use glob patterns while reading a file



Peter Stephenson wrote on Fri, 09 Nov 2018 09:35 +0000:
> On Thu, 2018-11-08 at 23:53 +0100, Dominik Ritter wrote:
> > I want to use read one of two files, regardless if it exists or not. My
> > first approach is to use the alternative glob syntax, but with no luck:
> > $(< ./(a|b)(N))
> 
> That's a special syntax, expecting a single file so that it doesn't try
> to do globbing.

I think this violates the principle of least surprise.  Grammars should be
composable and shouldn't have special cases.  The forms «foo» and
«print -r -- "$(<foo)"» should always be equivalent (assuming 'foo' outputs
a trailing newline).  Similarly, «<foo» and «<foo $READNULLCMD» are usually
equivalent, but «$(<*)» and «$(<* $READNULLCMD)» are not.

(I know that $(<foo) is optimised, but the whole point of optimisations is that
they don't change the semantics.)

By default, «*» after «<» is a glob.  There is no reason for a user to expect
«$(<*)» to be different: neither command substitution nor input redirection
implies 'noglob' or NO_MULTIOS in any other situation.

Frankly, I'm tempted to treat it as a bug — but before I jump to conclusions,
was this behaviour intentionally implemented this way?

>

The manual says:
.
    The substitution `tt($LPAR()cat) var(foo)tt(RPAR())' may be replaced
    by the equivalent but faster `tt($LPAR()<)var(foo)tt(RPAR())'.
.
which is correct only when var(foo) does not involve globbing.  I don't want to
open the user manual v. reference manual can of worms again, but I think there
is room for improvement here.

> You can get it to work with
> 
> $(cat <./(a|b)(N))

Cheers,

Daniel



Messages sorted by: Reverse Date, Date, Thread, Author