[strongSwan-dev] [PATCH 1/4] vici-perl: Do not use Switch
Andreas Hofmeister
andi at collax.com
Thu Dec 17 14:56:12 CET 2015
Switch.pm, which was implemented as a source filter, has been deprecated in
Perl 5.10 and was later removed from the core modules in Perl 5.14 or so.
Unfortunately, its replacement, the given/when/default construct, has since
been downgraded to "experimental" status because of problems with the underlying
"smart-match" operator.
Thus, as of Perl 5.22, Perl still has no actually usable "switch"-like construct.
So just use boring, old and ugly "if/elsif/else" constructs instead, which are
compatible with almost any Perl version.
---
.../vici/perl/Vici-Session/lib/Vici/Message.pm | 161 ++++++++++-----------
.../vici/perl/Vici-Session/lib/Vici/Packet.pm | 119 +++++++--------
2 files changed, 127 insertions(+), 153 deletions(-)
diff --git a/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Message.pm b/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Message.pm
index 228431c..370046b 100644
--- a/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Message.pm
+++ b/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Message.pm
@@ -11,7 +11,6 @@ our $VERSION = '0.9';
use strict;
use warnings;
-use Switch;
use Vici::Transport;
use constant {
@@ -85,52 +84,46 @@ sub parse {
(my $key, $data) = unpack('C/a*a*', $data);
- switch ($type)
- {
- case KEY_VALUE
- {
- (my $value, $data) = unpack('n/a*a*', $data);
- $hash->{$key} = $value;
- }
- case SECTION_START
- {
- my %section = ();
- $data = parse($data, \%section);
- $hash->{$key} = \%section;
- }
- case LIST_START
- {
- my @list = ();
- my $more = 1;
+ if ( $type == KEY_VALUE )
+ {
+ (my $value, $data) = unpack('n/a*a*', $data);
+ $hash->{$key} = $value;
+ }
+ elsif ( $type == SECTION_START )
+ {
+ my %section = ();
+ $data = parse($data, \%section);
+ $hash->{$key} = \%section;
+ }
+ elsif ( $type == LIST_START )
+ {
+ my @list = ();
+ my $more = 1;
- while (length($data) > 0 and $more)
+ while (length($data) > 0 and $more)
+ {
+ (my $type, $data) = unpack('Ca*', $data);
+ if ( $type == LIST_ITEM )
{
- (my $type, $data) = unpack('Ca*', $data);
- switch ($type)
- {
- case LIST_ITEM
- {
- (my $value, $data) = unpack('n/a*a*', $data);
- push(@list, $value);
- }
- case LIST_END
- {
- $more = 0;
- $hash->{$key} = \@list;
- }
- else
- {
- die "message parsing error: ", $type, "\n"
- }
- }
+ (my $value, $data) = unpack('n/a*a*', $data);
+ push(@list, $value);
+ }
+ elsif ( $type == LIST_END )
+ {
+ $more = 0;
+ $hash->{$key} = \@list;
+ }
+ else
+ {
+ die "message parsing error: ", $type, "\n"
}
}
- else
- {
- die "message parsing error: ", $type, "\n"
- }
- }
- }
+ }
+ else
+ {
+ die "message parsing error: ", $type, "\n"
+ }
+ }
return $data;
}
@@ -141,31 +134,28 @@ sub encode_hash {
while ( (my $key, my $value) = each %$hash )
{
- switch (ref($value))
+ if ( ref($value) eq 'HASH' )
{
- case 'HASH'
- {
- $enc .= pack('CC/a*', SECTION_START, $key);
- $enc .= encode_hash($value);
- $enc .= pack('C', SECTION_END);
- }
- case 'ARRAY'
- {
- $enc .= pack('CC/a*', LIST_START, $key);
+ $enc .= pack('CC/a*', SECTION_START, $key);
+ $enc .= encode_hash($value);
+ $enc .= pack('C', SECTION_END);
+ }
+ elsif ( ref($value) eq 'ARRAY' )
+ {
+ $enc .= pack('CC/a*', LIST_START, $key);
- foreach my $item (@$value)
- {
- $enc .= pack('Cn/a*', LIST_ITEM, $item);
- }
- $enc .= pack('C', LIST_END);
- }
- else
+ foreach my $item (@$value)
{
- $enc .= pack('CC/a*n/a*', KEY_VALUE, $key, $value);
+ $enc .= pack('Cn/a*', LIST_ITEM, $item);
}
+ $enc .= pack('C', LIST_END);
+ }
+ else
+ {
+ $enc .= pack('CC/a*n/a*', KEY_VALUE, $key, $value);
}
}
- return $enc;
+ return $enc;
}
sub raw_hash {
@@ -185,38 +175,35 @@ sub raw_hash {
}
$raw .= $key;
- switch (ref($value))
+ if ( ref($value) eq 'HASH' )
{
- case 'HASH'
- {
- $raw .= '{' . raw_hash($value) . '}';
- }
- case 'ARRAY'
- {
- my $first_item = 1;
- $raw .= '[';
+ $raw .= '{' . raw_hash($value) . '}';
+ }
+ elsif ( ref($value) eq 'ARRAY' )
+ {
+ my $first_item = 1;
+ $raw .= '[';
- foreach my $item (@$value)
+ foreach my $item (@$value)
+ {
+ if ($first_item)
{
- if ($first_item)
- {
- $first_item = 0;
- }
- else
- {
- $raw .= ' ';
- }
- $raw .= $item;
+ $first_item = 0;
}
- $raw .= ']';
- }
- else
- {
- $raw .= '=' . $value;
+ else
+ {
+ $raw .= ' ';
+ }
+ $raw .= $item;
}
+ $raw .= ']';
+ }
+ else
+ {
+ $raw .= '=' . $value;
}
}
- return $raw;
+ return $raw;
}
1;
diff --git a/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Packet.pm b/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Packet.pm
index 62dac0a..45f657a 100644
--- a/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Packet.pm
+++ b/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Packet.pm
@@ -11,7 +11,6 @@ our $VERSION = '0.9';
use strict;
use warnings;
-use Switch;
use Vici::Message;
use Vici::Transport;
@@ -45,21 +44,18 @@ sub request {
my $response = $self->{'Transport'}->receive();
my ($type, $data) = unpack('Ca*', $response);
- switch ($type)
+ if ( $type == CMD_RESPONSE )
{
- case CMD_RESPONSE
- {
- return Vici::Message->from_data($data);
- }
- case CMD_UNKNOWN
- {
- die "unknown command '", $command, "'\n"
- }
- else
- {
- die "invalid response type\n"
- }
- };
+ return Vici::Message->from_data($data);
+ }
+ elsif ( $type == CMD_UNKNOWN )
+ {
+ die "unknown command '", $command, "'\n"
+ }
+ else
+ {
+ die "invalid response type\n"
+ }
}
sub register {
@@ -70,21 +66,18 @@ sub register {
my $response = $self->{'Transport'}->receive();
my ($type, $data) = unpack('Ca*', $response);
- switch ($type)
+ if ( $type == EVENT_CONFIRM )
{
- case EVENT_CONFIRM
- {
- return
- }
- case EVENT_UNKNOWN
- {
- die "unknown event '", $event, "'\n"
- }
- else
- {
- die "invalid response type\n"
- }
- };
+ return
+ }
+ elsif ( $type == EVENT_UNKNOWN )
+ {
+ die "unknown event '", $event, "'\n"
+ }
+ else
+ {
+ die "invalid response type\n"
+ }
}
sub unregister {
@@ -95,21 +88,18 @@ sub unregister {
my $response = $self->{'Transport'}->receive();
my ($type, $data) = unpack('Ca*', $response);
- switch ($type)
+ if ( $type == EVENT_CONFIRM )
{
- case EVENT_CONFIRM
- {
- return
- }
- case EVENT_UNKNOWN
- {
- die "unknown event '", $event, "'\n"
- }
- else
- {
- die "invalid response type\n"
- }
- };
+ return
+ }
+ elsif ( $type == EVENT_UNKNOWN )
+ {
+ die "unknown event '", $event, "'\n"
+ }
+ else
+ {
+ die "invalid response type\n"
+ }
}
sub streamed_request {
@@ -123,33 +113,30 @@ sub streamed_request {
my $more = 1;
my @list = ();
- while ($more)
- {
+ while ($more)
+ {
my $response = $self->{'Transport'}->receive();
my ($type, $data) = unpack('Ca*', $response);
- switch ($type)
+ if ( $type == EVENT )
+ {
+ (my $event_name, $data) = unpack('C/a*a*', $data);
+
+ if ($event_name eq $event)
+ {
+ my $msg = Vici::Message->from_data($data);
+ push(@list, $msg);
+ }
+ }
+ elsif ( $type == CMD_RESPONSE )
+ {
+ $self->unregister($event);
+ $more = 0;
+ }
+ else
{
- case EVENT
- {
- (my $event_name, $data) = unpack('C/a*a*', $data);
-
- if ($event_name eq $event)
- {
- my $msg = Vici::Message->from_data($data);
- push(@list, $msg);
- }
- }
- case CMD_RESPONSE
- {
- $self->unregister($event);
- $more = 0;
- }
- else
- {
- $self->unregister($event);
- die "invalid response type\n";
- }
+ $self->unregister($event);
+ die "invalid response type\n";
}
}
return \@list;
--
2.6.2
More information about the Dev
mailing list