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. say "Something went wrong: [email protected]";
  34. say "Tested instance: ". (Dumper $instance);
  35. } else {
  36. say "OK";
  37. }
  38. }
  39.  
  40. my $foo = new Foo;
  41. my $bar = new Bar;
  42.  
  43. say 'Testing $foo';
  44. do_stuff $foo;
  45.  
  46. say 'Testing $bar';
  47. do_stuff $bar;
  48.  
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