Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Yet another does-function-exist question

by xaprb (Scribe)
on Apr 14, 2007 at 15:44 UTC ( [id://610073]=note: print w/replies, xml ) Need Help??


in reply to Re: Yet another does-function-exist question
in thread Yet another does-function-exist question

I'm trying to do the opposite of what you're suggesting. I explicitly don't want to call it if it's a function.

$func = "format_hard_drive"; print "Good news, function exists!" if eval { &$func() };

Replies are listed 'Best First'.
Re^3: Yet another does-function-exist question
by f00li5h (Chaplain) on Apr 15, 2007 at 05:30 UTC

    You can do it one of two ways.

    1. Check that the thing is valid before hand (as ref $coderef in my class below does)
    2. Check when you call that the result is valid (as the trigger_event sub will do if you remove the test in the first point)

    At the risk of getting a TL;DR, here's something that takes coderefs and then dispatches events with them, as I wrote this, I realised that it's surprisingly long for a simple example ;)

    This is the class that takes care of dispatching the events (to a single handler for each event type)

    This is the client code, that registeres event handlers (to be used as callbacks) and triggers the actual events on the object. Normally, the object would find it's own events, and handle them within a run() type method (Tl's MainLoop for example)

    The class will reject 'antelope' because it's not a real coderef, although if you were to pass in 'main::antelope' as the name, and remove the check for ref $coderef, you should be able to use just names of subs. I don't see why you would want to do this, since it's the same number of characters to write \& as it is to enclose the sub's name quote likes

    Justification: this does answer your question about checking if it's callable before hand (because it uses ref $subref to decide if it can be callled (long before the events actually turn up). This is a fairly painless way to check if something is callable, you can also use anonymous subs if you want

    Hope this helps.

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
      $ perl -le 'print ref (\&no_such_sub);' CODE Useless test.

        Oh, my!

        @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-18 03:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found