On Tue, May 01, 2007 at 09:26:38AM -0700, Jonathan Lang wrote:
: On 5/1/07, brian d foy <[EMAIL PROTECTED]> wrote:
: >I was thinking about default filehandles yesterday. select() doesn't
: >seem to be around except as an "Unfiled" function in S16.
: >
: >Then, as I was looking at
: >
: >   .say( "Hello World" );
: 
: At various times, I have seen something to the effect of each of the
: following being bandied about:
: 
:  $*OUT.say( "Hello World" );
:  "Hello World".say;
: 
: That is, both filehandles and strings have 'say' methods that do
: essentially the same thing, but using subtly different syntax.  How
: would I use &("Hello World".say) to write to filehandle $FH?  My gut
: reaction would be to use an adverb for the purpose:
: 
:  "Hello World".say :to($FH);
: 
: This would also work for the sub version:
: 
:  say :to($FH) "Hello World";

Don't need a named arg, since we can simply be polymorphic on strings vs
filehandles, which thankfully do not overlap semantically:

    "Hello World".say($FH)
    $FH.say("Hello World")

: With this in mind, you probably could create a localized alias for
: 'say', if you wanted to:
: 
:  {
:    my &say := &OUTER::say.assuming(:to($FH));
:    say "Hello World"; # same as 'say "Hello World" :to($FH);'
:  }
: 
: The catch with this is that you'd have to do this for each output
: routine separately.

You don't always want lexical scoping for this sort of thing; you
often want dynamic scoping instead.

: How about this: Do the output routines default to the global
: filehandles directly, or do they default to lexical bindings of them?
: That is, does 'say' output to $*OUT in the absence of an explicit
: filehandle, or does it output to $OUT (with the latter normally being
: bound to $*OUT)?  If the latter, you should be able to redirect all of
: your output in the rest of the current scope by saying:
: 
:  $OUT := $*ERR;
: 
: I can understand not being able to rebind the global filehandles.
: After all: once they're rebound, how would you ever find what they
: were originally bound to?

The plan introduced in A06 was to leave $*('IN'|'OUT'|'ERR') bound
to stdin, stdout, and stderr (which can still be dickered with on
the POSIXy level, of course), and instead emulate p5's select(FH)
using a global variable $*DEFOUT for the default handle of print
(and now say as well).

I note that $*DEFOUT didn't make it into any of the synopses (though it
did make it to pugs/t/builtins/io/say_and_print.t, oddly), so I've just
added it to pugs/docs/Spec/IO.pod, the proto-synopsis on IO.

Anyway, you should (eventually) just be able to say

    {
        temp $*DEFOUT := $MYFH;
        foo();
    }

to redirect the default output of foo() and then restore at the end of the
dynamic scope.  That's an improvement over dancing the select twostep.

Larry

Reply via email to