#!/usr/bin/env perl # Core Modules use strict; use warnings; use 5.010; use utf8; use open ':encoding(UTF-8)'; use open ':std'; use Carp; use Try::Tiny; use Data::Dumper; { my $name = qr/(? [^,]++ )/xms; my $weight = qr/(? \d++ )/xms; my $sex = qr/(? m|w )/xms; my %types = ( hund => qr/\A (? hund ) : $name , $weight , $sex \z/xms, katze => qr/\A (? katze ) : $name , $sex \z/xms, ); sub get_data { my ( $string ) = @_; # read type my $type; if ( $string =~ m/\A ([^:]++) : /xms ) { $type = $1; } else { croak "String [$string] is probably invalid"; } # get data from string if ( !exists $types{$type} ) { croak "Don't knew what to do with type [$type]."; } # return data $string =~ $types{$type}; return { %+ }; } } my @animals = ( "hund:bello,12,w", "katze:mini,m", "maus:Mickey,12,w", "muahahahaha", ); for my $animal ( @animals ) { try { my $data = get_data($animal); print Dumper($data); } catch { warn $_; }; }