zsh-users
 help / color / mirror / code / Atom feed
* Forcing expansion without explicit eval nor a subshell?
@ 2005-11-05  4:01 Lloyd Zusman
  2005-11-05  8:45 ` DervishD
  2005-11-05 19:56 ` Bart Schaefer
  0 siblings, 2 replies; 9+ messages in thread
From: Lloyd Zusman @ 2005-11-05  4:01 UTC (permalink / raw)
  To: zsh-users

Suppose I have the following variable defined:

  yumargs='--disablerepo=livna{,-updates,-testing,-extras}'

I know that I can do this:

  eval yum ${yumargs} update

And this:

  yum $(eval print -- ${yumargs}) update

In both cases, it expands to this before execution (line wrapping added
for clarity):

  yum --disablerepo=livna         \
      --disablerepo=livna-updates \
      --disablerepo=livna-testing \
      --disablerepo=livna-extras  \
      update

However, I'm wondering if there is a way to get the same effect without
explicitly using "eval" and without invoking a subshell via the $()
construct (nor with its moral equivalent, ``).

I want to assign to "yumargs" exactly as I did above, if possible, and
I'm looking for some combination of modifiers within the ${} construct
which would give me the expansion of "yumargs" that I desire, and
without the need for a subshell.  And I'd like the expansion of
"yumargs" to take place at the time it is used in the "yum" command, not
at the time that I define it.

Is all this possible?  If so, could someone give me a hint or pointer?

By the way, I tried the (e) operator within the ${} construct, but that
didn't work for me.

Thanks in advance for your suggestions.


-- 
 Lloyd Zusman
 ljz@asfast.com
 God bless you.


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

* Re: Forcing expansion without explicit eval nor a subshell?
  2005-11-05  4:01 Forcing expansion without explicit eval nor a subshell? Lloyd Zusman
@ 2005-11-05  8:45 ` DervishD
  2005-11-05 19:56 ` Bart Schaefer
  1 sibling, 0 replies; 9+ messages in thread
From: DervishD @ 2005-11-05  8:45 UTC (permalink / raw)
  To: Lloyd Zusman; +Cc: zsh-users

    Hi Lloyd ;)))))


 * Lloyd Zusman <ljz@asfast.com> dixit:
> Suppose I have the following variable defined:
> 
>   yumargs='--disablerepo=livna{,-updates,-testing,-extras}'
> 
> I know that I can do this:
> 
>   eval yum ${yumargs} update
> 
> And this:
> 
>   yum $(eval print -- ${yumargs}) update
[...] 
> However, I'm wondering if there is a way to get the same effect without
> explicitly using "eval" and without invoking a subshell via the $()
> construct (nor with its moral equivalent, ``).

    Well, I'm by no means an expert, but as far as I know, Zsh does
parameter expansion *first* and brace expansion *last* in the same
step. As zsh does not reparse and rexpand, you need to first expand
the parameter and after that expand the braces. That's why you need
the eval.

> By the way, I tried the (e) operator within the ${} construct, but that
> didn't work for me.

    Of course, because (e) doesn't perform brace expansion ;)

    Sorry for not being much helpful O:) I'm sure that somebody here
can think of a solution without using "eval" (although that's why
eval exists...).

    Buena suerte, Lloyd, y encantado de hablar otra vez contigo :)
(Good luck, Lloyd, and pleased to talk with you again):

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736 | http://www.dervishd.net
http://www.pleyades.net & http://www.gotesdelluna.net
It's my PC and I'll cry if I want to...


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

