about summary refs log tree commit
diff options
context:
space:
mode:
authorGraham Barr <gbarr@pobox.com>2007-08-23 00:43:24 +0000
committerGraham Barr <gbarr@pobox.com>2009-02-24 10:40:48 -0600
commit35d28d72ef1f1493ff1dbe949f7a6daeff8fab44 (patch)
treef37205f696918862915271e9ec04748bf66714d8
parent779dd028d04838ca947063463f240a7c3755095b (diff)
downloadperl-libnet-35d28d72ef1f1493ff1dbe949f7a6daeff8fab44.tar.gz
Better detection of utf8 string
-rw-r--r--Net/Cmd.pm23
1 files changed, 17 insertions, 6 deletions
diff --git a/Net/Cmd.pm b/Net/Cmd.pm
index a742860..4f0e444 100644
--- a/Net/Cmd.pm
+++ b/Net/Cmd.pm
@@ -22,7 +22,20 @@ BEGIN {
   }
 }
 
-my $doUTF8 = ($] > 5.008) && eval { require utf8 };
+BEGIN {
+  if (!eval { require utf8 }) {
+    *is_utf8 = sub { 0 };
+  }
+  elsif (eval { utf8::is_utf8(undef); 1 }) {
+    *is_utf8 = \&utf8::is_utf8;
+  }
+  elsif (eval { require Encode; Encode::is_utf8(undef); 1 }) {
+    *is_utf8 = \&Encode::is_utf8;
+  }
+  else {
+    *is_utf8 = sub { $_[0] =~ /[^\x00-\xff]/ };
+  }
+}
 
 $VERSION = "2.29";
 @ISA     = qw(Exporter);
@@ -386,11 +399,9 @@ sub datasend {
   my $arr  = @_ == 1 && ref($_[0]) ? $_[0] : \@_;
   my $line = join("", @$arr);
 
-  if ($doUTF8) {
-    # encode to individual utf8 bytes if
-    # $line is a string (in internal UTF-8)
-    utf8::encode($line) if utf8::is_utf8($line);
-  }
+  # encode to individual utf8 bytes if
+  # $line is a string (in internal UTF-8)
+  utf8::encode($line) if is_utf8($line);
 
   return 0 unless defined(fileno($cmd));