Gentoo Archives: gentoo-dev

From: Dan Douglas <ormaaj@×××××.com>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] lastpipe
Date: Sat, 26 May 2012 01:57:42
Message-Id: 1845702.CGevgmI2i3@smorgbox
In Reply to: Re: [gentoo-dev] lastpipe by Mike Frysinger
1 On Friday, May 25, 2012 08:52:00 PM Mike Frysinger wrote:
2 > On Friday 25 May 2012 18:33:43 Ciaran McCreesh wrote:
3 > > On Fri, 25 May 2012 15:02:32 -0500 Dan Douglas wrote:
4 > > > If it were made a policy now that ebuilds and eclasses cannot depend
5 > > > upon the subshell (for example, to set temporary positional
6 > > > parameters or isolate temporary variables), then maybe someday in the
7 > > > distant future this could be made the default, and in the meantime,
8 > > > an option for those with new enough shells. Since dependence on the
9 > > > subshell isn't very common, I think this should be feasible, and of
10 > > > course as a workaround all that's required is to wrap any such
11 > > > commands in parentheses.
12 > >
13 > > We'll be able to turn that on in a controlled way in EAPI 6. Having
14 > > said that, if we're reaching the point where speed of bash code is
15 > > at all relevant, then ebuilds are doing something wrong...
16 >
17 > i don't think speed is the main motivator, but rather avoiding behavior that
18 > bites new people all the time:
19 > count=0
20 > printf '%s\n' a b c | \
21 > while read line ; do
22 > : $(( count++ ))
23 > done
24 > echo $count
25 >
26 > w/out lastpipe, that shows 0. w/lastpipe, that shows 3.
27 > -mike
28
29 Right, performance is just a nice side-effect. It makes a number of things
30 cleaner (especially if the printf in that case were replaced with something
31 more complicated), and is more intuitive for beginners .
32
33 However, all involved code needs to be able to expect lastpipe to always be
34 either one way or the other, not mix-and-match. This means either EAPI 6
35 requires Bash 4.2, or if Bash version detection is involved, a lot of the
36 benefit to lastpipe is lost. Code which can't predict the behavior has to be
37 written not only as though lastpipe were disabled, but also to account for the
38 possility that it is enabled to avoid name conflicts.
39
40 In that example it would mean adding an explicit subshell, or saving and
41 restoring the value of "line" before/after, or putting it into a function and
42 using locals, or always making sure code executed after the loop initializes
43 "line" to a known state.
44 --
45 Dan Douglas

Attachments

File name MIME type
signature.asc application/pgp-signature