Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

zsh at perl conference and few questions



hello people,

i'm happy to annonce i'll talk about zsh during the european edition of
the perl conference (glasgow)

http://act.perlconference.org/tpc-2018-glasgow/talk/7338

as i prepare my slides those days, some questions came to me. some of
these are (detailed below):

a) can someone tell me why isn't the "alternative" syntax more used ?
b) why the while loop can't take (( )) or single instruction as do list ?
c) it seems the (+) syntax can't be used outside file expansions
d) is there a plan to have something like namespaces ?

any comment/feedback/thought would be really appreciated.

regards
marc


a) can someone tell me why isn't the "alternative" syntax more used ?

i felt in love with zsh about 20 years now and one of the reason is
the alternative syntax. so can someone explain to me why the "old"
one seems to be prefered even nowdays ?

    for x in {1..20}; do
        print "$x * 2 = $[x * 2]"
    done

seems terrible to me compared to

    for x ({1..20}) print "$x * 2 = $[x * 2]"

as time passed, i added those alias in my .zshenv

    alias @='for it'
    alias @-='while {read it}'

so i daily (hourly ...) write those kind of thing

    @ (*.txt) gzip $it

b) why the while loop can't take (( )) or single instruction as do list ?

those works fine

    if {true} print ok
    if (( 2 == 2 )) print ok
    for ((count=3; count; count--)) print $count
    while ((count)) {print $[count--]}

so why not those ?

    while ((count)) print $[count--]
    while {read it} ((sum+=it))

c) it seems the (+) syntax can't be used outside file expansions
   (or did i miss something?)

as you can write

   by_word_count () REPLY=$( wc -w < $REPLY )
   print -l *.html(.o+by_word_count)

why shouldn't i write

   by_word_count () REPLY=$( wc -w < $REPLY )
   files=( *.html(.) )
   print -l ${(o+by_word_count)files}

or filter with something like

   large () (( $( wc -w < $REPLY ) > 200 ))
   files=( *.html(.) )
   print -l ${(+large)files}

d) is there a plan to have something like namespaces ?


using setopt pathdirs, you can put functions into files
in your path and reuse the name of it into the function
names. i use it to create a poor man namespace.

so if i have net/utils in my path i can fill it with

    net/utils/running\? ()
        ping -W 1 -c 1 ${1?ip or hostname to ping} \
        &> /dev/null

    net/utils/report () {
        local status="missing"
        net/utils/running\? ${1?ip or hostname to ping} &&
            status="online"
        print -u2 "$1 is $status"
    }

so now i can write

    $ . net/utils
    $ net/utils/report prometheus
    prometheus is online

in uze.zsh, i created something to export a function into the main
namespace (just copy the values of $functions )

so i can write

    $ . net/utils report
    $ prometheus
    prometheus is online

so for the user of the net/utils lib, that's fine ...
*but* when i write functions, i have to be careful about
the current namespace. the thing i dream about is the ability
to rewrite

    net/utils/report () {
        local status="missing"
        net/utils/running\? ${1?ip or hostname to ping} &&
            status="online"
        print -u2 "$1 is $status"
    }

as "the report function of this namespace"... if a // prefix
could exist, i could write something like

    //report () {
        local status="missing"
        //running\? ${1?ip or hostname to ping} &&
            status="online"
        print -u2 "$1 is $status"
    }

this should be awesome but i don't know if realistic as zsh
don't have syntax closure as well.



Messages sorted by: Reverse Date, Date, Thread, Author