summaryrefslogtreecommitdiff
path: root/ext/List
diff options
context:
space:
mode:
authorGraham Barr <gbarr@pobox.com>2002-03-18 10:10:55 +0000
committerGraham Barr <gbarr@pobox.com>2002-03-18 10:10:55 +0000
commitc0f790df8cb21363579f99a3e816b2d7b6702278 (patch)
treecc5f2e255b6716831039ac767a830df919ba7db5 /ext/List
parentb47081478a8748d8ebb42a1f0afef81460bce6da (diff)
downloadperl-c0f790df8cb21363579f99a3e816b2d7b6702278.tar.gz
Sync with Scalar-List-Utils-1.07
p4raw-id: //depot/perl@15283
Diffstat (limited to 'ext/List')
-rw-r--r--ext/List/Util/ChangeLog8
-rw-r--r--ext/List/Util/lib/List/Util.pm2
-rw-r--r--ext/List/Util/lib/Scalar/Util.pm30
-rw-r--r--ext/List/Util/t/openhan.t33
4 files changed, 71 insertions, 2 deletions
diff --git a/ext/List/Util/ChangeLog b/ext/List/Util/ChangeLog
index 5ab668b155..934643ace1 100644
--- a/ext/List/Util/ChangeLog
+++ b/ext/List/Util/ChangeLog
@@ -1,3 +1,11 @@
+Change 713 on 2002/03/18 by <gbarr@pobox.com> (Graham Barr)
+
+ Add Scalar::Util::openhandle()
+
+Change 647 on 2001/09/18 by <gbarr@pobox.com> (Graham Barr)
+
+ Release 1.06
+
Change 645 on 2001/09/07 by <gbarr@pobox.com> (Graham Barr)
Some platforms require the main executable to export symbols
diff --git a/ext/List/Util/lib/List/Util.pm b/ext/List/Util/lib/List/Util.pm
index 91dbcdb7b6..1843873e5e 100644
--- a/ext/List/Util/lib/List/Util.pm
+++ b/ext/List/Util/lib/List/Util.pm
@@ -11,7 +11,7 @@ require DynaLoader;
our @ISA = qw(Exporter DynaLoader);
our @EXPORT_OK = qw(first min max minstr maxstr reduce sum shuffle);
-our $VERSION = "1.06_00";
+our $VERSION = "1.07_00";
bootstrap List::Util $VERSION;
diff --git a/ext/List/Util/lib/Scalar/Util.pm b/ext/List/Util/lib/Scalar/Util.pm
index 1329d1a48a..e518a4c445 100644
--- a/ext/List/Util/lib/Scalar/Util.pm
+++ b/ext/List/Util/lib/Scalar/Util.pm
@@ -10,9 +10,27 @@ require Exporter;
require List::Util; # List::Util loads the XS
our @ISA = qw(Exporter);
-our @EXPORT_OK = qw(blessed dualvar reftype weaken isweak tainted readonly);
+our @EXPORT_OK = qw(blessed dualvar reftype weaken isweak tainted readonly openhandle);
our $VERSION = $List::Util::VERSION;
+sub openhandle ($) {
+ my $fh = shift;
+ my $rt = reftype($fh) || '';
+
+ return defined(fileno($fh)) ? $fh : undef
+ if $rt eq 'IO';
+
+ if (reftype(\$fh) eq 'GLOB') { # handle openhandle(*DATA)
+ $fh = \(my $tmp=$fh);
+ }
+ elsif ($rt ne 'GLOB') {
+ return undef;
+ }
+
+ (tied(*$fh) or defined(fileno($fh)))
+ ? $fh : undef;
+}
+
1;
__END__
@@ -69,6 +87,16 @@ If EXPR is a scalar which is a weak reference the result is true.
weaken($ref);
$weak = isweak($ref); # true
+=item openhandle FH
+
+Returns FH if FH may be used as a filehandle and is open, or FH is a tied
+handle. Otherwise C<undef> is returned.
+
+ $fh = openhandle(*STDIN); # \*STDIN
+ $fh = openhandle(\*STDIN); # \*STDIN
+ $fh = openhandle(*NOTOPEN); # undef
+ $fh = openhandle("scalar"); # undef
+
=item readonly SCALAR
Returns true if SCALAR is readonly.
diff --git a/ext/List/Util/t/openhan.t b/ext/List/Util/t/openhan.t
new file mode 100644
index 0000000000..9eed5b9488
--- /dev/null
+++ b/ext/List/Util/t/openhan.t
@@ -0,0 +1,33 @@
+#!./perl
+
+BEGIN {
+ unless (-d 'blib') {
+ chdir 't' if -d 't';
+ @INC = '../lib';
+ require Config; import Config;
+ keys %Config; # Silence warning
+ if ($Config{extensions} !~ /\bList\/Util\b/) {
+ print "1..0 # Skip: List::Util was not built\n";
+ exit 0;
+ }
+ }
+}
+
+
+use Scalar::Util qw(openhandle);
+
+print "1..4\n";
+
+print "not " unless defined &openhandle;
+print "ok 1\n";
+
+my $fh = \*STDERR;
+print "not " unless openhandle($fh) == $fh;
+print "ok 2\n";
+
+print "not " unless fileno(openhandle(*STDERR)) == fileno(STDERR);
+print "ok 3\n";
+
+print "not " if openhandle(CLOSED);
+print "ok 4\n";
+