zsh-users
 help / color / mirror / code / Atom feed
* autoload and alias expansion question
@ 2008-02-25 16:43 Peter Hegedus
  2008-02-25 17:17 ` Peter Stephenson
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Hegedus @ 2008-02-25 16:43 UTC (permalink / raw)
  To: zsh-users

Hello,

I have a question about alias expansion within autoloaded functions.
I'm working on a script library where I'd like to use global aliases,
but somehow can't get it to work.

To demonstrate:

hegedus@su20% echo $ZSH_VERSION
4.3.4
hegedus@su20% FPATH+=:.
hegedus@su20% alias ECHO=echo
hegedus@su20% w DATE
DATE not found
hegedus@su20% cat >foobar <<EOF
foobar () { }
alias DATE=date ; barfoo () { setopt aliases ; DATE ; ECHO OK }
EOF
hegedus@su20% autoload foobar
hegedus@su20% foobar
hegedus@su20% barfoo
barfoo: command not found: DATE
OK
hegedus@su20% w autoload
autoload: shell built-in command
hegedus@su20%

Please note I didn't use the -U flag to autoload. External aliases are
expanded, but those defined within the autoloaded script are not.
I tried using -k or -z flags, placing the definitions w/in the autoloaded
function, neither made a difference. Sourcing the file directly naturally works.

Do I miss something? Is there a way to make this work?

Thanks,
Peter


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

* Re: autoload and alias expansion question
  2008-02-25 16:43 autoload and alias expansion question Peter Hegedus
@ 2008-02-25 17:17 ` Peter Stephenson
  2008-02-25 18:40   ` Peter Hegedus
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2008-02-25 17:17 UTC (permalink / raw)
  To: zsh-users

"Peter Hegedus" wrote:
> hegedus@su20% cat >foobar <<EOF
> foobar () { }
> alias DATE=date ; barfoo () { setopt aliases ; DATE ; ECHO OK }
> EOF
> hegedus@su20% autoload foobar
> hegedus@su20% foobar
> hegedus@su20% barfoo
> barfoo: command not found: DATE
> OK

> Do I miss something?

No, you don't.  Aliases are expanded very early, during the initial
parsing of the code, so the DATE is reached and parsed before the alias
command is run.

> Is there a way to make this work?

You have two obvious options.

Firstly, most people when writing scripts or functions would use scripts
or functions at this point anyway, i.e. DATE() { date; }.  The main use
of aliases is as a hack at the command line to save typing; they are
rather less atractive if that's not an issue.

Second, you can put the alias definitions in a separate file and ensure
that that gets loaded before the function.  There are various ways of
doing this if that's what you really need.  Until we know why you're not
using a function for DATE it's hard to answer.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


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

* Re: autoload and alias expansion question
  2008-02-25 17:17 ` Peter Stephenson
@ 2008-02-25 18:40   ` Peter Hegedus
  2008-02-25 19:11     ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Hegedus @ 2008-02-25 18:40 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

Thanks for the quick answer!

I need it mostly for global aliases, to reduce clutter, for example

alias -g  DN='>/dev/null 2>&1'
alias -g  NE='2>/dev/null'
alias -g  LF='>>$_LF 2>&1'

The separate file approach works, thanks, I just needed to added an
extra autoload wherever that base library was loaded.

Thanks,
Peter

>"Peter Hegedus" wrote:
>> hegedus@su20% cat >foobar <<EOF
>> foobar () { }
>> alias DATE=date ; barfoo () { setopt aliases ; DATE ; ECHO OK }
>> EOF
>> hegedus@su20% autoload foobar
>> hegedus@su20% foobar
>> hegedus@su20% barfoo
>> barfoo: command not found: DATE
>> OK
>
>> Do I miss something?
>
>No, you don't.  Aliases are expanded very early, during the initial
>parsing of the code, so the DATE is reached and parsed before the alias
>command is run.
>
>> Is there a way to make this work?
>
>You have two obvious options.
>
>Firstly, most people when writing scripts or functions would use scripts
>or functions at this point anyway, i.e. DATE() { date; }.  The main use
>of aliases is as a hack at the command line to save typing; they are
>rather less atractive if that's not an issue.
>
>Second, you can put the alias definitions in a separate file and ensure
>that that gets loaded before the function.  There are various ways of
>doing this if that's what you really need.  Until we know why you're not
>using a function for DATE it's hard to answer.
>
>-- 
>Peter Stephenson <pws@csr.com>                  Software Engineer
>CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
>Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070
>


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

* Re: autoload and alias expansion question
  2008-02-25 18:40   ` Peter Hegedus
@ 2008-02-25 19:11     ` Bart Schaefer
  2008-02-25 21:40       ` Peter Hegedus
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2008-02-25 19:11 UTC (permalink / raw)
  To: Peter Hegedus; +Cc: zsh-users

On Feb 25,  1:40pm, Peter Hegedus wrote:
}
} I need it mostly for global aliases, to reduce clutter

This is almost never a good idea.

http://minnie.tuhs.org/UnixTree/V7/usr/src/cmd/sh/mac.h.html


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

* Re: autoload and alias expansion question
  2008-02-25 19:11     ` Bart Schaefer
@ 2008-02-25 21:40       ` Peter Hegedus
  2008-02-26  1:31         ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Hegedus @ 2008-02-25 21:40 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

Are you referring to the long-term supportability problem of macros
in general? Or something else?

For what it worth, this is for internal use only, and for a small group of
sysadmins and DBAs (2-3 people total).

Thanks,
Peter

>On Feb 25,  1:40pm, Peter Hegedus wrote:
>}
>} I need it mostly for global aliases, to reduce clutter
>
>This is almost never a good idea.
>
>http://minnie.tuhs.org/UnixTree/V7/usr/src/cmd/sh/mac.h.html


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

* Re: autoload and alias expansion question
  2008-02-25 21:40       ` Peter Hegedus
@ 2008-02-26  1:31         ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2008-02-26  1:31 UTC (permalink / raw)
  To: zsh-users

On Feb 25,  4:40pm, Peter Hegedus wrote:
}
} Are you referring to the long-term supportability problem of macros
} in general?

Essentially, yes.

It's actually a bit worse in a shell-like interpreted language with
loose syntax.  If a C #define is missing, your program probably won't
compile.  If a global alias doesn't get defined, your script runs anyway,
you pass an extra (or at least different) argument to some command, and
who knows what happens from there.


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

end of thread, other threads:[~2008-02-26  1:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-25 16:43 autoload and alias expansion question Peter Hegedus
2008-02-25 17:17 ` Peter Stephenson
2008-02-25 18:40   ` Peter Hegedus
2008-02-25 19:11     ` Bart Schaefer
2008-02-25 21:40       ` Peter Hegedus
2008-02-26  1:31         ` Bart Schaefer

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