fork download
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6. use 5.010;
  7.  
  8. # Parent
  9. package Foo {
  10. use strict;
  11. use warnings;
  12.  
  13. sub new { bless {}, shift }
  14. sub bar { 1448 }
  15. }
  16.  
  17. # Child
  18. package Bar {
  19. use strict;
  20. use warnings;
  21. our @ISA = qw/Foo/;
  22.  
  23. sub new { bless {}, shift }
  24. sub baz { 1337 }
  25. }
  26.  
  27. sub do_stuff {
  28. my Bar $instance = shift;
  29.  
  30. eval {
  31. say $instance -> bar;
  32. say $instance -> baz;
  33. }; if ($@) {
  34. say "Something went wrong: $@";
  35. say "Tested instance: ". (Dumper $instance);
  36. } else {
  37. say "OK";
  38. }
  39. }
  40.  
  41. my $foo = new Foo;
  42. my $bar = new Bar;
  43.  
  44. say 'Testing $foo';
  45. do_stuff $foo;
  46.  
  47. say 'Testing $bar';
  48. do_stuff $bar;
  49.  
Success #stdin #stdout 0s 21048KB
stdin
Standard input is empty
stdout
Testing $foo
1448
Something went wrong: Can't locate object method "baz" via package "Foo" at prog.pl line 32.

Tested instance: $VAR1 = bless( {}, 'Foo' );

Testing $bar
1448
1337
OK