in reply to Passing function to a function using \&<function name> method

Welcome to the Monastery!

Something along these lines:

timeout_func($timeout, \&myfunc, $arg1, $arg2); sub timeout_func { my ($timeout, $func, @args) = @_; # ... $func->(@args); }

HTH

FYI, that '\&<function name> mechanism' you're referring to is properly called a code reference (or 'coderef'). See perlref for more info.

--
3dan

Replies are listed 'Best First'.
Re: Re: Passing function to a function using \&<function name> method
by simonm (Vicar) on Dec 17, 2003 at 18:04 UTC
    $func->(@args);

    Or if your code needs to run on older versions of Perl, such as 5.005 (update: 5.003), use the old-fashioned equivalent syntax &$func(@args);.

Re: Re: Passing function to a function using \&<function name> method
by rich_d_thomas (Sexton) on Feb 08, 2004 at 15:23 UTC
    Hi 3dan,

    Thanks very much for this advice, sorry I did not reply soon after your advice, I've just been sooo busy on other things at the moment.

    This method works exactly how I wanted, which is really great.

    There are a few things I would like a bit of advice on.

    1) When I set it up to timeout on a system command using the `<system command>` method, the timeout does not work.

    If I use the system(<system command>) method, it does work.

    I have demonstrated this to myself by writing a short .bat script that I have deliberately just written:
    FTP <hostIPaddress>

    This means that it will ask the user for username and password.

    As control it not returned to perl, it's up to the timeout to get perl to gain control again after the timeout value.

    Does anyone know why the timeout does not work when I use the `<system command>` for performing system commands?

    2) In the timeout_func function, is there a way of getting the function name from the coderef somehow, so that when this times out, I can log the function name of the function that has taken longer than the timeout value?

    Thanks for all your help.
      2) In the timeout_func function, is there a way of getting the function name from the coderef somehow, so that when this times out, I can log the function name of the function that has taken longer than the timeout value?
      See Re: Getting name of sub from a hash-based dispatch table? for a function to convert a coderef to sub name.

      Does anyone know why the timeout does not work when I use the `<system command>` for performing system commands?

      No. I tested it with `cmd`, and it works fine for me. I am on Linux, and it sounds like you are on Win32, so maybe it's an OS thing with signals? *shrug*.

      In the timeout_func function, is there a way of getting the function name from the coderef somehow ... ?

      Not that I know of. You could get the name of the function from within the function itself with (caller 0)[3], but that won't help you in timeout_func (unless you set a package variable or something yucky like that). You could resort to using symbolic refs, though it's playing with fire...

      timeout_func($timeout, 'myfunc', ...); sub timeout_func { my ($timeout, $func_name, @args) = @_; # ... eval { # ... { no strict 'refs'; $func_name->(@args); } } if ($@) { die "$func_name died ($@)\n"; } }

      Notice that you pass the name of the function to call, instead of a hard reference to the function (the \&myfunc syntax).

      --
      edan (formerly known as 3dan)

        Hi edan,

        Thanks for your posting.

        I have tried both types of system command method and Linux and found the timeout to work. So it is a Windows32 problem.

        Do you know how I can find out more about the timeout not working when I use the `<system command>` method?

        I have read your suggestion about finding out the name of a function that has been passed into a function and am not sure on doing anything that is playing with fire.

        Do you know what makes this method playing with fire?

        Thank you. Richard Thomas.