fork download
  1. #!/usr/bin/perl
  2. package Point {
  3. my $_data = {};
  4.  
  5. sub new {
  6. my $class = shift;
  7. $_data = { @_ };
  8. bless $_data, $class;
  9. };
  10.  
  11. use overload '""' => sub { shift->_stringify() };
  12.  
  13. sub _stringify {
  14. my $self = shift;
  15. return sprintf 'A point at (%d, %d)', $self->{x}, $self->{y};
  16. }
  17. }
  18.  
  19. my $p = Point->new(x => 5, y => 10);
  20. print $p;
Success #stdin #stdout 0.01s 5580KB
stdin
Standard input is empty
stdout
A point at (5, 10)