[24341] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 6530 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu May 6 11:10:55 2004

Date: Thu, 6 May 2004 08:10:17 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 6 May 2004     Volume: 10 Number: 6530

Today's topics:
    Re: Strange DBI problem (Gil Vautour)
    Re: Strange DBI problem <dwall@fastmail.fm>
    Re: Text repetition operator (Anno Siegel)
        Use of goto after an alarm? axel@white-eagle.co.uk
    Re: Use of goto after an alarm? <for-spammers-only@web.de>
    Re: Use of goto after an alarm? (Anno Siegel)
    Re: Use of goto after an alarm? axel@white-eagle.co.uk
    Re: Use of goto after an alarm? <for-spammers-only@web.de>
    Re: Use of goto after an alarm? <for-spammers-only@web.de>
    Re: win32::ole and excel VBA macro conversion: SmallScr <domenico_discepola@quadrachemicals.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 6 May 2004 05:03:48 -0700
From: vautour@unb.ca (Gil Vautour)
Subject: Re: Strange DBI problem
Message-Id: <7087ed01.0405060403.3b215570@posting.google.com>

"David K. Wall" <dwall@fastmail.fm> wrote in message news:<Xns94E0B01C72E79dkwwashere@216.168.3.30>...
> Gil Vautour <vautour@unb.ca> wrote:
> 
> > I have a Perl CGI script that was failing with a Server 500 error
> > and I think I have traced it back to a DBI subroutine that is
> > inserting the form data into a MySql table.  I can submit the form
> > with identical data except for one field (email) and it will work
> > sometimes and sometimes not depending on the email address that is
> > submitted. Has anyone seem anything like this or have any idea of
> > what I should be looking for?
> 
> I have some guesses, but without code or error messages, guesses are 
> all they would be.
> 
> Check the server logs and see what the error is. If you don't have 
> access to the server logs, put
> 
>     use CGI::Carp qw(fatalsToBrowser);
> 
> in your program and try it again. That will send warnings and error 
> messages to the browser. If you can't figure it out from that, post a 
> *short* piece of code that exhibits the problem.
> 
> OK, now the guess: I suspect you're not using placeholders and 
> instead are constructing the SQL something like this:
> 
>     my $email = param('email');
>     my $sql = "insert into TableName (email) values ($email)";
>     my $dbh = DBI->connect( ... );
>     $dbh->do($sql);
> 
> Or maybe the '@' in the email address is being interpreted as the 
> beginning of an array name. (not as likely, IMO, but possible)

Well I have been using Carp but it still only produces a 500 error.  I
don't have direct access to the server logs, I had a sys admin take a
look for me.  The only thing he could find was a mal-formed Header
problem, but it seemed like this was caused by the script failing in
general.  Here is a snippet of the DBI code that I think is where the
problem occurs:

my $dbh = DBI->connect("DBI:mysql:$database:$hostname",$user,$password)||
die "Connect failed: $dbh->errstr\n";

my($query) = @_;
my(@form,$values,$key);

foreach $key ($query->param) {
        if ($query->param($key) ne "") {
	    $values = $query->param($key);
            $values = $dbh->quote($values);
            push @form,$values;
	} else {
	    $values = "null";
            push @form,$values;
        }
}

my $sql = "insert into Responses
values(null,$form[0],$form[1],$form[2],$form[3],$form[4],".
"$form[5],$form[6],$form[7],$form[8],$form[9],$form[10],$form[11],$form[12],$form[13],NOW());";

my $sth = $dbh->prepare($sql) || die "Can't prepare $sql:
$dbh->errstr\n";

my $rv = $sth->execute || die "Can't execute $sql: $sth->errstr\n";

my $rc = $sth->finish;
$rc = $dbh->disconnect;

}

Thanks,


------------------------------

Date: Thu, 06 May 2004 14:07:49 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: Strange DBI problem
Message-Id: <Xns94E1670CEA42Fdkwwashere@216.168.3.30>

Gil Vautour <vautour@unb.ca> wrote:

> "David K. Wall" <dwall@fastmail.fm> wrote in message
> news:<Xns94E0B01C72E79dkwwashere@216.168.3.30>... 
>> Gil Vautour <vautour@unb.ca> wrote:
>> 
>> > I have a Perl CGI script that was failing with a Server 500
>> > error and I think I have traced it back to a DBI subroutine
>> > that is inserting the form data into a MySql table.  I can
>> > submit the form with identical data except for one field
>> > (email) and it will work sometimes and sometimes not depending
>> > on the email address that is submitted. Has anyone seem
>> > anything like this or have any idea of what I should be looking
>> > for? 
>> 
>> I have some guesses, but without code or error messages, guesses
>> are all they would be.
>> 
>> Check the server logs and see what the error is. If you don't
>> have access to the server logs, put
>> 
>>     use CGI::Carp qw(fatalsToBrowser);
>> 
>> in your program and try it again. That will send warnings and
>> error messages to the browser. If you can't figure it out from
>> that, post a *short* piece of code that exhibits the problem.
>> 
  [snip my guess]
