Re: Tcl Switch case



On 19 feb, 10:02, "MartinLemburg@Siemens-PLM"
<martin.lemburg.siemens-...@xxxxxxx> wrote:
Hi Andreas,

while willing to agree, that this 8.5(.x) change (it works in 8.5.0,
too) equalizes the behaviour of such commands like lsearch, switch,
etc.:

    % lsearch -hello *hello
    0
    % switch -hello {-hello {expr {0}}}
    0

But I'm not willing to accept it as feature if syntactical elements
are handled like you showed:

    % switch -- {-- {expr {0}}}
    0

This is definitely (IMHO) an error, because "--" signals the end of
the options and that the next argument is the string to be used with
the switch cases.

It should behave like e.g. ...

    % glob --; # could have returned an empty list
    wrong # args: should be "glob ?switches? name ?name ...?"

    % regexp -- ----; # could have return 1
    wrong # args: should be "regexp ?switches? exp string ?matchVar? ?
subMatchVar subMatchVar ...?"

    % regsub -- ---- !; # could have returned "!!"
    wrong # args: should be "regsub ?switches? exp string subSpec ?
varName?"

So - no matter how far switch is nearer to e.g. lsearch in its syntax,
it is now much far away from the syntax of "--" accepting commands.

Best regards,

Martin

P.S.: I never understood, why such syntactical differences are between
e.g. lsort and e.g. switch. The first accepts any "-\w+" pattern as
data, if it is no valid option, the last throws an error if an "-\w+"
argument is not a valid option. What's about equalizing option
accepting commands?

On 18 Feb., 21:30, Andreas Leitgeb <a...@xxxxxxxxxxxxxxxxxxxxxxxx>
wrote:

Donald Arseneau <a...@xxxxxxxxx> wrote:
On Feb 18, 7:43 am, Mark Janssen <mpc.jans...@xxxxxxxxx> wrote:
Or use the stable Tcl 8.5.1. 8.5.1 recognizes that with [switch $a
$body], $a cannot be a flag so it will not try to interpret it as one..
WHAT!?!?  That breaks Tcl's parser rules.

nothing actually does.

The $a must be substituted
before the switch command has any effect -- it (switch) can only see
the resulting text that came out of $a.

What you say is true, but who spoke against it?

switch -- {-- {puts ok}}

Of course also prints "ok" to stdout (with 8.5.1 and up), so no
discrimination between $,[] and constant values is happening.
It's the number of args, that prevents "switch $a $body" from
taking $a for an option, not whether they're constant or not.

I certainly agree that the option handling of Tcl commands is a bit
inconsistent between commands. One of the things I would like to see
addressed in Tcl9 is making option handling in Tcl more uniform and
providing an easier way to add the same handling in your own procs.

I was thinking about using for instance a dict to pass options, but I
am not sure I really like the syntax that would lead to e.g.

lsearch {-inline 1 -all 1} list pattern

all commands would then have the general form:

command ?options? arg1 arg2 arg3 .....

This also has the advantage of removing the need for -- to signify the
end of the options. Determining if any options have been specified
reduces to simple argument counting for all commands.

Mark
.



Relevant Pages

  • Re: Tcl Switch case
    ... too) equalizes the behaviour of such commands like lsearch, switch, ...
    (comp.lang.tcl)
  • Re: Tcl Switch case
    ... too) equalizes the behaviour of such commands like lsearch, switch, ...
    (comp.lang.tcl)
  • Re: Reintroducing fish, the friendly interactive shell
    ... Kurt Swanson skrev: ... the writers of all the commands to implement this would be infeasible. ... specific switch, in which case val and flag must also be 0. ... int has_arg; ...
    (comp.unix.shell)
  • Re: expect and log_user
    ... a telnet interface) to get the list of which machine (actually which MAC ... address) is on which port of which switch. ... there are no more pages I issue a sequence of "q" commands). ... I suggest you to replace your first pattern something like: ...
    (comp.lang.tcl)
  • RE: Physical ports in IOS
    ... That way you know which commands and features are ... FE ports on the switch. ... Try 'show ip interface brief', ... what port the switch learned it from. ...
    (Pen-Test)