* Re: Forcing expansion without explicit eval nor a subshell?
  2005-11-05  4:01 Forcing expansion without explicit eval nor a subshell? Lloyd Zusman
  2005-11-05  8:45 ` DervishD
@ 2005-11-05 19:56 ` Bart Schaefer
  2005-11-05 20:03   ` Lloyd Zusman
  1 sibling, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2005-11-05 19:56 UTC (permalink / raw)
  To: Lloyd Zusman, zsh-users

On Nov 4, 11:01pm, Lloyd Zusman wrote:
} Subject: Forcing expansion without explicit eval nor a subshell?
}
} Suppose I have the following variable defined:
} 
}   yumargs='--disablerepo=livna{,-updates,-testing,-extras}'
} 
} I want to assign to "yumargs" exactly as I did above, if possible, and
} I'm looking for some combination of modifiers within the ${} construct
} which would give me the expansion of "yumargs" that I desire, and
} without the need for a subshell.  And I'd like the expansion of
} "yumargs" to take place at the time it is used in the "yum" command, not
} at the time that I define it.
} 
} Is all this possible?  If so, could someone give me a hint or pointer?

Given all the restrictions you've placed, no, it's not possible.  There
is no parameter-expansion modifier to force brace expansion.

If you relax the "exactly as above" restriction, you could do this:

yumargs=( --disablerepo=livna{,-updates,-testing,-extras} )

That is, make yumargs be an array of four strings rather than a single
string that contains a brace expression.  I'm unable to see a reason why
it would matter when the brace expansion is performed; but if you think
it's important to defer it, you're going to be stuck with eval.


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

* Re: Forcing expansion without explicit eval nor a subshell?
  2005-11-05 19:56 ` Bart Schaefer
@ 2005-11-05 20:03   ` Lloyd Zusman
  2005-11-05 21:33     ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Lloyd Zusman @ 2005-11-05 20:03 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer <schaefer@brasslantern.com> writes:

> On Nov 4, 11:01pm, Lloyd Zusman wrote:
> } Subject: Forcing expansion without explicit eval nor a subshell?
> }
> } Suppose I have the following variable defined:
> } 
> }   yumargs='--disablerepo=livna{,-updates,-testing,-extras}'
> } 
> } I want to assign to "yumargs" exactly as I did above, if possible, and
> } I'm looking for some combination of modifiers within the ${} construct
> } which would give me the expansion of "yumargs" that I desire, and
> } without the need for a subshell.  And I'd like the expansion of
> } "yumargs" to take place at the time it is used in the "yum" command, not
> } at the time that I define it.
> } 
> } Is all this possible?  If so, could someone give me a hint or pointer?
>
> Given all the restrictions you've placed, no, it's not possible.  There
> is no parameter-expansion modifier to force brace expansion.
>
> If you relax the "exactly as above" restriction, you could do this:
>
> yumargs=( --disablerepo=livna{,-updates,-testing,-extras} )
>
> That is, make yumargs be an array of four strings rather than a single
> string that contains a brace expression.  I'm unable to see a reason why
> it would matter when the brace expansion is performed; but if you think
> it's important to defer it, you're going to be stuck with eval.

I want to defer it in case I do this:

  yumargs='--disablerepo={dag,$livnastuff}'

... where $livnastuff can change, and where it might or might not
involve more brace expansions.  But that's really idiosyncratic, and I
guess I'll just live with having to use "eval".

Thanks a lot for your help.


-- 
 Lloyd Zusman
 ljz@asfast.com
 God bless you.


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

* Re: Forcing expansion without explicit eval nor a subshell?
  2005-11-05 20:03   ` Lloyd Zusman
@ 2005-11-05 21:33     ` Bart Schaefer
  2005-11-05 22:07       ` Lloyd Zusman
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2005-11-05 21:33 UTC (permalink / raw)
  To: zsh-users

On Nov 5,  3:03pm, Lloyd Zusman wrote:
}
} I want to defer it in case I do this:
} 
}   yumargs='--disablerepo={dag,$livnastuff}'
} 
} ... where $livnastuff can change, and where it might or might not
} involve more brace expansions.

The answer is still to stop thinking about brace expansions and start
thinking about array expansions.

    yumargs=( --disablerepo={dag,'$^livnastuff'} )
    livnastuff=( dries extras )
    yum ${(e)yumargs}

You can keep abstracting this out:

    reponames=( dag '$^livnastuff' )
    yumargs=( --disablerepo=$^reponames )
    livnastuff=( dries extras )
    yum ${(e)yumargs}

