Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My CT script...will this work? 1

Status
Not open for further replies.

MTputerman

Technical User
Jan 2, 2003
7
US
I'm reviving a previous post...I let it die but still need to work on the problem.

I have written this script:

Code:
 sub getit()

 {

 # $value = "";

 foreach (1..9)

 {

 $input = $ctport->collect(1,10);  # get 1 digit...zeros get collected, 
 but don't get assigned to variable

 $ctport->play("$input");  # echo the digit...zeros don't echo

 $value .= $input; 

 next;

 }

 $value;  # return value

The problem is that the sub never returns zeros that I enter. ie entering 1010 would return 11.

justice41 (member) had this reply:

what does the collect() subroutine look like? this is probably where the problem is. A zero evaluates to false so if a variable holds a zero and is evaluated in boolean context (as in an if statement) then the statement returns false even though the variable holds a valid value.

Posting the collect() subroutine would be helpful in tracking down the problem.


Here is where the previous post died. Here is the code:
Code:
 /*--------------------------------------------------------------------------*
 FUNCTION....: ctcollect
 AUTHOR......: David Rowe
 DATE CREATED: 10/10/01

 Collect digits handler.

\*--------------------------------------------------------------------------*/

void ctcollect(int h, int newSd) {
  char      s[VPB_MAX_STR];
  int       state, ret, rc;
  char      line[MAX_MSG];
  VPB_EVENT e;
  int       digits;
  char      buf[VPB_MAX_STR];

  // read digits
  memset(line,0x0,MAX_MSG);
  read_line(newSd,line);
  digits = atoi(line);

  // read time out in seconds
  memset(line,0x0,MAX_MSG);
  read_line(newSd,line);
  int unsigned long seconds = atol(line);

  // read inter digit time out in seconds
  memset(line,0x0,MAX_MSG);
  read_line(newSd,line);
  int unsigned long inter_seconds = atol(line);

  VPB_DIGITS d = {"", digits, seconds*SEC2MS, inter_seconds*SEC2MS};
  vpb_get_digits_async(h, &d, buf);

  state = 0;
  do {
    ret = vpb_get_event_ch_async(h, &e);
    if (ret == VPB_OK)
    {
      vpb_translate_event(&e, s); s[strlen(s)-1]=0;
      mylog(LOG_INFO,"%s",s);

      if (e.type == VPB_DIGIT)
      {
 state = 1;
 sprintf(s, "%s\n", buf);
 rc = send(newSd, s, strlen(s)+1, 0);
      }

      if (e.type == VPB_TONEDETECT) 
      {
         if(e.data == VPB_BUSY)
  {
      state = 1;
      rc = send(newSd,"\n", strlen("\n")+1, 0);
  }
      }
    } // endof if(ret == VPB_OK)
    else
      vpb_sleep(100);
  } while(!state);
 
}

I was wondering if I should force the function to int? I'm going to try it, but don't want to do it without some advice from a Perl 'superior'!
Thanks lots,
Scott
 
Scott hi,

There's a Perl convention that when a sub or function needs to return a zero, without it being an error, it actually returns the string "0 but true".

So, rather than returning int's I would always return strings, and return the "0 but true" string for a zero.

You seem to be returning strings already, from the look of your code.

How are you calling your C routine by the way? Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Mike,
The ctcollect subroutine is part of CTServer, an API in Linux that allows Perl scripts to run and control the telephony card. Here is the declaration:

Code:
use Telephony::CTPort;
$ctport = new Telephony::CTPort(1200); # first port of CT card

I forgot that the telephony code was in C when I started this post...oops. Sorry Perl guys!

Scott
 
That's ok :) we don't mind C.

Was the "0 but true" thing helpful? Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top