home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / perl / 7671 < prev    next >
Encoding:
Internet Message Format  |  1993-01-05  |  2.2 KB

  1. Path: sparky!uunet!pipex!bnr.co.uk!uknet!mcsun!sunic!seunet!enea!sommar
  2. From: sommar@enea.se (Erland Sommarskog)
  3. Newsgroups: comp.lang.perl
  4. Subject: wantarray bug in (syb)perl?
  5. Message-ID: <1993Jan5.130532.4698@enea.se>
  6. Date: 5 Jan 93 13:05:32 GMT
  7. Organization: Enea Data AB
  8. Lines: 60
  9.  
  10. I haven't tracked down the following very closely. It was simply
  11. too easy to get around. But anyway, the code below doesn't seem
  12. to work if I say "if (wantarray)" instead of "if ($wantarray)",
  13. but seems to work OK when I catch wantarray in the beginning 
  14. of the routine as I do.
  15.  
  16. I wouldn't say that has made me lose sleep, but I though someone
  17. might be interest. If not, well another waste of bandwidth...
  18.  
  19. sybperl -v
  20. This is perl, version 4.0
  21.  
  22. $RCSfile: perl.c,v $$Revision: 4.0.1.4 $$Date: 91/06/10 01:23:07 $
  23. Patch level: 10
  24.  
  25. Copyright (c) 1989, 1990, 1991, Larry Wall
  26.  
  27. Perl may be copied only under the terms of either the Artistic License or the
  28. GNU General Public License, which may be found in the Perl 4.0 source kit.
  29.  
  30. sub sql {
  31.     local(@sql) = @_;
  32. ;# Calls dbcmd for each element in @sql with the default DB handle and
  33. ;# the default sepatator. Each row is returned as a string with lines
  34. ;# separated by $SQL_sep. Return value is scalar or array depending on
  35. ;# wantarray. If a scalar is required, and there is more than one row,
  36. ;# only the first is returned.
  37.  
  38.     local($line, @res, @data, $stat, $_, $wantarray);
  39.  
  40.     $wantarray = wantarray;    # Seems to get cleared otherwise.
  41.  
  42.     foreach $line (@sql) {
  43.        &dbcmd($def_handle, " $line") || &abort("dbcmd failed");
  44.     }
  45.  
  46.     &dbsqlexec($def_handle) || &abort("dbsqlexec failed");
  47.  
  48.     while(($stat = &dbresults($def_handle)) != $NO_MORE_RESULTS) {
  49.         &abort("dbresults failed") if $stat == $FAIL;
  50.         while (@data = &dbnextrow($def_handle)) {
  51.             foreach (@data) { s/^NULL$//; }
  52.             push(@res, join($SQL_sep, @data));
  53.         }
  54.     }
  55.  
  56.     if ($wantarray) {       # only "wantarray" always gives false here.
  57.        @res;
  58.     }
  59.     else {
  60.        if (length(@res) > 1) {
  61.            $" = "\n";
  62.            &abort("single-row query returned more than one row:\n@row");
  63.        }
  64.        $res[$[];
  65.     }
  66. }
  67. -- 
  68. Erland Sommarskog - ENEA Data, Stockholm - sommar@enea.se
  69. Jag gav en k{ck tjeck en check.
  70.