Or

    pkgnames=( kernel '$^livnapkgs' )
    reponames=( dag '$^livnarepos' )
    yumargs=( --exclude='${(e)^pkgnames}' --disablerepo='${(e)^reponames}' )
    livnarepos=( dries extras )
    yum ${(e)yumargs}


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

* Re: Forcing expansion without explicit eval nor a subshell?
  2005-11-05 21:33     ` Bart Schaefer
@ 2005-11-05 22:07       ` Lloyd Zusman
  2005-11-05 22:44         ` OT: poldek [Re: Forcing expansion without explicit eval nor a subshell?] GoTaR
  2005-11-06  3:02         ` Forcing expansion without explicit eval nor a subshell? Bart Schaefer
  0 siblings, 2 replies; 9+ messages in thread
From: Lloyd Zusman @ 2005-11-05 22:07 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer <schaefer@brasslantern.com> writes:

> On Nov 5,  3:03pm, Lloyd Zusman wrote:
> }
> } I want to defer it in case I do this:
> } 
> }   yumargs='--disablerepo={dag,$livnastuff}'
> } 
> } ... where $livnastuff can change, and where it might or might not
> } involve more brace expansions.
>
> The answer is still to stop thinking about brace expansions and start
> thinking about array expansions.
>
>     yumargs=( --disablerepo={dag,'$^livnastuff'} )
>     livnastuff=( dries extras )
>     yum ${(e)yumargs}
>
> You can keep abstracting this out:
>
>     reponames=( dag '$^livnastuff' )
>     yumargs=( --disablerepo=$^reponames )
>     livnastuff=( dries extras )
>     yum ${(e)yumargs}
>
> Or
>
>     pkgnames=( kernel '$^livnapkgs' )
>     reponames=( dag '$^livnarepos' )
>     yumargs=( --exclude='${(e)^pkgnames}' --disablerepo='${(e)^reponames}' )
>     livnarepos=( dries extras )
>     yum ${(e)yumargs}


Hmm ... OK.  I get it.  Thanks.

However, I'm still not sure how to do what I want in the array world,
because sometimes, there is nesting.

I'll explain:

This is what I often need to do these days, because the livna sites seem
to be down a lot:

  yum --disablerepo=livna \
      --disablerepo=livna-updates \
      --disablerepo=livna-extras \
      --disablerepo=livna-testing \
      update

This could be abreviated as such, in the brace-expansion world:

  yum --disablerepo=livna{,-updates,-extras,-testing} update

But sometimes, I also want to disable others, which would go like this:

  yum --disablerepo={dag,livna{,-updates,-extras,-testing}} update

Note the nesting of braces in this case.

Other times, livna is OK, but freshrpms is down:

  yum --disablerepo={dag,freshrpms} update

Therefore, sometimes there is nesting, and other times, there isn't.

I can't figure out how to do the nesting stuff using arrays, without
things getting rather complex.  The brace syntax handles nesting very
nicely.

But perhaps I'm still missing something about arrays ... ???


-- 
 Lloyd Zusman
 ljz@asfast.com
 God bless you.


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

