How can I call methods on Perl scalars?

I saw some code that called methods on scalars (numbers), something like:

print 42->is_odd 

What do you need to overload so that you can achieve this kind of “functionality” in your code?

+4
source share
2 answers

Do you mean autobox ? See Also Should I use autobox in Perl? .

+10
source

This is an example of using the auto box function.

 #!/usr/bin/perl use strict; use warnings; package MyInt; sub is_odd { my $int = shift; return ($int%2); } package main; use autobox INTEGER => 'MyInt'; print "42: ".42->is_odd."\n"; print "43: ".43->is_odd."\n"; print "44: ".44->is_odd."\n"; 
+1
source

All Articles