> 
> Well I have been using Carp but it still only produces a 500
> error.  

Not Carp.  CGI::Carp.  There *is* a difference; please be precise. Do 
you mean you put the statement

    use CGI::Carp qw(fatalsToBrowser);

in your program and you *still* get a 500 error?

What happens when you run it from the command line with the same 
data?

>        I don't have direct access to the server logs, I had a sys
> admin take a look for me.  The only thing he could find was a
> mal-formed Header problem, but it seemed like this was caused by
> the script failing in general.  Here is a snippet of the DBI code
> that I think is where the problem occurs:
> 
> my $dbh =
> DBI->connect("DBI:mysql:$database:$hostname",$user,$password)|| 
> die "Connect failed: $dbh->errstr\n";
> 
> my($query) = @_;
> my(@form,$values,$key);
> 
> foreach $key ($query->param) {

It's better to confine $key to the loop, e.g.;

    foreach my $key ( $query->param ) {

I'd put $values inside the loop, too.

        my $values = $query->param($key);
        if ($values ne '') {
            # ...
    
>         if ($query->param($key) ne "") {
                                     ^^
As a matter of style, single quotes are often preferred when there's 
no variable interpolation happening.

>          $values = $query->param($key);
>             $values = $dbh->quote($values);

Hmm, the use of quote() makes my guess about placeholders wrong, as 
placeholders implicitly use the quote method.

>             push @form,$values;
>     } else {
>          $values = "null";
>             push @form,$values;
>         }
>}
> 
> my $sql = "insert into Responses
> values(null,$form[0],$form[1],$form[2],$form[3],$form[4],".
> "$form[5],$form[6],$form[7],$form[8],$form[9],$form[10],$form[11],$
> form[12],$form[13],NOW());"; 

This seems to me a roundabout way of doing things.  Someone please 
correct me if I'm wrong, but I've generally done something like this:

    # untested
    my $sth = $dbh->prepare(q{
        insert into Responses (colname1, colname2)
        values (?,?)
        }) 
      or die $dbh->errstr;

    $sth->execute( param('column1'), param('column2') )
        or die $sth->errstr;

It seems odd that you never actually say which columns into which 
you're inserting data, but a quick test shows that it works. I never 
knew that. (and now that I know it, I don't really like it. it seems 
sloppy.)

> my $sth = $dbh->prepare($sql) || die "Can't prepare $sql:
> $dbh->errstr\n";
> 
> my $rv = $sth->execute || die "Can't execute $sql:
> $sth->errstr\n"; 
> 
> my $rc = $sth->finish;
> $rc = $dbh->disconnect;
> 
>}

I don't see anything obviously wrong, but I've been mistaken before. 

As I said above, run the program from the command line with the same 
data and see what happens. It's difficult to debug something without 
error and warning messages.



------------------------------

Date: 6 May 2004 10:17:36 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Text repetition operator
Message-Id: <c7d3c0$6s1$5@mamenchi.zrz.TU-Berlin.DE>

Mark Clements  <mark.clements@kcl.ac.uk> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> 
> 
> > As Tony Curtis noted, "x" evaluates its argument only once, so the
> > result will always be a repetition of the same string, even if you
> > overload stringification (or tie a variable appropriately).
> sure - but it could probably be used to produce the required output in
> one fell swoop. You are 
> right, though: it's unlikely that this is desirable.
> 
> > But even if it worked, the result would be seriously obfuscated code.
> agreed.
> 
> > Exploiting side effects is always sneaky; using a side effect that
> > happens on every *access* is doubly so.
> overloading should always be used with caution, similarly tie. I got the
> impression that the OP 
> was more interested in playing "I thought it would be really cool if..."

    use overload '""' => sub { ${ $_[ 0]} ++ };
    my $x = bless \ (my $y = 'a');

    print $x, $x, $x, "\n";  # abc

Abigail uses something ismilar in one of her japh's, and that is
where code like this belongs :)

Anno



------------------------------

Date: Thu, 06 May 2004 12:38:08 GMT
From: axel@white-eagle.co.uk
Subject: Use of goto after an alarm?
Message-Id: <Qkqmc.707$Ur7.528@nurse.blueyonder.net>

I am puzzled by the strange action of 'goto' (or lack of action) when
an alarm call is caught.

Code:

======================
print "Ready> ";
local $SIG{ALRM} = sub {
                        print "Nope\n";
                        goto LAB1;
                        print "Nope 2\n";
                        };
