| 1 | package SPVM::BlessedObject::Class; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 466 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 466 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 466 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 466 | ▶ | use base 'SPVM::BlessedObject'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 466 | ▶ | use SPVM::ExchangeAPI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 471 | our $AUTOLOAD; | |||||||||||||
| 12 | 213 | ▶ | sub AUTOLOAD { | ||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=7.1
| |||||||||||||||
| 13 | 213 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=7.1
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 0 | my $method_name = $AUTOLOAD; | |||||||||||||
| 16 | |||||||||||||||
| 17 | # If the class method is not found, AUTOLOAD is called. | ||||||||||||||
| 18 | 0 | ▶ | unless (ref $self) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 19 | 0 | my $basic_type_name = $self; | |||||||||||||
| 20 | 0 | $basic_type_name =~ s/^SPVM:://; | |||||||||||||
| 21 | 213 | $method_name =~ s/^.*:://; | |||||||||||||
| 22 | |||||||||||||||
| 23 | 213 | Carp::confess("$basic_type_name#$method_name method is not found"); | |||||||||||||
| 24 | } | ||||||||||||||
| 25 | |||||||||||||||
| 26 | # For a static instant method call | ||||||||||||||
| 27 | 213 | $method_name =~ s/^SPVM:://; | |||||||||||||
| 28 | |||||||||||||||
| 29 | # For an instance method call | ||||||||||||||
| 30 | 213 | $method_name =~ s/^BlessedObject::Class:://; | |||||||||||||
| 31 | |||||||||||||||
| 32 | 213 | my ($caller_class_name, $caller_file, $caller_line) = caller; | |||||||||||||
| 33 | 213 | my $caller_method_abs_name = "$caller_class_name#(Unknown)"; | |||||||||||||
| 34 | 213 | my $caller_info = SPVM::ExchangeAPI::CallerInfo->new(method_abs_name => $caller_method_abs_name, file => $caller_file, line => $caller_line); | |||||||||||||
| 35 | |||||||||||||||
| 36 | 213 | my $ret = $self->__api->call_method($self, $method_name, @_, $caller_info); | |||||||||||||
| 37 | |||||||||||||||
| 38 | 213 | return $ret; | |||||||||||||
| 39 | } | ||||||||||||||
| 40 | |||||||||||||||
| 41 | 471 | 1; | |||||||||||||
| 42 | |||||||||||||||
| 43 | =head1 Name | ||||||||||||||
| 44 | |||||||||||||||
| 45 | SPVM::BlessedObject::Class - SPVM Class | ||||||||||||||
| 46 | |||||||||||||||
| 47 | =head1 Description | ||||||||||||||
| 48 | |||||||||||||||
| 49 | The object of C<SPVM::BlessedObject::Class> class holds an instance of an SPVM class. | ||||||||||||||
| 50 | |||||||||||||||
| 51 | =head1 Usage | ||||||||||||||
| 52 | |||||||||||||||
| 53 | use SPVM 'Point'; | ||||||||||||||
| 54 | |||||||||||||||
| 55 | my $point = SPVM::Point->new; | ||||||||||||||
| 56 | $point->set_x(4); | ||||||||||||||
| 57 | my $x = $point->x; | ||||||||||||||
| 58 | $point->clear; | ||||||||||||||
| 59 | |||||||||||||||
| 60 | =head1 Instance Methods | ||||||||||||||
| 61 | |||||||||||||||
| 62 | =head2 AUTOLOAD | ||||||||||||||
| 63 | |||||||||||||||
| 64 | # Instance method call | ||||||||||||||
| 65 | my $ret = $blessed_object_class->foo(@args); | ||||||||||||||
| 66 | |||||||||||||||
| 67 | # Static instance method call | ||||||||||||||
| 68 | my $ret = $blessed_object_class->SPVM::MyClass::foo(@args); | ||||||||||||||
| 69 | |||||||||||||||
| 70 | Calls an SPVM instance method using L<SPVM::ExchangeAPI/"call_method"> with the arguments, and returns the return value. | ||||||||||||||
| 71 | |||||||||||||||
| 72 | The static instance method call is allowed. | ||||||||||||||
| 73 | |||||||||||||||
| 74 | If the class or the method is not found, an exception is thrown. | ||||||||||||||
| 75 | |||||||||||||||
| 76 | If the invocant cannnot be assigned to the class of the static method call, an exception is thrown. | ||||||||||||||
| 77 | |||||||||||||||
| 78 | Examples: | ||||||||||||||
| 79 | |||||||||||||||
| 80 | use SPVM 'Point'; | ||||||||||||||
| 81 | |||||||||||||||
| 82 | # Creates an SPVM::BlessedObject::Class object of the Point class. | ||||||||||||||
| 83 | my $point = Point->new; | ||||||||||||||
| 84 | |||||||||||||||
| 85 | # Calls instance methods in the Point class. | ||||||||||||||
| 86 | $point->set_x(4); | ||||||||||||||
| 87 | my $x = $point->x; | ||||||||||||||
| 88 | $point->clear; | ||||||||||||||
| 89 | |||||||||||||||
| 90 | =head1 Copyright & License | ||||||||||||||
| 91 | |||||||||||||||
| 92 | Copyright (c) 2023 Yuki Kimoto | ||||||||||||||
| 93 | |||||||||||||||
| 94 | MIT License | ||||||||||||||