* OT: poldek [Re: Forcing expansion without explicit eval nor a subshell?]
  2005-11-05 22:07       ` Lloyd Zusman
@ 2005-11-05 22:44         ` GoTaR
  2005-11-06  3:02         ` Forcing expansion without explicit eval nor a subshell? Bart Schaefer
  1 sibling, 0 replies; 9+ messages in thread
From: GoTaR @ 2005-11-05 22:44 UTC (permalink / raw)
  To: Lloyd Zusman; +Cc: zsh-users

On Sat, Nov 05, 2005 at 17:07:30 -0500, Lloyd Zusman wrote:

>   yum --disablerepo=livna{,-updates,-extras,-testing} update

Disgressing from subject: yum is to poldek like plain sh to zsh;)

http://poldek.pld.org.pl/

-- 
GoTaR <priv0.onet.pl->gotar>        http://vfmg.sourceforge.net/
                                    http://tccs.sourceforge.net/


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

* Re: Forcing expansion without explicit eval nor a subshell?
  2005-11-05 22:07       ` Lloyd Zusman
  2005-11-05 22:44         ` OT: poldek [Re: Forcing expansion without explicit eval nor a subshell?] GoTaR
@ 2005-11-06  3:02         ` Bart Schaefer
  2005-11-06  3:40           ` Lloyd Zusman
  1 sibling, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2005-11-06  3:02 UTC (permalink / raw)
  To: zsh-users

On Nov 5,  5:07pm, Lloyd Zusman wrote:
}
} I can't figure out how to do the nesting stuff using arrays, without
} things getting rather complex.

You do end up needing one array for each level of nesting.

E.g.:

    yumargs=( --disablerepo='${(e)^disablerepos}' )
    disablerepos=( dag '${(e)^livnarepos' )
    livnarepos=( livna{,-updates,-extras,-testing} )

    yum ${(e)yumargs}

This corresponds to your example

    yum --disablerepo={dag,livna{,-updates,-extras,-testing}}

The extra arrays are the price you pay for wanting to avoid defining
e.g. $livnarepos until the last minute and also avoid eval.

You might also ask yourself whether there's another way to get the end
result that you want, such as writing a wrapper function around yum.


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

* Re: Forcing expansion without explicit eval nor a subshell?
  2005-11-06  3:02         ` Forcing expansion without explicit eval nor a subshell? Bart Schaefer
@ 2005-11-06  3:40           ` Lloyd Zusman
  0 siblings, 0 replies; 9+ messages in thread
From: Lloyd Zusman @ 2005-11-06  3:40 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer <schaefer@brasslantern.com> writes:

> On Nov 5,  5:07pm, Lloyd Zusman wrote:
> }
> } I can't figure out how to do the nesting stuff using arrays, without
> } things getting rather complex.
>
> You do end up needing one array for each level of nesting.
>
> E.g.:
>
>     yumargs=( --disablerepo='${(e)^disablerepos}' )
>     disablerepos=( dag '${(e)^livnarepos' )
>     livnarepos=( livna{,-updates,-extras,-testing} )
>
>     yum ${(e)yumargs}
>
> This corresponds to your example
>
>     yum --disablerepo={dag,livna{,-updates,-extras,-testing}}
>
> The extra arrays are the price you pay for wanting to avoid defining
> e.g. $livnarepos until the last minute and also avoid eval.
>
> You might also ask yourself whether there's another way to get the end
> result that you want, such as writing a wrapper function around yum.

Thanks.

Well, after realizing that I have to use "eval" with brace expansion, I
went and wrote this wrapper function:

  function yum {
    eval /usr/bin/yum ${yumargs:-} '"$@"'
  }

... and I have these variables set:

  livna='livna{,-testing,-updates,-extras}'
  dag=dag
  dries=dries
  freshrpms=freshrpms
  newrpms=newrpms
  fedora='fedora-{core,extras}'

Then, I can do things like this:

  yumargs="--d={$dag,$livna}"  # --d is equivalent to --disablerepo
  yum update

This lets me change yumargs based on the availability of the
repositories, and then just issue normal "yum" commands.

And now I see how I can do pretty much the same thing with arrays.

Thanks again.


-- 
 Lloyd Zusman
 ljz@asfast.com
 God bless you.


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

end of thread, other threads:[~2005-11-06  3:44 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-05  4:01 Forcing expansion without explicit eval nor a subshell? Lloyd Zusman
2005-11-05  8:45 ` DervishD
2005-11-05 19:56 ` Bart Schaefer
2005-11-05 20:03   ` Lloyd Zusman
2005-11-05 21:33     ` Bart Schaefer
2005-11-05 22:07       ` Lloyd Zusman
2005-11-05 22:44         ` OT: poldek [Re: Forcing expansion without explicit eval nor a subshell?] GoTaR
2005-11-06  3:02         ` Forcing expansion without explicit eval nor a subshell? Bart Schaefer
2005-11-06  3:40           ` Lloyd Zusman

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