alarm 2;
eval {                          # XXX
        $a = <STDIN>;
};                              # XXX
$b = alarm (0);
if ($b > 0) {
        print "OK: $b: $a";
} else {
        print "Seems to have timed out: $a";
}
exit;

LAB1:   
        print "\nTimed out\n";
        exit;

====================== produces:
Ready> Nope
Seems to have timed out:
====================== 

Removing the eval statements (marked with comments 'XXX' above),
produces:
====================== 
Ready> Nope
Can't find label LAB1 at ./q1.pl line 8.
====================== 

Please no flames about the use of 'goto'...  I am just curious as to why
it seems to screw up in these circumstances.

Axel




------------------------------

Date: Thu, 06 May 2004 15:01:35 +0200
From: Toni Erdmann <for-spammers-only@web.de>
Subject: Re: Use of goto after an alarm?
Message-Id: <c7dcvk$ksu$1@news.mch.sbs.de>

axel@white-eagle.co.uk wrote:
> I am puzzled by the strange action of 'goto' (or lack of action) when
> an alarm call is caught.
> 
> Code:
> 
> ======================
> print "Ready> ";
> local $SIG{ALRM} = sub {
>                         print "Nope\n";
>                         goto LAB1;
>                         print "Nope 2\n";
>                         };
> alarm 2;
> eval {                          # XXX
>         $a = <STDIN>;
> };                              # XXX
> $b = alarm (0);
> if ($b > 0) {
>         print "OK: $b: $a";
> } else {
>         print "Seems to have timed out: $a";
> }
> exit;
> 
> LAB1:   
>         print "\nTimed out\n";
>         exit;
> 
> ====================== produces:
> Ready> Nope
> Seems to have timed out:
> ====================== 
> 
> Removing the eval statements (marked with comments 'XXX' above),
> produces:
> ====================== 
> Ready> Nope
> Can't find label LAB1 at ./q1.pl line 8.
> ====================== 
> 
> Please no flames about the use of 'goto'...  I am just curious as to why
> it seems to screw up in these circumstances.

LAB1 is a label, which is valid inside the "sub{}" only.
You can't 'goto' == exit out of a subroutine.

Toni


------------------------------

Date: 6 May 2004 13:12:12 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Use of goto after an alarm?
Message-Id: <c7ddjc$ds8$1@mamenchi.zrz.TU-Berlin.DE>

Toni Erdmann  <for-spammers-only@web.de> wrote in comp.lang.perl.misc:

[ in reply to a question by axel@white-eagle.co.uk about goto]

> LAB1 is a label, which is valid inside the "sub{}" only.
> You can't 'goto' == exit out of a subroutine.

If you answer questions, please look things up instead of making them up.

From "perldoc -f goto":

                                         ....  It can
    be used to go almost anywhere else within the
    dynamic scope, including out of subroutines...

Anno


------------------------------

Date: Thu, 06 May 2004 13:14:16 GMT
From: axel@white-eagle.co.uk
Subject: Re: Use of goto after an alarm?
Message-Id: <ISqmc.11970$f62.9545@pathologist.blueyonder.net>

Toni Erdmann <for-spammers-only@web.de> wrote:
> LAB1 is a label, which is valid inside the "sub{}" only.
> You can't 'goto' == exit out of a subroutine.

I had wondered about that, althought the Camel book explicitly says that
it is possible to use goto out of a subroutine... and I have tested
it to confirm:

====== code

xx();
print "Goto Failed\n";
exit;

LAB1:   
        print "'Goto' worked\n";
        exit;

sub xx { goto LAB1; }
====== results
'Goto' worked
====== 

Axel



------------------------------

Date: Thu, 06 May 2004 16:50:35 +0200
From: Toni Erdmann <for-spammers-only@web.de>
Subject: Re: Use of goto after an alarm?
Message-Id: <c7djbr$f1a$1@news.mch.sbs.de>

axel@white-eagle.co.uk wrote:

> Toni Erdmann <for-spammers-only@web.de> wrote:
> 
>>LAB1 is a label, which is valid inside the "sub{}" only.
>>You can't 'goto' == exit out of a subroutine.
> 
> 
> I had wondered about that, althought the Camel book explicitly says that
> it is possible to use goto out of a subroutine... and I have tested
> it to confirm:
> 
> ====== code
> 
> xx();
> print "Goto Failed\n";
> exit;
> 
> LAB1:   
>         print "'Goto' worked\n";
>         exit;
> 
> sub xx { goto LAB1; }
> ====== results
> 'Goto' worked
> ====== 

Sorry, my fault.

The difference between your first posting and this one here is
that "LAB1:" here is declared/used before sub xx {} is defined.

Maybe this can explain the error?

Toni


