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

If I try to close the statement handle, the program hangs
and does not finish. I am connected to sybase and sql,
am getting info from sql side only here
#get dates in right format my($daysql) = "select convert(char(8),getdate(),112),convert(char(8),(dateadd(mont +h,1,getdate())),112)"; my($dtcursor)=$scb_db->prepare($daysql); $dtcursor->execute; my($ddate,$nextddate)=$dtcursor->fetchrow_array; my($day) = substr($ddate,6,2); print STDOUT "$ddate,$nextddate,$day\n"; close (OUT); close (ERROR); print STDOUT "Normal Exit\n"; $apex_db->disconnect; die "Error disconnecting from database: $DBI::err <br> $D +BI::errstr\n" if $DBI::err ; $scb_db->disconnect; die "Error disconnecting from database: $DBI::err <br> $D +BI::errstr\n" if $DBI::err ; exit (0);

20080607 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: filehandle close causes hang
by CountZero (Bishop) on Jun 05, 2008 at 19:59 UTC
    I see only one statement handle ($dtcursor), but I don't see you closing it anywhere, so how do you know your script hangs?

    Also, please note that unless you need specifically a list context you can do my $lexical_variable; rather than my ($lexical_variable);. In cases where the function or operator to the right of the equals sign has a different behaviour depending on the context this can really trip you up!

    CountZero

    PS: please remove the <br> tags and replace them by newlines.

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      I print the variable in the called subroutine and the date is there. the next statement is the return, I try to print the variable in the calling subroutine, and it never gets to the print statement, it just hangs.
      The code should read thus, and I am running perl in sybas usix attaching to sql database:
      my($ddate,$paydate); $ddate = '20080609'; print STDOUT "DATE $ddate\n"; $pay_date = get_medate($ddate); print STDOUT "back in call $pay_date\n"; sub get_medate { use strict 'vars'; my($dte) = "select max(calendar_date) from business_days_table where substring(calendar_date,1,6) =substring('$ddte',1,6) and is_NYSE_open = 'Y' and is_bank_holiday='N'"; print STDOUT "$mesql\n"; my($gt_cursor)=$scb_db->prepare($mesql); $gt_cursor->execute; my($medate) = $gt_cursor->fetchrow ; print STDOUT "MEDATE IN SUB $medate\n"; return $medate; }
      I get the print from the subroutine, I get no print from the calling line or after.

      Jun 14, 2008 at 04:33 UTC McDarren Added code tags

Re: filehandle close causes hang
by pc88mxer (Vicar) on Jun 05, 2008 at 20:52 UTC
    Can you tell us exactly what line your program is hanging on? Just add some warn statements:
    close(OUT); warn "got past close OUT"; close(ERROR); warn "got past close ERROR"; ... $apex_db->disconnect; warn "got past apex_db disconnect"; ...
      The code should read thus, and I am running perl in sybase unix attaching to sql database: my($ddate,$paydate); $ddate = '20080609'; print STDOUT "DATE $ddate\n"; $pay_date = get_medate($ddate); print STDOUT "back in call $pay_date\n"; . . . exit(0); sub get_medate { use strict 'vars'; my($dte) = "select max(calendar_date) from business_days_table where substring(calendar_date,1,6) =substring('$ddte',1,6) and is_NYSE_open = 'Y' and is_bank_holiday='N'"; print STDOUT "$mesql\n"; my($gt_cursor)=$scb_db->prepare($mesql); $gt_cursor->execute; my($medate) = $gt_cursor->fetchrow ; print STDOUT "MEDATE IN SUB $medate\n"; return $medate; } I get the print from the subroutine, I get no print from the calling line or after.
        I found the problem. I am running in SYBASE, and all the cursors have to be globally declared. If I do that, there are no hangups.