fork download
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. {
  6. use Scalar::Util;
  7. sub new { bless {} }
  8. use overload
  9. '.' => sub {
  10. my ($self, $value, $swapped) = @_;
  11. die unless $swapped;
  12. die unless Scalar::Util::looks_like_number($value);
  13. $self->{value} = $value;
  14. return $self;
  15. },
  16. '""' => sub {
  17. my $self = shift;
  18. return "$self->{value} Celsius";
  19. }
  20. }
  21.  
  22. sub C # This can be exported by the C module.
  23. {
  24. die if @_;
  25. C->new()
  26. }
  27.  
  28. my $T;
  29. eval { $T = C . 25 }; # Dies
  30. eval { $T = C .25; }; # Dies
  31. $T = 25 .C; # Makes a C object with the value 5
  32. print $T;
Success #stdin #stdout 0.01s 6308KB
stdin
Standard input is empty
stdout
25 Celsius