Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^2: How to call a sub-routine ref

by greengaroo (Hermit)
on Oct 19, 2012 at 20:16 UTC ( #1000033=note: print w/ replies, xml ) Need Help??

in reply to Re: How to call a sub-routine ref
in thread How to call a sub-routine ref

Imagine this:




Don't you think the second could lead to confusions?

I'm just trying to find what is best, I honestly prefer the first but I've never seen it until recently, so I am thinking maybe no one uses it because it may be harder to understand at first sight.

There are no stupid questions, but there are a lot of inquisitive idiots.

Comment on Re^2: How to call a sub-routine ref
Select or Download Code
Replies are listed 'Best First'.
Re^3: How to call a sub-routine ref
by roboticus (Chancellor) on Oct 19, 2012 at 20:30 UTC


    Odd, I find your examples a good case for using the second one. Again, different people will find different things more clear.

    When I use hashes, I generally use $$h{foo} in preference to $h->{foo}, primarily because it's easier to type, and I use hashes quite a bit. If I were trying to make it visually distinctive, I'd make the harder to type one be for the less frequently used case. Thus, I'd use something like:


    To my eye, that stands out better. (But with my luck, it would blow up, so I'd better whip up a test...)

    $ cat use strict; use warnings; sub p { print "foo(", join(", ", @_), ")\n"; } my $hr = { key=>\&p }; $$hr{key}->('arg'); $ perl foo(arg)


    When your only tool is a hammer, all problems look like your thumb.

      I like the second rule in perlref.
      Anywhere you'd put an identifier (or chain of identifiers) as part of a variable or subroutine name, you can replace the identifier with a BLOCK returning a reference of the correct type.

      Sure, the result is often ugly, but there are no exceptions or special cases. What more could my poor memory want.

Re^3: How to call a sub-routine ref
by BrowserUk (Pope) on Oct 19, 2012 at 20:52 UTC

    For that particular case, you don't need either the -> or the &{}:

    sub x{ say "x got [ @_ ]"; };; $h{x} = \&x;; $href = \%h;; $href->{x}('fred','bill');; x got [ fred bill ]

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re^3: How to call a sub-routine ref
by ikegami (Pope) on Oct 19, 2012 at 21:52 UTC
    I find the first one much harder to read. The difference between ( and { is subtle, but properly named variables ($handlers->{$event_name}->($event)) would make that irrelevant.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000033]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2015-11-25 03:46 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (670 votes), past polls