Sum data type: Difference between revisions

Added Perl example
(Added Perl example)
Line 255:
Naming a tag can be omitted, but then introspection, i. e. retrieving which alternative is “active”, can not be done.
A <tt>record</tt> can have at most one variant part, which has to appear next to the ''end'' of the <tt>record</tt> definition.
 
=={{header|Perl}}==
No native type in Perl for this, use a filter to enforce the rules.
<lang perl>use strict;
use warnings;
use feature 'say';
 
sub filter {
my($text) = @_;
if (length($text)>1 and $text eq reverse $text) {
return 1, 'Palindromic';
} elsif (0 == length(($text =~ s/\B..*?\b ?//gr) =~ s/^(.)\1+//r)) {
return 1, 'Alliterative';
}
return 0, 'Does not compute';
}
 
for my $text ('otto', 'ha ha', 'a', 'blue skies', 'tiptoe through the tulips', 12321) {
my($status,$message) = analyze $text;
printf "%s $message\n", $status ? 'Yes' : 'No ';
}</lang>
{{out}}
<pre>Yes Palindromic
Yes Alliterative
No Does not compute
No Does not compute
Yes Alliterative
Yes Palindromic</pre>
 
=={{header|Perl 6}}==
2,392

edits