Re: (newbie) storing outputs from all lines of procs into a string
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: (newbie) storing outputs from all lines of procs into a string

From: Bryan Oakley <oakley@bardo.clearlight.com>
Date: Mon Dec 19 2005 - 18:49:20 CET

Andreas Leitgeb wrote:
> Bryan Oakley <oakley@bardo.clearlight.com> wrote:
>
>>Is there a reason you want to continually rename puts?
>
>
> yes, there is. Just read a few predecessor posts, if you're
> curious.
>
> For short: it's about having a special puts for just a
> limited part of the code, which is not under his control.
>

I'd probably still go with a single redefinition, with a traffic cop:

     rename puts _puts
     proc puts {args} {
        if {$::do_something_special} {
            <do the special stuff here>
        }
        eval _puts $args
     }
     proc whatever {} {
         set ::do_something_special 1
         puts "Hello, world"
         set ::do_something_special 0
     }

It's not perfect, but personally I prefer that over the constant
juggling of proc names.

Thinking out loud for a moment... another way to accomplish it is to use
interpreter aliases. Make 'puts' an alias either to the core puts or a
special one, and toggle between them as needed:

     rename puts core_puts
     proc my_puts {args} {...}
     proc whatever {} {
         # swap in special definition of puts
         interp alias {} puts {} my_puts

         <code that should use special form of puts>

         # swap in standard definition of puts
         interp alias {} puts {} core_puts
     }

I think that may make the code more self-evident, but it's arguable
which is truly better.
Received on Fri Dec 23 19:02:17 2005