Change 14878 by jhi@alpha on 2002/02/26 01:01:13

        Subject: [PATCH] Perl interface to newIO()
        From: [EMAIL PROTECTED] (Yitzchak Scott-Thoennes)      
        Date: Mon, 25 Feb 2002 15:04:57 -0800
        Message-ID: <[EMAIL PROTECTED]>

Affected files ...

.... //depot/perl/lib/Symbol.pm#11 edit
.... //depot/perl/lib/Symbol.t#3 edit

Differences ...

==== //depot/perl/lib/Symbol.pm#11 (text) ====
Index: perl/lib/Symbol.pm
--- perl/lib/Symbol.pm.~1~      Mon Feb 25 18:15:05 2002
+++ perl/lib/Symbol.pm  Mon Feb 25 18:15:05 2002
@@ -15,6 +15,12 @@
 
     ungensym $sym;      # no effect
 
+    # localize *FOO IO handle but not $FOO, %FOO, etc.
+    my $save_fooio = *FOO{IO} || geniosym;
+    *FOO = geniosym;
+    use_foo();
+    *FOO{IO} = $save_fooio;
+
     print qualify("x"), "\n";              # "Test::x"
     print qualify("x", "FOO"), "\n"        # "FOO::x"
     print qualify("BAR::x"), "\n";         # "BAR::x"
@@ -42,6 +48,10 @@
 support anonymous globs, C<Symbol::ungensym> is also provided.
 But it doesn't do anything.
 
+C<Symbol::geniosym> creates an anonymous IO handle.  This can be
+assigned into an existing glob without affecting the non-IO portions
+of the glob.
+
 C<Symbol::qualify> turns unqualified symbol names into qualified
 variable names (e.g. "myvar" -E<gt> "MyPackage::myvar").  If it is given a
 second parameter, C<qualify> uses it as the default package;
@@ -68,7 +78,7 @@
 require Exporter;
 @ISA = qw(Exporter);
 @EXPORT = qw(gensym ungensym qualify qualify_to_ref);
-@EXPORT_OK = qw(delete_package);
+@EXPORT_OK = qw(delete_package geniosym);
 
 $VERSION = 1.04;
 
@@ -89,6 +99,13 @@
     $ref;
 }
 
+sub geniosym () {
+    my $sym = gensym();
+    # force the IO slot to be filled
+    select(select $sym);
+    *$sym{IO};
+}
+
 sub ungensym ($) {}
 
 sub qualify ($;$) {

==== //depot/perl/lib/Symbol.t#3 (xtext) ====
Index: perl/lib/Symbol.t
--- perl/lib/Symbol.t.~1~       Mon Feb 25 18:15:05 2002
+++ perl/lib/Symbol.t   Mon Feb 25 18:15:05 2002
@@ -5,7 +5,7 @@
     @INC = '../lib';
 }
 
-use Test::More tests => 10;
+use Test::More tests => 14;
 
 BEGIN { $_ = 'foo'; }  # because Symbol used to clobber $_
 
@@ -26,6 +26,27 @@
 
 $sym1 = $sym2 = undef;
 
+# Test geniosym()
+
+use Symbol qw(geniosym);
+
+$sym1 = geniosym;
+like( $sym1, qr/=IO\(/, 'got an IO ref' );
+
+$FOO = 'Eymascalar';
+*FOO = $sym1;
+
+is( $sym1, *FOO{IO}, 'assigns into glob OK' );
+
+is( $FOO, 'Eymascalar', 'leaves scalar alone' );
+
+{
+    local $^W=1;               # 5.005 compat.
+    my $warn;
+    local $SIG{__WARN__} = sub { $warn .= "@_" };
+    readline FOO;
+    like( $warn, qr/unopened filehandle/, 'warns like an unopened filehandle' );
+}
 
 # Test qualify()
 package foo;
End of Patch.

Reply via email to