9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] Local variables and rc functions
@ 2008-08-13 21:27 Micah Stetson
  2008-08-14  9:25 ` roger peppe
  0 siblings, 1 reply; 7+ messages in thread
From: Micah Stetson @ 2008-08-13 21:27 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Is this a bug?

% fn foo { echo $bar }
% bar=baz foo

%

I would expect to see baz instead of a blank line.

Micah



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] Local variables and rc functions
  2008-08-13 21:27 [9fans] Local variables and rc functions Micah Stetson
@ 2008-08-14  9:25 ` roger peppe
  2008-08-14 11:20   ` kazumi iwane
  0 siblings, 1 reply; 7+ messages in thread
From: roger peppe @ 2008-08-14  9:25 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

looks like a bug to me; after all:

% bar=baz {echo $bar}
baz
%

i'm somewhat surprised this hasn't been fixed years ago.

On Wed, Aug 13, 2008 at 10:27 PM, Micah Stetson <micah@stetsonnet.org> wrote:
> Is this a bug?
>
> % fn foo { echo $bar }
> % bar=baz foo
>
> %
>
> I would expect to see baz instead of a blank line.
>
> Micah
>
>



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] Local variables and rc functions
  2008-08-14  9:25 ` roger peppe
@ 2008-08-14 11:20   ` kazumi iwane
  2008-08-14 12:14     ` roger peppe
  0 siblings, 1 reply; 7+ messages in thread
From: kazumi iwane @ 2008-08-14 11:20 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

if you want to delay the expansion of an env var until fn invocation,,use eval.

% fn foo {eval echo $$bar}

On 8/14/08, roger peppe <rogpeppe@gmail.com> wrote:
> looks like a bug to me; after all:
>
> % bar=baz {echo $bar}
> baz
> %
>
> i'm somewhat surprised this hasn't been fixed years ago.
>
> On Wed, Aug 13, 2008 at 10:27 PM, Micah Stetson <micah@stetsonnet.org>
> wrote:
>> Is this a bug?
>>
>> % fn foo { echo $bar }
>> % bar=baz foo
>>
>> %
>>
>> I would expect to see baz instead of a blank line.
>>
>> Micah



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] Local variables and rc functions
  2008-08-14 11:20   ` kazumi iwane
@ 2008-08-14 12:14     ` roger peppe
  2008-08-14 12:47       ` kazumi iwane
  0 siblings, 1 reply; 7+ messages in thread
From: roger peppe @ 2008-08-14 12:14 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On Thu, Aug 14, 2008 at 12:20 PM, kazumi iwane <kiwane@gmail.com> wrote:
> if you want to delay the expansion of an env var until fn invocation,,use eval.
>
> % fn foo {eval echo $$bar}

sorry, but that's so, so wrong.

1)  $$bar gives the value of the variable
named by the contents of the variable bar,
so that the above, given that $bar contains 'baz'
will echo $baz.

2) $$bar above is evaluated _before_ eval is invoked,
so the line is not very different from {echo $$bar}, except that
if $$bar holds any syntax characters they'll get interpreted by the shell.
it's almost never necessary to use eval with rc.

3) expansion of env. vars *is* delayed until fn invocation.
e.g.
% fn foo {echo $bar}
% bar = baz
% foo
baz
%

it just seems as if the local variable assignment isn't propagated
into the fn context. i don't have time to look into it now (i really
shouldn't be spending time writing this email, but i couldn't resist the bait).

  rog.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] Local variables and rc functions
  2008-08-14 12:14     ` roger peppe
@ 2008-08-14 12:47       ` kazumi iwane
  0 siblings, 0 replies; 7+ messages in thread
From: kazumi iwane @ 2008-08-14 12:47 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

i apologize for the noise. i should have tested befoqe posting.
rog, thank you for correcting me.

On 8/14/08, roger peppe <rogpeppe@gmail.com> wrote:
> On Thu, Aug 14, 2008 at 12:20 PM, kazumi iwane <kiwane@gmail.com> wrote:
>> if you want to delay the expansion of an env var until fn invocation,,use
>> eval.
>>
>> % fn foo {eval echo $$bar}
>
> sorry, but that's so, so wrong.
>
> 1)  $$bar gives the value of the variable
> named by the contents of the variable bar,
> so that the above, given that $bar contains 'baz'
> will echo $baz.
>
> 2) $$bar above is evaluated _before_ eval is invoked,
> so the line is not very different from {echo $$bar}, except that
> if $$bar holds any syntax characters they'll get interpreted by the shell.
> it's almost never necessary to use eval with rc.
>
> 3) expansion of env. vars *is* delayed until fn invocation.
> e.g.
> % fn foo {echo $bar}
> % bar = baz
> % foo
> baz
> %
>
> it just seems as if the local variable assignment isn't propagated
> into the fn context. i don't have time to look into it now (i really
> shouldn't be spending time writing this email, but i couldn't resist the
> bait).
>
>   rog.
>
>



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] Local variables and rc functions
  2008-08-14 14:32 Russ Cox
@ 2008-08-14 18:10 ` Micah Stetson
  0 siblings, 0 replies; 7+ messages in thread
From: Micah Stetson @ 2008-08-14 18:10 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

> Both fixes are in plan9port.

Thanks, Russ.

Micah



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] Local variables and rc functions
@ 2008-08-14 14:32 Russ Cox
  2008-08-14 18:10 ` Micah Stetson
  0 siblings, 1 reply; 7+ messages in thread
From: Russ Cox @ 2008-08-14 14:32 UTC (permalink / raw)
  To: 9fans

> Is this a bug?
>
> % fn foo { echo $bar }
> % bar=baz foo
>
> %
>
> I would expect to see baz instead of a blank line.

Yes, it is a bug.  The fix is:

diff -r f7e7b9ab4cfb src/cmd/rc/simple.c
--- a/src/cmd/rc/simple.c	Sun Jul 20 06:17:17 2008 -0400
+++ b/src/cmd/rc/simple.c	Thu Aug 14 10:27:08 2008 -0400
@@ -130,7 +130,7 @@ execfunc(var *func)
 	starval = runq->argv->words;
 	runq->argv->words = 0;
 	poplist();
-	start(func->fn, func->pc, (struct var *)0);
+	start(func->fn, func->pc, runq->local);
 	runq->local = newvar(strdup("*"), runq->local);
 	runq->local->val = starval;
 	runq->local->changed = 1;

There's also a small performance bug here:

diff -r f7e7b9ab4cfb src/cmd/rc/code.c
--- a/src/cmd/rc/code.c	Sun Jul 20 06:17:17 2008 -0400
+++ b/src/cmd/rc/code.c	Thu Aug 14 10:27:08 2008 -0400
@@ -339,9 +339,9 @@ outcode(tree *t, int eflag)
 				outcode(c0, eflag);
 				emitf(Xlocal);
 			}
-			t = tt;
-			outcode(c2, eflag);
-			for(;t->type=='=';t = c2) emitf(Xunlocal);
+			outcode(t, eflag);
+			for(t = tt; t->type=='='; t = c2)
+				emitf(Xunlocal);
 		}
 		else{
 			for(t = tt;t;t = c2){

Both fixes are in plan9port.

Russ



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-08-14 18:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-13 21:27 [9fans] Local variables and rc functions Micah Stetson
2008-08-14  9:25 ` roger peppe
2008-08-14 11:20   ` kazumi iwane
2008-08-14 12:14     ` roger peppe
2008-08-14 12:47       ` kazumi iwane
2008-08-14 14:32 Russ Cox
2008-08-14 18:10 ` Micah Stetson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).