http://www.perlmonks.org?node_id=1020584


in reply to Use reference for functions in functions

sub func1 (\$) { my $object=shift; $object->doSomething; }

In addition to the problems pointed out by BrowserUk and 7stud, the code quoted above from the OP includes another practice you'll probably want to drop at the earliest opportunity: using prototypes. (See Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen for a thorough discussion of many good reasons to avoid them unless you really know what they're doing.)

The  (\$) prototype in the quoted code causes a reference to a scalar to be passed to the function as an argument. This scalar was an object reference to begin with, so what you wind up with is a reference-to-a-reference. This reference-to-a-reference must then be de-referenced in order to use it to invoke a class method. E.g.,
    ${$object}->doSomething;
in the quoted code (assuming an otherwise-normal OO idiom). This is akin to putting your pants on over your head: it's a neat party trick, but probably not something you want to do every morning.

>perl -wMstrict -le "package Dog; ;; use List::Util qw(sum); ;; sub new { my $class = shift; my $self = {}; bless $self, $class; } ;; sub bark { printf 'woof '; } ;; sub do_math { my $self = shift; $self->bark for 1 .. sum @_; } ;; ;; package main; ;; sub func1 (\$) { my $obj = shift; ${$obj}->bark(); print qq{\n}; } ;; sub func2 (\$;@) { my $obj = shift; ${$obj}->do_math(@_); print qq{\n}; } ;; my $dog = Dog->new(); func1($dog); func2($dog, 3, 2, 1); " woof woof woof woof woof woof woof