From: Michael Stapelberg Date: Mon, 22 Mar 2010 20:37:44 +0000 (+0100) Subject: Get rid of _bytelength, use encode_utf8 and length instead. Correctly check for scalar X-Git-Tag: AnyEvent-0.02~10 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=07fc46deaf75dcdc110fc35f990cce98003fa650;p=i3%2Fi3 Get rid of _bytelength, use encode_utf8 and length instead. Correctly check for scalar --- diff --git a/lib/AnyEvent/I3.pm b/lib/AnyEvent/I3.pm index 7296a9c5..2addeb34 100644 --- a/lib/AnyEvent/I3.pm +++ b/lib/AnyEvent/I3.pm @@ -7,6 +7,7 @@ use JSON::XS; use AnyEvent::Handle; use AnyEvent::Socket; use AnyEvent; +use Encode; =head1 NAME @@ -76,12 +77,6 @@ my %events = ( output => ($event_mask | 1), ); -sub _bytelength { - my ($scalar) = @_; - use bytes; - length($scalar) -} - sub i3 { AnyEvent::I3->new(@_) } @@ -172,7 +167,7 @@ sub subscribe { my ($self, $callbacks) = @_; my $payload = encode_json [ keys %{$callbacks} ]; - my $message = $magic . pack("LL", _bytelength($payload), 2) . $payload; + my $message = $magic . pack("LL", length($payload), 2) . $payload; $self->{ipchdl}->push_write($message); # Register callbacks for each message type @@ -185,7 +180,8 @@ sub subscribe { =head2 $i3->message($type, $content) Sends a message of the specified C to i3, possibly containing the data -structure C, if specified. +structure C (or C, encoded as utf8, if C is a +scalar), if specified. my $reply = $i3->message(TYPE_COMMAND, "reload")->recv; if ($reply->{success}) { @@ -200,13 +196,14 @@ sub message { my $payload = ""; if ($content) { - if (ref($content) eq "SCALAR") { - $payload = $content; + if (not ref($content)) { + # Convert from Perl’s internal encoding to UTF8 octets + $payload = encode_utf8($content); } else { $payload = encode_json $content; } } - my $message = $magic . pack("LL", _bytelength($payload), $type) . $payload; + my $message = $magic . pack("LL", length($payload), $type) . $payload; $self->{ipchdl}->push_write($message); my $cv = AnyEvent->condvar;