| 1 | #!/usr/local/bin/perl | ||
| 2 | ################################################################################ | ||
| 3 | |||
| 4 | ### Scenario 1: Extending an Object with Module Methods | ||
| 5 | |||
| 6 | package MyMethods; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use Exporter 'import'; | ||
| 11 | our @EXPORT = qw(increment_value display_value); | ||
| 12 | |||
| 13 | sub increment_value { | ||
| 14 | my ($self, $key) = @_; | ||
| 15 | $self->{$key}++ if exists $self->{$key}; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub display_value { | ||
| 19 | my ($self, $key) = @_; | ||
| 20 | print "$key: ", $self->{$key} // 'undefined', "\n"; | ||
| 21 | } | ||
| 22 | |||
| 23 | ############################################################################ | ||
| 24 | |||
| 25 | package MyObject; | ||
| 26 | |||
| 27 | sub new { | ||
| 28 | my $class = shift; | ||
| 29 | my $self = bless { count => 0 }, $class; | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | ############################################################################ | ||
| 34 | |||
| 35 | package main; | ||
| 36 | |||
| 37 | use strict; | ||
| 38 | use warnings; | ||
| 39 | use Extender; | ||
| 40 | |||
| 41 | my $object = MyObject->new(); | ||
| 42 | |||
| 43 | # Extend the object with methods from MyMethods | ||
| 44 | Extend($object, 'MyMethods'); | ||
| 45 | |||
| 46 | # Using extended methods | ||
| 47 | $object->increment_value('count'); | ||
| 48 | $object->display_value('count'); # Outputs: count: 1 | ||
| 49 | |||
| 50 | $object->increment_value('count'); | ||
| 51 | $object->display_value('count'); # Outputs: count: 2 | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | ### Scenario 2: Adding Custom Methods to an Object | ||
| 56 | |||
| 57 | package MyDataStructure; | ||
| 58 | |||
| 59 | sub new { | ||
| 60 | my $class = shift; | ||
| 61 | my $self = bless { data => [] }, $class; | ||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | ############################################################################ | ||
| 66 | |||
| 67 | package main; | ||
| 68 | |||
| 69 | use Extender; | ||
| 70 | |||
| 71 | my $object = MyDataStructure->new(); | ||
| 72 | Extends($object, | ||
| 73 | add_data => sub { my ($self, $item) = @_; push @{$self->{data}}, $item; }, | ||
| 74 | get_data => sub { my $self = shift; return @{$self->{data}}; }, | ||
| 75 | ); | ||
| 76 | |||
| 77 | # Using custom methods | ||
| 78 | $object->add_data('Item 1'); | ||
| 79 | $object->add_data('Item 2'); | ||
| 80 | my @data = $object->get_data(); | ||
| 81 | print "Data: @data\n"; # Outputs: Data: Item 1 Item 2 | ||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | ### Scenario 3: Extending Multiple Objects with Different Modules | ||
| 86 | |||
| 87 | package MyMethods; | ||
| 88 | |||
| 89 | use strict; | ||
| 90 | use warnings; | ||
| 91 | use Exporter 'import'; | ||
| 92 | our @EXPORT = qw(increment_value display_value); | ||
| 93 | |||
| 94 | sub increment_value { | ||
| 95 | my ($self, $value) = @_; | ||
| 96 | $$self += $value; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub display_value { | ||
| 100 | my ($self) = @_; | ||
| 101 | print "Value: $$self\n"; | ||
| 102 | } | ||
| 103 | |||
| 104 | ############################################################################ | ||
| 105 | |||
| 106 | package MyClass1; | ||
| 107 | |||
| 108 | sub new { | ||
| 109 | my $class = shift; | ||
| 110 | my $self = bless \0, $class; | ||
| 111 | return $self; | ||
| 112 | } | ||
| 113 | |||
| 114 | ############################################################################ | ||
| 115 | |||
| 116 | package MyClass2; | ||
| 117 | |||
| 118 | sub new { | ||
| 119 | my $class = shift; | ||
| 120 | my $self = bless \0, $class; | ||
| 121 | return $self; | ||
| 122 | } | ||
| 123 | |||
| 124 | ############################################################################ | ||
| 125 | |||
| 126 | package main; | ||
| 127 | |||
| 128 | use strict; | ||
| 129 | use warnings; | ||
| 130 | use Extender; | ||
| 131 | use MyMethods; | ||
| 132 | |||
| 133 | my $object1 = MyClass1->new(); | ||
| 134 | my $object2 = MyClass2->new(); | ||
| 135 | |||
| 136 | # Extend $object1 with MyMethods | ||
| 137 | Extends($object1, | ||
| 138 | increment_value => \&MyMethods::increment_value, | ||
| 139 | display_value => \&MyMethods::display_value, | ||
| 140 | ); | ||
| 141 | |||
| 142 | # Extend $object2 with MyMethods | ||
| 143 | Extends($object2, | ||
| 144 | increment_value => \&MyMethods::increment_value, | ||
| 145 | display_value => \&MyMethods::display_value, | ||
| 146 | ); | ||
| 147 | |||
| 148 | # Using extended methods | ||
| 149 | $object1->increment_value(10); | ||
| 150 | $object1->display_value(); # Outputs: Value: 10 | ||
| 151 | |||
| 152 | $object2->increment_value(20); | ||
| 153 | $object2->display_value(); # Outputs: Value: 20 | ||
| 154 | |||
| 155 | 1; | ||
| 156 | |||
| 157 | ### Scenario 4: Adding Methods to Raw References (Hash, Array, Scalar) | ||
| 158 | |||
| 159 | ############################################################################ | ||
| 160 | |||
| 161 | # PACKAGES | ||
| 162 | |||
| 163 | package HashMethods; | ||
| 164 | |||
| 165 | use strict; | ||
| 166 | use warnings; | ||
| 167 | use Exporter 'import'; | ||
| 168 | our @EXPORT = qw(set get); | ||
| 169 | |||
| 170 | sub set { | ||
| 171 | my ($self, $key, $value) = @_; | ||
| 172 | $self->{$key} = $value; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub get { | ||
| 176 | my ($self, $key) = @_; | ||
| 177 | return $self->{$key}; | ||
| 178 | } | ||
| 179 | |||
| 180 | 1; | ||
| 181 | |||
| 182 | ############################################################################ | ||
| 183 | |||
| 184 | package ArrayMethods; | ||
| 185 | |||
| 186 | use strict; | ||
| 187 | use warnings; | ||
| 188 | use Exporter 'import'; | ||
| 189 | our @EXPORT = qw(add get); | ||
| 190 | |||
| 191 | sub add { | ||
| 192 | my ($self, $item) = @_; | ||
| 193 | push @$self, $item; | ||
| 194 | } | ||
| 195 | |||
| 196 | sub get { | ||
| 197 | my ($self, $index) = @_; | ||
| 198 | return $self->[$index]; | ||
| 199 | } | ||
| 200 | |||
| 201 | 1; | ||
| 202 | |||
| 203 | ############################################################################ | ||
| 204 | |||
| 205 | package ScalarMethods; | ||
| 206 | |||
| 207 | use strict; | ||
| 208 | use warnings; | ||
| 209 | use Exporter 'import'; | ||
| 210 | our @EXPORT = qw(set get substr length); | ||
| 211 | |||
| 212 | sub set { | ||
| 213 | my ($self, $value) = @_; | ||
| 214 | $$self = $value; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub get { | ||
| 218 | my ($self) = @_; | ||
| 219 | return $$self; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub substr { | ||
| 223 | my $self = shift; | ||
| 224 | return substr($$self, @_); | ||
| 225 | } | ||
| 226 | |||
| 227 | sub length { | ||
| 228 | my ($self) = @_; | ||
| 229 | return length $$self; | ||
| 230 | } | ||
| 231 | |||
| 232 | 1; | ||
| 233 | |||
| 234 | ############################################################################ | ||
| 235 | |||
| 236 | # MAIN | ||
| 237 | |||
| 238 | package main; | ||
| 239 | |||
| 240 | use strict; | ||
| 241 | use warnings; | ||
| 242 | use Extender; | ||
| 243 | use HashMethods; | ||
| 244 | use ArrayMethods; | ||
| 245 | use ScalarMethods; | ||
| 246 | |||
| 247 | my $hash_object = {}; | ||
| 248 | my $array_object = []; | ||
| 249 | my $scalar_object = \""; | ||
| 250 | |||
| 251 | # Extend $hash_object with methods from HashMethods | ||
| 252 | Extend($hash_object, 'HashMethods', 'set', 'get'); | ||
| 253 | |||
| 254 | # Extend $array_object with methods from ArrayMethods | ||
| 255 | Extend($array_object, 'ArrayMethods', 'add', 'get'); | ||
| 256 | |||
| 257 | # Extend $scalar_object with methods from ScalarMethods | ||
| 258 | Extend($scalar_object, 'ScalarMethods', 'set', 'get', 'substr', 'length'); | ||
| 259 | |||
| 260 | # Using extended methods for hash object | ||
| 261 | $hash_object->set('key', 'value'); | ||
| 262 | print $hash_object->get('key'), "\n"; # Outputs: value | ||
| 263 | |||
| 264 | # Using extended methods for array object | ||
| 265 | $array_object->add('item1'); | ||
| 266 | $array_object->add('item2'); | ||
| 267 | print $array_object->get(0), "\n"; # Outputs: item1 | ||
| 268 | |||
| 269 | # Using extended methods for scalar object | ||
| 270 | $scalar_object->set('John'); | ||
| 271 | print $scalar_object->get(), "\n"; # Outputs: John | ||
| 272 | print $scalar_object->length(), "\n"; # Outputs: 4 | ||
| 273 | print $scalar_object->substr(1, 2), "\n"; # Outputs: oh | ||
| 274 | $scalar_object->substr(1, 2, "ane"); | ||
| 275 | print $scalar_object->get(), "\n"; # Outputs: Jane | ||
| 276 | |||
| 277 | 1; | ||
| 278 | |||
| 279 | ### Scenario 5: Using Reference to Scalar with Code Ref | ||
| 280 | |||
| 281 | package main; | ||
| 282 | |||
| 283 | use Extender; | ||
| 284 | |||
| 285 | my $object = {}; | ||
| 286 | my $code_ref = sub { my ($self, $arg) = @_; return "Hello, $arg!"; }; | ||
| 287 | |||
| 288 | # Extenda $object with a method using reference to scalar with code ref | ||
| 289 | Extends($object, 'greet' => \$code_ref); | ||
| 290 | |||
| 291 | # Using extended method | ||
| 292 | print $object->greet('Alice'), "\n"; # Outputs: Hello, Alice! | ||
| 293 | |||
| 294 | 1; | ||
| 295 | |||
| 296 | ### Scenario 6: Used on shared variables | ||
| 297 | |||
| 298 | package main; | ||
| 299 | |||
| 300 | use strict; | ||
| 301 | use warnings; | ||
| 302 | use threads; | ||
| 303 | use threads::shared; | ||
| 304 | use Extender; | ||
| 305 | |||
| 306 | # Example methods to manipulate shared data | ||
| 307 | |||
| 308 | ############################################################################ | ||
| 309 | |||
| 310 | # Method to set data in a shared hash | ||
| 311 | sub set_hash_data { | ||
| 312 | my ($self, $key, $value) = @_; | ||
| 313 | lock(%{$self}); | ||
| 314 | $self->{$key} = $value; | ||
| 315 | } | ||
| 316 | |||
| 317 | # Method to get data from a shared hash | ||
| 318 | sub get_hash_data { | ||
| 319 | my ($self, $key) = @_; | ||
| 320 | lock(%{$self}); | ||
| 321 | return $self->{$key}; | ||
| 322 | } | ||
| 323 | |||
| 324 | ############################################################################ | ||
| 325 | |||
| 326 | # Method to add item to a shared array | ||
| 327 | sub add_array_item { | ||
| 328 | my ($self, $item) = @_; | ||
| 329 | lock(@{$self}); | ||
| 330 | push @{$self}, $item; | ||
| 331 | } | ||
| 332 | |||
| 333 | # Method to get item from a shared array | ||
| 334 | sub get_array_item { | ||
| 335 | my ($self, $index) = @_; | ||
| 336 | lock(@{$self}); | ||
| 337 | return $self->[$index]; | ||
| 338 | } | ||
| 339 | |||
| 340 | ############################################################################ | ||
| 341 | |||
| 342 | # Method to set data in a shared scalar | ||
| 343 | sub set_scalar_data { | ||
| 344 | my ($self, $value) = @_; | ||
| 345 | lock(${$self}); | ||
| 346 | ${$self} = $value; | ||
| 347 | } | ||
| 348 | |||
| 349 | # Method to get data from a shared scalar | ||
| 350 | sub get_scalar_data { | ||
| 351 | my ($self) = @_; | ||
| 352 | lock(${$self}); | ||
| 353 | return ${$self}; | ||
| 354 | } | ||
| 355 | |||
| 356 | ############################################################################ | ||
| 357 | |||
| 358 | # Create shared data structures | ||
| 359 | my %shared_hash :shared; | ||
| 360 | my @shared_array :shared; | ||
| 361 | my $shared_scalar :shared; | ||
| 362 | |||
| 363 | # Create shared objects | ||
| 364 | my $shared_hash_object = \%shared_hash; | ||
| 365 | my $shared_array_object = \@shared_array; | ||
| 366 | my $shared_scalar_object = \$shared_scalar; | ||
| 367 | |||
| 368 | ############################################################################ | ||
| 369 | |||
| 370 | # Extend the shared hash object with custom methods | ||
| 371 | Extends($shared_hash_object, | ||
| 372 | set_hash_data => \&set_hash_data, | ||
| 373 | get_hash_data => \&get_hash_data, | ||
| 374 | ); | ||
| 375 | |||
| 376 | # Extend the shared array object with custom methods | ||
| 377 | Extends($shared_array_object, | ||
| 378 | add_array_item => \&add_array_item, | ||
| 379 | get_array_item => \&get_array_item, | ||
| 380 | ); | ||
| 381 | |||
| 382 | # Extend the shared scalar object with custom methods | ||
| 383 | Extends($shared_scalar_object, | ||
| 384 | set_scalar_data => \&set_scalar_data, | ||
| 385 | get_scalar_data => \&get_scalar_data, | ||
| 386 | ); | ||
| 387 | |||
| 388 | ############################################################################ | ||
| 389 | |||
| 390 | # Create threads to manipulate shared objects concurrently | ||
| 391 | |||
| 392 | # Thread for shared hash object | ||
| 393 | my $hash_thread = threads->create(sub { | ||
| 394 | $shared_hash_object->set_hash_data('key1', 'value1'); | ||
| 395 | print "Hash thread: key1 = " . $shared_hash_object->get_hash_data('key1') . "\n"; | ||
| 396 | }); | ||
| 397 | |||
| 398 | # Thread for shared array object | ||
| 399 | my $array_thread = threads->create(sub { | ||
| 400 | $shared_array_object->add_array_item('item1'); | ||
| 401 | print "Array thread: item at index 0 = " . $shared_array_object->get_array_item(0) . "\n"; | ||
| 402 | }); | ||
| 403 | |||
| 404 | # Thread for shared scalar object | ||
| 405 | my $scalar_thread = threads->create(sub { | ||
| 406 | $shared_scalar_object->set_scalar_data('shared_value'); | ||
| 407 | print "Scalar thread: value = " . $shared_scalar_object->get_scalar_data() . "\n"; | ||
| 408 | }); | ||
| 409 | |||
| 410 | ############################################################################ | ||
| 411 | |||
| 412 | # Wait for all threads to finish | ||
| 413 | $hash_thread->join(); | ||
| 414 | $array_thread->join(); | ||
| 415 | $scalar_thread->join(); | ||
| 416 | |||
| 417 | 1; | ||
| 418 | |||
| 419 | # Alias - The Alias function creates an alias for an existing method on an object. | ||
| 420 | |||
| 421 | package MyClass; | ||
| 422 | |||
| 423 | sub new { | ||
| 424 | my $class = shift; | ||
| 425 | my $self = bless {}, $class; | ||
| 426 | return $self; | ||
| 427 | } | ||
| 428 | |||
| 429 | sub original_method { | ||
| 430 | return "Original method"; | ||
| 431 | } | ||
| 432 | |||
| 433 | ############################################################################ | ||
| 434 | |||
| 435 | package main; | ||
| 436 | |||
| 437 | use Extender; | ||
| 438 | |||
| 439 | my $object = MyClass->new(); | ||
| 440 | |||
| 441 | # Create an alias for 'original_method' as 'alias_method' | ||
| 442 | Alias($object, 'alias_method', 'original_method'); | ||
| 443 | |||
| 444 | # Using the alias method | ||
| 445 | print $object->alias_method(), "\n"; # Outputs: Original method | ||
| 446 | |||
| 447 | 1; | ||
| 448 | |||
| 449 | # Unload - The Unload function removes a method from an object. | ||
| 450 | |||
| 451 | package MyClass; | ||
| 452 | |||
| 453 | sub new { | ||
| 454 | my $class = shift; | ||
| 455 | my $self = bless {}, $class; | ||
| 456 | return $self; | ||
| 457 | } | ||
| 458 | |||
| 459 | sub method1 { | ||
| 460 | return "Method 1"; | ||
| 461 | } | ||
| 462 | |||
| 463 | sub method2 { | ||
| 464 | return "Method 2"; | ||
| 465 | } | ||
| 466 | |||
| 467 | ############################################################################ | ||
| 468 | |||
| 469 | package main; | ||
| 470 | |||
| 471 | use Extender; | ||
| 472 | |||
| 473 | my $object = MyClass->new(); | ||
| 474 | |||
| 475 | # Before Unload | ||
| 476 | print $object->method1(), "\n"; # Outputs: Method 1 | ||
| 477 | print $object->method2(), "\n"; # Outputs: Method 2 | ||
| 478 | |||
| 479 | # Unload 'method1' | ||
| 480 | Unload($object, 'method1'); | ||
| 481 | |||
| 482 | # After Unload | ||
| 483 | # 'method1' should not be available | ||
| 484 | eval { | ||
| 485 | $object->method1(); | ||
| 486 | }; | ||
| 487 | warn "Error: $@" if $@; # Outputs: Error: Can't locate object method "method1" via package | ||
| 488 | |||
| 489 | # 'method2' should still be available | ||
| 490 | print $object->method2(), "\n"; # Outputs: Method 2 | ||
| 491 | |||
| 492 | 1; | ||
| 493 | |||
| 494 | # AddMethod - The AddMethod function dynamically adds a new method to an object. | ||
| 495 | |||
| 496 | package MyClass; | ||
| 497 | |||
| 498 | sub new { | ||
| 499 | my $class = shift; | ||
| 500 | my $self = bless {}, $class; | ||
| 501 | return $self; | ||
| 502 | } | ||
| 503 | |||
| 504 | ############################################################################ | ||
| 505 | |||
| 506 | package main; | ||
| 507 | |||
| 508 | use Extender; | ||
| 509 | |||
| 510 | my $object = MyClass->new(); | ||
| 511 | |||
| 512 | # Add a new method 'new_method' | ||
| 513 | AddMethod($object, 'new_method', sub { return "New method"; }); | ||
| 514 | |||
| 515 | # Using the added method | ||
| 516 | print $object->new_method(), "\n"; # Outputs: New method | ||
| 517 | |||
| 518 | 1; | ||
| 519 | |||
| 520 | # Decorate - The Decorate function wraps an existing method on an object with additional behavior. | ||
| 521 | |||
| 522 | package MyClass; | ||
| 523 | |||
| 524 | sub new { | ||
| 525 | my $class = shift; | ||
| 526 | my $self = bless {}, $class; | ||
| 527 | return $self; | ||
| 528 | } | ||
| 529 | |||
| 530 | sub original_method { | ||
| 531 | return "Original method"; | ||
| 532 | } | ||
| 533 | |||
| 534 | ############################################################################ | ||
| 535 | |||
| 536 | package main; | ||
| 537 | |||
| 538 | use Extender; | ||
| 539 | |||
| 540 | my $object = MyClass->new(); | ||
| 541 | |||
| 542 | # Decorate 'original_method' | ||
| 543 | Decorate($object, 'original_method', sub { | ||
| 544 | my ($self, $original, @args) = @_; | ||
| 545 | return "Before: " . $original->($self, @args) . " After"; | ||
| 546 | }); | ||
| 547 | |||
| 548 | # Using the decorated method | ||
| 549 | print $object->original_method(), "\n"; # Outputs: Before: Original method After | ||
| 550 | |||
| 551 | 1; | ||
| 552 | |||
| 553 | # ApplyRole - The ApplyRole function applies methods from a role package to an object. | ||
| 554 | |||
| 555 | package TestRole; | ||
| 556 | |||
| 557 | sub apply { | ||
| 558 | my ($class, $object) = @_; | ||
| 559 | no strict 'refs'; | ||
| 560 | *{$object . "::new_method"} = sub { return "New method"; }; | ||
| 561 | } | ||
| 562 | |||
| 563 | ############################################################################ | ||
| 564 | |||
| 565 | package MyClass; | ||
| 566 | |||
| 567 | sub new { | ||
| 568 | my $class = shift; | ||
| 569 | my $self = bless {}, $class; | ||
| 570 | return $self; | ||
| 571 | } | ||
| 572 | |||
| 573 | ############################################################################ | ||
| 574 | |||
| 575 | package main; | ||
| 576 | |||
| 577 | use Extender; | ||
| 578 | |||
| 579 | my $object = MyClass->new(); | ||
| 580 | |||
| 581 | # Apply 'TestRole' to $object | ||
| 582 | ApplyRole($object, 'TestRole'); | ||
| 583 | |||
| 584 | # Using the applied method | ||
| 585 | print $object->new_method(), "\n"; # Outputs: New method | ||
| 586 | |||
| 587 | 1; | ||
| 588 | |||
| 589 | # InitHook - The InitHook function attaches initialization and destruction hooks to an object. | ||
| 590 | |||
| 591 | package TestObject; | ||
| 592 | |||
| 593 | sub new { return bless {}, shift } | ||
| 594 | |||
| 595 | sub DESTROY { | ||
| 596 | my $self = shift; | ||
| 597 | # Implement destruction logic if needed | ||
| 598 | } | ||
| 599 | |||
| 600 | ############################################################################ | ||
| 601 | |||
| 602 | package main; | ||
| 603 | |||
| 604 | use Extender; | ||
| 605 | |||
| 606 | InitHook('TestObject', 'INIT', sub { | ||
| 607 | print "Initializing object\n"; | ||
| 608 | }); | ||
| 609 | |||
| 610 | InitHook('TestObject', 'DESTRUCT', sub { | ||
| 611 | print "Destructing object\n"; | ||
| 612 | }); | ||
| 613 | |||
| 614 | my $object = TestObject->new(); # Output: Initializing object | ||
| 615 | undef $object; # Output: Destructing object | ||
| 616 | |||
| 617 | 1; | ||
| 618 | |||
| 619 | # Creating Extender Class objects from any (even shared) reference typed variable except for CODE refrerences | ||
| 620 | |||
| 621 | package main; | ||
| 622 | |||
| 623 | use Extender; | ||
| 624 | |||
| 625 | my $object = Extend({},'Extender'); | ||
| 626 | $object->Extends( method => sub { return "method"; } ); | ||
| 627 | print $object->method(), "\n"; # Outputs: method | ||
| 628 | |||
| 629 | my $array = Extend([],'Extender'); | ||
| 630 | $array->Extends( method => sub { return "method"; } ); | ||
| 631 | print $array->method(), "\n"; # Outputs: method | ||
| 632 | |||
| 633 | my $scalar = Extend(\"",'Extender'); | ||
| 634 | $scalar->Extends( method => sub { return "method"; } ); | ||
| 635 | print $scalar->method(), "\n"; # Outputs: method | ||
| 636 | |||
| 637 | my $glob = Extend(\*GLOB,'Extender'); | ||
| 638 | $glob->Extends( method => sub { return "method"; } ); | ||
| 639 | print $glob->method(), "\n"; # Outputs: method | ||
| 640 | |||
| 641 | 1; | ||
| 642 | |||
| 643 | # Creating an STDERR Logger with decorative functionalities | ||
| 644 | |||
| 645 | use strict; | ||
| 646 | use warnings; | ||
| 647 | use Time::Piece; | ||
| 648 | |||
| 649 | ############################################################################ | ||
| 650 | # BaseLogger.pm | ||
| 651 | package BaseLogger; | ||
| 652 | use strict; | ||
| 653 | use warnings; | ||
| 654 | |||
| 655 | sub new { | ||
| 656 | my $class = shift; | ||
| 657 | return bless {}, $class; | ||
| 658 | } | ||
| 659 | |||
| 660 | sub log { | ||
| 661 | my ($self, $message) = @_; | ||
| 662 | print STDERR $message; | ||
| 663 | } | ||
| 664 | |||
| 665 | 1; | ||
| 666 | |||
| 667 | ############################################################################ | ||
| 668 | # LoggerDecorators.pm | ||
| 669 | package LoggerDecorators; | ||
| 670 | |||
| 671 | use strict; | ||
| 672 | use warnings; | ||
| 673 | use Time::Piece; | ||
| 674 | |||
| 675 | # Timestamp decorator | ||
| 676 | sub add_timestamp { | ||
| 677 | my ($logger) = @_; | ||
| 678 | return sub { | ||
| 679 | my ($self, $message) = @_; | ||
| 680 | my $timestamp = localtime->strftime('%Y-%m-%d %H:%M:%S'); | ||
| 681 | $logger->($self, "[$timestamp] $message"); | ||
| 682 | }; | ||
| 683 | } | ||
| 684 | |||
| 685 | # Log level decorator | ||
| 686 | sub add_log_level { | ||
| 687 | my ($logger, $level) = @_; | ||
| 688 | return sub { | ||
| 689 | my ($self, $message) = @_; | ||
| 690 | $logger->($self, "[$level] $message"); | ||
| 691 | }; | ||
| 692 | } | ||
| 693 | |||
| 694 | 1; | ||
| 695 | |||
| 696 | ############################################################################ | ||
| 697 | # Example.pl | ||
| 698 | |||
| 699 | package main; | ||
| 700 | use strict; | ||
| 701 | use warnings; | ||
| 702 | use BaseLogger; | ||
| 703 | use LoggerDecorators; | ||
| 704 | |||
| 705 | # Create an instance of BaseLogger | ||
| 706 | my $logger = BaseLogger->new(); | ||
| 707 | |||
| 708 | # Create a decorated logger | ||
| 709 | my $decorated_logger = sub { | ||
| 710 | my ($self, $message) = @_; | ||
| 711 | $logger->log($message); | ||
| 712 | }; | ||
| 713 | |||
| 714 | # Apply decorators to extend logging functionality | ||
| 715 | $decorated_logger = add_timestamp($decorated_logger); | ||
| 716 | $decorated_logger = add_log_level($decorated_logger, 'INFO'); | ||
| 717 | |||
| 718 | # Helper function to capture STDERR output | ||
| 719 | sub capture_stderr { | ||
| 720 | my ($code) = @_; | ||
| 721 | my $output; | ||
| 722 | { | ||
| 723 | open my $stderr_backup, '>&', STDERR or die "Cannot backup STDERR: $!"; | ||
| 724 | open STDERR, '>', \$output or die "Cannot redirect STDERR: $!"; | ||
| 725 | |||
| 726 | $code->(); | ||
| 727 | |||
| 728 | open STDERR, '>&', $stderr_backup or die "Cannot restore STDERR: $!"; | ||
| 729 | close $stderr_backup; | ||
| 730 | } | ||
| 731 | return $output; | ||
| 732 | } | ||
| 733 | |||
| 734 | # Capture logging output | ||
| 735 | my $stderr_output = capture_stderr(sub { | ||
| 736 | $decorated_logger->("This is a test message\n"); | ||
| 737 | }); | ||
| 738 | |||
| 739 | # Output captured log | ||
| 740 | print "Captured STDERR output:\n"; | ||
| 741 | print $stderr_output; | ||
| 742 | |||
| 743 | 1; | ||
| 744 | |||
| 745 | # Extending Decorating and Aliasing class objects | ||
| 746 | |||
| 747 | use strict; | ||
| 748 | use warnings; | ||
| 749 | use Extender; | ||
| 750 | |||
| 751 | # Declare the class with a namespace and version ########################### | ||
| 752 | class MyApp::Logger 1.0 { | ||
| 753 | |||
| 754 | # Constructor method | ||
| 755 | method new { | ||
| 756 | my $class = shift; | ||
| 757 | return bless {}, $class; | ||
| 758 | } | ||
| 759 | |||
| 760 | # Method with attributes | ||
| 761 | method log : Private { | ||
| 762 | my ($self, $message) = @_; | ||
| 763 | print STDERR $message; | ||
| 764 | } | ||
| 765 | |||
| 766 | # Public method to log an info message | ||
| 767 | method log_info { | ||
| 768 | my ($self, $message) = @_; | ||
| 769 | $self->log("[INFO] $message\n"); | ||
| 770 | } | ||
| 771 | |||
| 772 | # Public method to log an error message | ||
| 773 | method log_error { | ||
| 774 | my ($self, $message) = @_; | ||
| 775 | $self->log("[ERROR] $message\n"); | ||
| 776 | } | ||
| 777 | |||
| 778 | } | ||
| 779 | |||
| 780 | # Usage example | ||
| 781 | package main; | ||
| 782 | |||
| 783 | # Create a new Logger object ############################################### | ||
| 784 | my $logger = Extend( | ||
| 785 | MyApp::Logger->new(), | ||
| 786 | 'Extender' | ||
| 787 | ) | ||
| 788 | ->Extends( | ||
| 789 | 'log_warn', sub { | ||
| 790 | my ($self, $message) = @_; | ||
| 791 | $self->log("[WARN] $message\n"); | ||
| 792 | } | ||
| 793 | ) | ||
| 794 | ->Alias( | ||
| 795 | 'log_error', 'log_err' | ||
| 796 | ) | ||
| 797 | ->Decorate( | ||
| 798 | 'log_info', | ||
| 799 | sub { | ||
| 800 | my ($self, $original, @args) = @_; | ||
| 801 | # Add extra behavior before and after | ||
| 802 | my $result = $original->($self, @args); | ||
| 803 | $self->log("[DECORATED] $args[0]\n"); | ||
| 804 | return $result; | ||
| 805 | } | ||
| 806 | ); | ||
| 807 | |||
| 808 | # Use the extended functionalities | ||
| 809 | $logger->log_info("This is an informational message."); | ||
| 810 | $logger->log_warn("This is a warning message."); | ||
| 811 | $logger->log_err("This is an error message."); | ||
| 812 | |||
| 813 | # Testing the decorated method | ||
| 814 | $logger->log_info("This message is decorated."); |