Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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

In reply to Re: Use reference for functions in functions by AnomalousMonk
in thread Use reference for functions in functions by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (19)
    As of 2015-07-07 19:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (93 votes), past polls