| 1 | # This module is a base class for all Lingy object classes and base classes, | ||
| 2 | # and also a class for 'Lingy::Class' objects. | ||
| 3 | |||
| 4 | use strict; use warnings; | ||
| 5 | package Lingy::Class; | ||
| 6 | |||
| 7 | # This section is base class support for all Lingy object classes. | ||
| 8 | # They all inherit from this: | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | die sprintf "No 'new' method defined for class '%s'.", ref($_[0]); | ||
| 12 | } | ||
| 13 | |||
| 14 | sub NAME { | ||
| 15 | my ($self) = @_; | ||
| 16 | my $class = ref($self) or die; | ||
| 17 | $class =~ s/^Lingy::/lingy.lang./; | ||
| 18 | return $class; | ||
| 19 | } | ||
| 20 | |||
| 21 | my %common = map {($_, 1)} @Lingy::Common::EXPORT; | ||
| 22 | |||
| 23 | sub _method_names { | ||
| 24 | my ($self) = @_; | ||
| 25 | my $class = ref($self) || $self; | ||
| 26 | no strict 'refs'; | ||
| 27 | grep { | ||
| 28 | not( | ||
| 29 | exists($common{$_}) or | ||
| 30 | /(^_|^[A-Z]+$|can|import|isa|new)/ | ||
| 31 | ) | ||
| 32 | } keys %{"$class\::"}; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | # This section is for special Lingy::Class objects, which are needed to | ||
| 37 | # mimic Clojure class behavior. | ||
| 38 | |||
| 39 | use overload '""' => sub { | ||
| 40 | ref($_[0]) eq __PACKAGE__ ? ${$_[0]} : $_[0]; | ||
| 41 | }; | ||
| 42 | |||
| 43 | sub _new { | ||
| 44 | my (undef, $name) = @_; | ||
| 45 | bless \$name, __PACKAGE__; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub _name { | ||
| 49 | ref($_[0]) eq __PACKAGE__ or | ||
| 50 | die sprintf "Can't call '_name' on '%s' object", | ||
| 51 | ref($_[0]); | ||
| 52 | my $name = ${$_[0]}; | ||
| 53 | $name =~ s/^Lingy::/lingy.lang./; | ||
| 54 | return $name; | ||
| 55 | } | ||
| 56 | |||
| 57 | # Public methods: | ||
| 58 | sub isInstance { | ||
| 59 | my ($base_class, $instance) = @_; | ||
| 60 | my $instance_class = ref($instance); | ||
| 61 | $instance->isa($base_class) ? Lingy::Common::true() : Lingy::Common::false(); | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; |