in reply to Perl OOP Function Entrance

Here's an example. Note that you have to export the functions that you want to use without a fully qualified name, and your methods can't reference self (or, you have to check if $self is defined before using it, which means your code will operate differently in function or method mode).

use warnings; use strict; package Obj; use Exporter qw(import); our @EXPORT_OK = qw(foo); sub new { return bless {}, shift; } sub foo { my $self; if ($_[0] eq __PACKAGE__ || ref $_[0] eq __PACKAGE__){ $self = shift; } my ($x, $y) = @_; return $x * $y; }

The script:

use warnings; use strict; use feature 'say'; use lib '.'; use Obj qw(foo); my $o = Obj->new; say $o->foo(2, 2); say Obj::foo(2, 2); say foo(2, 2);

Output:

4 4 4