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

Replies are listed 'Best First'.
Re^2: Use reference for functions in functions
by 7stud (Deacon) on Feb 26, 2013 at 01:03 UTC

    Nice.

    In addition to the problems pointed out by BrowserUk and 7stud,

    Nope. You win.

    Problems with Reference Prototypes
    
    At some level, they're more predictable...But I'm afraid that these, too, may often be more trouble than they're worth.
    
    You see ... (\$) (\@) (\%) ... don't actually say that you must pass in a scalar reference, an array reference, and a hash reference. Rather, they say you must pass in a scalar variable, an array variable, and a hash variable. That means that the compiler insists upon seeing a properly notated variable of the given type, complete with ``$'', ``@'', or ``%'' in that slot ['slot' means the corresponding position in the argument list when you call the subroutine.] You must not use a backslash. The compiler silently supplies the backslash for you. 

    So given this sub:

    use strict; use warnings; use 5.012; sub do_stuff(\$) { my $sref = shift; say $$sref; }
    ...you can call the sub like this:
    my $str = 'hello'; do_stuff($str); #legal: no slash in position 1 of arg list --output:-- hello

    But the prototype prevents you from calling the sub like this:

    my $str = 'hello'; do_stuff(\$str); #illegal: slash in position 1 of arg list --output:-- Type of arg 1 to main::do_stuff must be scalar (not single ref constru +ctor)

    But the prototype doesn't stop you from doing this:

    my $str = \'hello'; do_stuff($str); #legal: no slash in position 1 of arg list --output:-- SCALAR(0x100839858)
    ...and then you end up with unintended consequences.
Re^2: Use reference for functions in functions
by T_I (Novice) on Feb 26, 2013 at 09:37 UTC

    Thanks for the pointer to the article. I'll read it and learn how to use prototyping correctly. I didn't choose IT as a profession just to skip everything I don't know how to do or use, but to learn new things. As of now, prototyping is going to be one of the latter.

    When doing something, it's worth doing it right.
    If it's not worth doing it right, it's not worth doing at all.

      The biggest thing to remember about prototypes is that you don't typically need them.

        I beg to differ. I found them very useful yesterday when I was rewriting a function from having 2 arguments to having 3. (and saving some lookups on the way) With proptotyping I was able to differentiate between both functions and have the code still functioning for my coworkers while I was optimizing the function. (and after rewrinting all function calls drop the old 2 argument sub)

        When you want to have the same function react differently based on the amount of arguments, by example while rewriting the function, it's useful. (unless you start shipping hashes/arrays and stuff like that to functions, then it gets interesting and to be honest a lot more fun)