GithubHelp home page GithubHelp logo

Comments (6)

SylvAbdomen avatar SylvAbdomen commented on August 27, 2024 1

Interesting! Thanks for looking in to this. I didn't know about shvar. For now I can work around the issue with a loop like this.

proc p {
	var line = ""
	while true {
		try { shvar IFS= { read -r line } }
		if (_status !== 0) {
			break
		}
		write $line
	}
}

I just recently started learning YSH, and it's been great so far. Love the project!

from oil.

andychu avatar andychu commented on August 27, 2024

Hmmm I just reproduced this ...

Let me look into this, thanks for the clear report!

from oil.

andychu avatar andychu commented on August 27, 2024

Hm funny that it works in proc without IFS= (though removing IFS= does change the behavior, even for one read arg!)


But I think there's still a bug here. I think it is probably related to lack of dynamic scope in procs

Our replacement for dynamic scope is shvar IFS= { read -r line }, but that is actually problematic in loops, since it's a command

I'll look into it a little more ... hm

from oil.

andychu avatar andychu commented on August 27, 2024

OK wow, now I see the problem...

It's because

  1. IFS= anycommand creates a new stack frame in shell. That's how "temp bindings" are implemented in all shells

  2. In OSH shell functions, read -r line uses dynamic scope. This means it often creates a global variable, unless you declare with local or var inside the function

$ f() { read -r zz; echo "inside f: $zz"; }; f <<< 'stdin'; echo "global: $zz"
inside f: stdin
global: stdin

Actually I was confused about this for a long time ... I thought that read -r line would logically modify the calling stack frame! But it doesn't -- it looks up the whole stack, and if nothing is there, it creates a global

  1. YSH proc don't have dynamic scope. We got rid of that because it's unfamiliar to say Python and JS programmers

So basically YSH creates the line variable in the temp binding, and it gets immediately thrown away when read returns !!


So bottom line:

I will add

And then let me think about what we can do about this IFS= read pitfall

It will also affect mapfile and any other builtins which set variables


Hm hm

from oil.

andychu avatar andychu commented on August 27, 2024

I implemented

  • read --raw-line for unbuffered read
  • for line in <> { echo $line } for buffered read

So you don't have to use the old shell idioms

Documented here

9911231

I guess I will turn this bug into "do something about IFS= read -r` in YSH ... that is confusing

Maybe we need a lint rule or something

from oil.

andychu avatar andychu commented on August 27, 2024

Related to

from oil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.