strange1 has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl script that I use to dial phone numbers. I use the code below to dial the number: $modem->dial( $number ) Which is fine, howeve I would like to have the ability to press a key to hangup the call when it is dialing or ringing. What would be the best way to achieve this? Note: I'm a novice when it comes to programming and perl is the only Language I have had any success in using. I know the code to make the modem hang up: $modem->hangup(); However I don't know how to execute "$modem->hangup();" when the perl scripts is busy executing "$modem->dial( $number )" Thanks

Replies are listed 'Best First'.
Re: Device::Modem Help
by ptum (Priest) on Apr 18, 2006 at 20:10 UTC

    Update: Sigh. You'd think I would actually read your question. Sorry.

    Have you tried using answer() or passing a timeout with your dial?


    If you're using Device::Modem then I think that a simple hangup() method call will do:

    hangup() Does what it is supposed to do. Hang up the phone thus terminating any active call. Usage:

    $ok = $modem->hangup();

    Welcome to the Monastery. You'll probably want to read up on a few of the tips for formatting questions, etc. It probably wouldn't hurt me to re-read those tips. :)


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
      Thanks for the reply and welcome.

      I don't think I have explained my problem very well.

      I would like to have the option to hangup the call by pressing a key while the modem is dialing.

      How do I interupt the  $modem->dial( $number); command and end it using the $modem->hangup(); command?

      I hope this explains it better?

      Thanks
        Hi

        I have used the timeout, however I would like to end the call with user interaction via the keyboard.
Re: Device::Modem Help
by zentara (Cardinal) on Apr 19, 2006 at 12:54 UTC
    It would be real easy if your dialer script, used Tk, Gtk2, POE, or some other event loop system. Why? Because you need to do 2 things at once, dial, and monitor for a key.

    For example, if you look at the following loop, a 'q' will stop it, but the 'print' is fast and the loop will be looping fast checking for the 'q'. In your case, if you try dialing instead of printing, the loop will not proceed, while the dial sub is running.

    #!/usr/bin/perl use Term::ReadKey; $char='q'; while(1){ ReadMode ('cbreak'); if (defined ($ch = ReadKey(-1))){ #input was waiting and it was $ch if ($ch eq $char){exit(0);} }else{ # no input was waiting #your program goes here print "############################\n"; select(undef,undef,undef,.01); } ReadMode ('normal'); # restore normal tty settings } __END__

    Now if you have an event lopp, like this Tk program, the dial can proceed, and you can interrupt it with a key press. This is just pseudo code, because I don't have the code you are using, but it should give you the idea.

    #!/usr/bin/perl use warnings; use strict; use Tk; #use Device::Modem; #my $modem = new Device::Modem( log =>'file,modemlog',port => '/dev/tt +yS1' ); my $mw = MainWindow->new; $mw->Label( -text => 'press q to quit dial' )->pack; $mw->Button(-text=> 'Start Dial', -command => \&start_dial )->pack; $mw->Button(-text=> 'Quit', -command => \&exit )->pack; $mw->bind( '<q>' => \&stop_dial ); MainLoop; sub start_dial{ print "dial started\n"; #you would do something like #$modem->dial('123-456-2345'); } ############################ sub stop_dial { print "q pressed\n"; # do something like # $modem->reset(); } __END__

    There are other Event modules, search cpan for them, or look at Roll your own Event-loop for ideas which will let you do it from a commandline script, without a GUI.


    I'm not really a human, but I play one on earth. flash japh
Re: Device::Modem Help
by BrowserUk (Patriarch) on Apr 20, 2006 at 00:43 UTC

    The obligatory simple (untested) solution:

    use Term::ReadKey; use threads; ... my $flag = 0; async{ $modem->hangup(), $flag = -1 if ReadKey 0.25; redo unless $flag }; if( $modem->dial( $number ) and not $flag ) { print "Modem connected"; $flag = 1; ## terminate thread } else { print "Dial failed:", $flag == -1 ? ' Terminated by user' : ''; }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Hi, Thanks for the reply browserUK. Could you explain or add some more comments to the code?

      Thanks