zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: saved from prince of eval
Date: Sun, 8 Nov 2015 11:57:52 -0800	[thread overview]
Message-ID: <151108115752.ZM859@torch.brasslantern.com> (raw)
In-Reply-To: <563F8168.5080006@eastlink.ca>

On Nov 8,  9:07am, Ray Andrews wrote:
}
} I'm trying to get some mileage out of the '(e)' flag, but it frustrates me

If you haven't already, you should read through the "Rules" section
under "Parameter Expansion".  "man zshexpn" and search for "Rules".

In particular the last sentence of the #1 rule "Nested Substitution":

     ... Note  that,  unless  the  '(P)'
     flag is present, the flags and any subscripts apply directly to
     the value of the nested substitution; for example,  the  expan-
     sion ${${foo}} behaves exactly the same as ${foo}.

Then note that (e) isn't applied until nearly the end of the procedure,
at rule #10.

So in this expression:

} 1:    $ foo="${(e)${array}[${top}, ${bottom}]}"

First ${array} expands, and then [$[top},${bottom}] applies to that
value -- which isn't an array, it's a string, so the subscripts are
extracting a range of characters from that string.  Finally that
substring is re-evaluated (but probably is nothing interesting).

What you needed was

    foo="${(e)${(P)array}[${top}, ${bottom}]}"

though as previously explained that doesn't work as you want for an
associative $array.
 
} If I insert the literal name of the array in place of "${array}" 
} everything is fine.

In "${(e)the_literal_array[${top}, ${bottom}]}" the subscript applies
directly the array (because there is no nested expression).

} But this works:
} 
} 2:    $ bar='\$${array}[${top}, ${bottom}]'
} 3:    $ foo="${(e)$(print -R "${(e)${bar}}")}"

This describes that you've got at least three levels of unexpanded
references:

- whatever array is named by $array contains unexpanded references
  in the array elements
- bar contains unexpanded references to $array, $top, and $bottom
- expanding \$ in $bar becomes an unexpanded reference to the array
  named by $array with the subscript expanded from $top and $bottom

I have no idea why you want to put yourself in that situation, but if
you have three unexpanded levels then you need three re-evaluations:

- the inner (e) flag
- $(...), which you can think of as:  eval "..." | read
- the outer (e) flag
 
} I'm not sure how to interpret it tho. Is 'print' doing the work here, or 
} is print a bystander as a nested use of '(e)' works?

In this case the "print" is doing nothing except provide the standard
output to be captured from $(...).

You can replace the $(...) with another ${(e)...} to get your third
needed re-evaluation, and you don't need the inermost ${ } around bar:

    foo="${(e)${(e)${(e)bar}}}"

Again it's pretty ridiculous that you're doing anything like this in
the first place.

}       $ foo='stranger'
}       $ bar='echo howdy $foo'
}       $ eval baz=$bar; echo $baz
}     zsh: command not found: howdy << Ferkrissakes

Well, think about it a bit harder.  Or better yet, "setopt xtrace" and
watch what is happening.

torch% foo='stranger'
torch% bar='echo howdy $foo'
torch% setopt xtrace
torch% eval baz=$bar; echo $baz
+zsh:4> eval 'baz=echo howdy $foo'
+(eval):1> baz=echo howdy stranger
zsh: command not found: howdy

The assignment becomes "baz=echo" which is prefixed to the command
"howdy stranger".


  reply	other threads:[~2015-11-08 19:57 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-08 17:07 Ray Andrews
2015-11-08 19:57 ` Bart Schaefer [this message]
2015-11-08 22:04   ` Ray Andrews
2015-11-09  8:50     ` Ray Andrews
2015-11-09 18:41       ` Bart Schaefer
2015-11-10  0:48         ` Ray Andrews
2015-11-11  1:07         ` Ray Andrews
2015-11-11  4:18           ` Bart Schaefer
2015-11-11  5:31             ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=151108115752.ZM859@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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).