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

Re: Slurping a file (was: more spllitting travails)



On 2/11/24, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Sat, Feb 10, 2024 at 2:48 PM Stephane Chazelas <stephane@xxxxxxxxxxxx>
> wrote:
>>
>> IMO, it would be more useful if the result was returned in the
>> variable whose name was given as argument (defaulting to REPLY
>> if none was given like for read or sysread).
>
> Could also read a file provided by name as an argument instead of only
> reading stdin, but I elected to commit the most straightforward
> version.
>
>> And would be better if upon error the returned variable
>> contained either what was successfully read or nothing (like
>> read but unlikely sysread).
>
> I had the impression this slurp-er was intended to work like examples
> from other languages, which do not have "read"-like behavior.
>
>> zslurp() {
>>   emulate -L zsh -o no_multibyte
>>   typeset -n _zslurp_var=${1-REPLY}
>
> Is there really any reason to prefix the locals with "_zslurp_" ?
> That's good practice if the function might call other code that's less
> careful about it's names and scoping, or if you need to the variable
> to become global, but nothing like that occurs here.

I guess it makes this outcome less likely,
% zslurp _zslurp_var
zslurp:2: _zslurp_var: invalid self reference

Is there any way to avoid it completely?

-- 
Mikael Magnusson




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