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

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.
  • Comment on Re: Re: Passing function to a function using \&<function name> method

Replies are listed 'Best First'.
Re: Re: Re: Passing function to a function using \&<function name> method
by edan (Curate) on Feb 09, 2004 at 06:24 UTC

    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.

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

        You might want to look at your local (ActiveState?) perl documentation about the alarm function. Perhaps it's not implemented, or partially implemented? Or Google. Don't know what else to tell you.

        Do you know what makes this method playing with fire?

        Using symbolic references is generally considered a bit risky, unless you really know what you are doing, because (among other reasons) you can cause some very hard to find bugs when you start using them. Do a Super Search for 'symbolic references' (or 'refs', etc.) to find out what other Monks have to say about them. Here is a recent thread about them, to get you started: variables names used to define variables. Basically, they can be a handy feature, but only if you're careful, and there is frequently a better way to do things.

        --
        edan (formerly known as 3dan)

Re: Re: Re: Passing function to a function using \&<function name> method
by ysth (Canon) on Feb 09, 2004 at 08:50 UTC
    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.