Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Canon concerning coderef calls?

by revdiablo (Prior)
on Nov 15, 2004 at 22:40 UTC ( [id://407995]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Canon concerning coderef calls?
in thread Canon concerning coderef calls?

Chromatic's OO way

I wouldn't really call that an OO way. Sure, it uses can as a class method on main, but that's where the OOishness ends. It returns a plain coderef and executes it with a different dereferencing syntax than you used. I believe it is also the only alternative that does not use a symbolic reference.

To reinforce the point about being a plain coderef, this works:

foreach (@list) { my $coderef = main->can($_); &$coderef($_); }

And this does too, if you really want to get rid of all the pointy arrows:

foreach (@list) { my $coderef = UNIVERSAL::can('main', $_); &$coderef($_); }

Update: and if you want to see what I would consider the "OO way":

foreach (@list) { main->$_($_); }

But, frankly, I think that's very ugly. It uses a symbolic reference too. I would use can as chromatic originally did.

Replies are listed 'Best First'.
Re^4: Canon concerning coderef calls?
by NetWallah (Canon) on Nov 16, 2004 at 03:28 UTC
    I agree with all your notes, revdiablo.

    I particularly like the self-documenting "UNIVERSAL::can" call, although research shows that the "can" method (or allegedly exported sub) does not really exist in any module - documentation says it is built into perl.

        Earth first! (We'll rob the other planets later)

      research shows that the "can" method (or allegedly exported sub) does not really exist in any module - documentation says it is built into perl

      I suppose it all depends on what you mean by "built into perl." It's true that it's part the perl core, and can be relied on being there. But that's only because UNIVERSAL is the base class for all classes. If you run perldoc -f can, or browse through perlfunc, you will see that can is not a builtin function in the strict sense of the term.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found