Wednesday, November 16, 2011

Switch/case with regular expressions in Perl

Creating a switch/case in perl with regular expressions are the input, similar to bash' wildcarding.
sub adjusttype {
 my ($record) = @_;
SWITCH: {
  (
        $y[0] =~ /Local/
    or $y[0] =~ /Announc/
  ) && do { $record->{type} = 'Talk'; last SWITCH; };
  ( $y[0] =~ /and answers/ )
   && do { $record->{type} = 'Question and answers'; last SWITCH; };
  ( $y[0] )
   && do {    # strip the type from the title (if exists)
   $record->{title} =~ s/$y[0]//g;
   $record->{type} = $y[0];
   last SWITCH;
   };
 }

No comments:

Post a Comment