[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

The "loop and a half"

On 6 October 2017 at 13:56, bartc <bc at freeuk.com> wrote:
> If you don't like the word 'crude', try 'lazy'. Take this example of the gcc
> C compiler:
>  > gcc -E program.c
> This preprocesses the code and shows the result. Typical programs will have
> many thousands of lines of output, but it just dumps it to the console. You
> /have/ to use '>' to use it practically (Windows doesn't really have a
> working '|' system.)

No you don't. Ignoring the fact that "windows doesn't really have a
working '|' system" (which is an oversimplification, by the way) the
following all work:

    data = subprocess.check_output(["gcc", "-E", "program.c"])
    $x = (gcc -E program.c)
    for /f %i in ('gcc -E program.c') do ...

If gcc -E wrote its output to a file, you'd have to read that file,
manage the process of deleting it after use (and handle possible
deletion of it if an error occurred), etc.

Writing to stdout is very often a good design. Not always, but nothing
is ever 100% black and white, but sufficiently often that building an
OS based on the idea (Unix) was pretty successful :-)