------------------------------

Date: Thu, 06 May 2004 17:04:17 +0200
From: Toni Erdmann <for-spammers-only@web.de>
Subject: Re: Use of goto after an alarm?
Message-Id: <c7dk5i$hlf$1@news.mch.sbs.de>

Toni Erdmann wrote:

> axel@white-eagle.co.uk wrote:
> 
>> Toni Erdmann <for-spammers-only@web.de> wrote:
>>
>>> LAB1 is a label, which is valid inside the "sub{}" only.
>>> You can't 'goto' == exit out of a subroutine.
>>
>>
>>
>> I had wondered about that, althought the Camel book explicitly says that
>> it is possible to use goto out of a subroutine... and I have tested
>> it to confirm:
>>
>> ====== code
>>
>> xx();
>> print "Goto Failed\n";
>> exit;
>>
>> LAB1:           print "'Goto' worked\n";
>>         exit;
>>
>> sub xx { goto LAB1; }
>> ====== results
>> 'Goto' worked
>> ====== 
> 
> 
> Sorry, my fault.
> 
> The difference between your first posting and this one here is
> that "LAB1:" here is declared/used before sub xx {} is defined.
> 
> Maybe this can explain the error?

No it does not explain the problem. I tried it.

Toni


------------------------------

Date: Thu, 6 May 2004 09:48:53 -0400
From: "Domenico Discepola" <domenico_discepola@quadrachemicals.com>
Subject: Re: win32::ole and excel VBA macro conversion: SmallScroll
Message-Id: <Jlrmc.37072$kc2.548331@nnrp1.uunet.ca>


"Bob Walton" <invalid-email@rochester.rr.com> wrote in message
news:4099940B.10103@rochester.rr.com...
> Domenico Discepola wrote:
>
> > Hello all.  I'm trying to write some code to scroll my window pane (in
> > Microsoft Excel) & wish convert the following VBA macro to Perl format:
> > ActiveWindow.SmallScroll ToRight:=-3
> >
> > I have defined the following: $excel (excel object), $worksheet
(worksheet
> > object), $workbook (workbook object).
> >
> > I tried:
> > $excel->ActiveWindow->SmallScroll->{ToRight} = -3;
> > $workbook->ActiveWindow->SmallScroll->{ToRight} = -3;
> > $worksheet->ActiveWindow->SmallScroll->{ToRight} = -3;
> >
> > all without success...  Any suggestions would be appreciated.
>
>
> Try:
>
>    $excel->ActiveWindow->SmallScroll({ToRight=>-3});
>
> -- 
> Bob Walton

Thanks Bob - it worked like a charm.  Now, here's my next question...  I
find it very difficult to translate VBA into Perl.  The existing
documentation is very basic.  For instance, although Activestate's ASPN
provides a general example of how to translate VBA into Perl, it could not
provide me with the proper method that you did (as you can tell by my
trial-and-error approach).  One trick I learned from someone in the
newsgroups is outlined in the following code.  He suggested I print out all
key/values in the hash associated with each object.  This way, you can drill
down and search through all methods available in each object.  However, I
was still unable to find out how to scroll an Excel worksheet.  My question
is, does anyone know of a good reference guide/book that gives lots of
concrete examples?  I read the Win32 Perl Programming book by Dave Roth and
he does provide great examples but not enough of them.  I would even go
about suggesting the development of some kind of module that helps
individuals drill down into various objects' methods and properties...

#!perl
use strict;
use warnings;
use Win32::OLE;

sub quitapp () {
 my ( $ComObject ) = @_;
 $ComObject->Quit();

}

sub main () {
 my $class = "Excel.Application";

 #Create application class called "$excel"
 my $excel = Win32::OLE->GetActiveObject( $class );

 if ( ! $excel ) {
  $excel = new Win32::OLE( $class, \&quitapp ) || die "Could not create COM
${class} object\n";
 }

 $excel->{SheetsInNewWorkbook} = 1;

 #create a new workbook
 my $workbook = $excel->Workbooks->Add();

 #create a new worksheet
 my $worksheet = $workbook->WorkSheets(1);
 $worksheet->{Name} = "test";

 while ( my ($key, $value) = each %$excel ) {
         #print "$key = $value\n";

         if ( $key eq 'ActiveWindow' ) {

          while ( my ( $key2, $value2 ) = each %$value ) {
           #print "$key2 = $value2\n";

           if ( $key2 eq 'Panes' ) {
            while ( my ( $key3, $value3 ) = each %$value2 ) {
             print "$key3 = $value3\n";

            }
           }
          }

         }

     }

 $workbook->Close();
 sleep(2);
  $excel->Quit();
 }

&main();
exit 0;




------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 6530
***************************************


home help back first fref pref prev next nref lref last post