[28549] in Perl-Users-Digest
Perl-Users Digest, Issue: 9913 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 31 18:10:21 2006
Date: Tue, 31 Oct 2006 15:10:12 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 31 Oct 2006 Volume: 10 Number: 9913
Today's topics:
Re: Problem using Spreadsheet::Excel <jdbarcelo@gmail.com>
Re: Problem using Spreadsheet::Excel <jdbarcelo@gmail.com>
Re: Problem using Spreadsheet::Excel <jgibson@mail.arc.nasa.gov>
Re: Regex question <abigail@abigail.be>
Re: Regex question <abigail@abigail.be>
Re: Regex question <wahab@chemie.uni-halle.de>
Re: Regex question <abigail@abigail.be>
Re: Regex question <xicheng@gmail.com>
Re: SIGCHLD not called under Windows kalahari@gmail.com
Use of uninitialized variable with print << HTMLEND (an kevinwhite@bellsouth.net
Re: Use of uninitialized variable with print << HTMLEND kevinwhite@bellsouth.net
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 31 Oct 2006 13:00:26 -0800
From: "dave" <jdbarcelo@gmail.com>
Subject: Re: Problem using Spreadsheet::Excel
Message-Id: <1162328425.279245.104080@b28g2000cwb.googlegroups.com>
Ok so I followed your suggestions. I did a little house cleaning and
prototyped my subroutines and took out the "&" that I put in front of
them. I also simplified my join function and it still works like you
said it would.
I put die statements after every function call, both DBI and WriteExcel
and my script died at line 62 (now it is 68) like I had said originally
with a "File not found". I am kind of at a loss since the excel file
gets created at the beginning of the script and I am able to create
worksheets just fine. I would think the worksheet data structures
would be pointing to the correct location of the file. I know that
they work ok because in lines 34 thru 49 I execute
"$worksheet1->write(0, X, '<heading name>')" for each one of my column
names where X is the column number and <heading name> is...well my
heading name.
Its the following code where the breakdown is:
65 $worksheet1->keep_leading_zeros() or die "Couldn't execute
keep_leading_zeros method: $!";
66 my $row_num = 1;
67 while ( my @row = $sth->fetchrow_array() ) {
68 $worksheet1->write_row( $row_num, 0, \@row ) or die "Couldn't
execute write method: $!";
69 $row_num = $row_num + 1;
70
71 my $line = join("|", map{defined($_) ? $_ : ""} @row);
72 #my $line = join("|",@row);
73 print $line, "\n";
74 push(@DOCIDS, "$row[2]");
75 push(@DATA, $line);
76 }
line 68 just does not want to work. I don't want to use
$sth->fetchrow_arrayref() because I like to be able to build the @DATA
and @DOCID arrays simultaneously. I am sure there is a better way to
do this but this should work.
No where in my script do I ever change the current working directory of
the script's process to another directory. That is the only
concievable way I can think of that would return an error of "File not
found". The other is maybe the $worksheet1 data structure has fallen
out of scope but that doesn't make any sense since it is a globaly
defined variable. And also I think the error would be worse than a
file not found.
Any ideas?
Dave
On Oct 31, 12:29 pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
wrote:
> dave wrote:
> > But
> > my Excel doc is not being written. If I were to put a die statement at
> > line 62 it would say "File not found". What does this mean. It means the file it's trying to write isn't found. Put those die
> statements back in, and add them to the rest of your code to help you
> debug it. You have it for your DBI related code, so why not the rest?
> Also, you could narrow down your issue by working only the
> WriteExcel related code, once it writes row 0, then add in
> some fake data for the rows, then add in the DBI.
>
> > 30 my $workbook = Spreadsheet::WriteExcel->new('test.xls')or die "Can't create text.xls: $!";
>
> > 31 $worksheet1 = $workbook->add_worksheet("test 1");Hang on.. you have enabled strict but it doesn't complain about
> $worksheet1 not being defined?
>
> > 53 my $sqlStmt = &sql_statement_setup( $opt{S}, $opt{P} );Don't use '&', also give placeholders a try, in your SQL.
>
> > 54
> > 55 my $sth = $DBH->prepare($sqlStmt) || die "\nPrepare error: $DBI::err
> > ... $DBI::err str\n";
> > 56 $sth->execute() || die "\nExecute error: $DBI::err ...
> > $DBI::errstr\n";
> > 57
> > 58 my $row_num = 1;
> > 59 while ( my @row = $sth->fetchrow_array() ) {Could use fetchrow_arrayref.
>
> > 60 my $array_ref = \@row;No reason to do that.
>
> > 61 $worksheet1->keep_leading_zeros();Possibly you only need to call this once.
>
> > 62 $worksheet1->write_row( $row_num, 0, $array_ref );
> > 63 $row_num = $row_num + 1;$worksheet1->write_row( $row_num++, 0, \@row );> 64
> > 65 my $line = join("|", map{defined($_) ? $_ : ""} @row);What is the map doing that join('|', @row) doesn't do?
------------------------------
Date: 31 Oct 2006 13:21:24 -0800
From: "dave" <jdbarcelo@gmail.com>
Subject: Re: Problem using Spreadsheet::Excel
Message-Id: <1162329684.301002.174290@b28g2000cwb.googlegroups.com>
Here is some new data. I read in the WriteExcel docs that the write
method returns 0 on success. Well I evaluated this in my code and I
found that my write_row method was indeed returning 0 which would leave
me to believe that at least it thinks it is writing correctly. But at
the same time $! = "No such file or directory".
I am ignorant to the way perl internals work so if this behavior is not
unusal then I would be much obliged if someone could clue me in.
Dave
On Oct 31, 3:00 pm, "dave" <jdbarc...@gmail.com> wrote:
> Ok so I followed your suggestions. I did a little house cleaning and
> prototyped my subroutines and took out the "&" that I put in front of
> them. I also simplified my join function and it still works like you
> said it would.
>
> I put die statements after every function call, both DBI and WriteExcel
> and my script died at line 62 (now it is 68) like I had said originally
> with a "File not found". I am kind of at a loss since the excel file
> gets created at the beginning of the script and I am able to create
> worksheets just fine. I would think the worksheet data structures
> would be pointing to the correct location of the file. I know that
> they work ok because in lines 34 thru 49 I execute
> "$worksheet1->write(0, X, '<heading name>')" for each one of my column
> names where X is the column number and <heading name> is...well my
> heading name.
>
> Its the following code where the breakdown is:
> 65 $worksheet1->keep_leading_zeros() or die "Couldn't execute
> keep_leading_zeros method: $!";
> 66 my $row_num = 1;
> 67 while ( my @row = $sth->fetchrow_array() ) {
> 68 $worksheet1->write_row( $row_num, 0, \@row ) or die "Couldn't
> execute write method: $!";
> 69 $row_num = $row_num + 1;
> 70
> 71 my $line = join("|", map{defined($_) ? $_ : ""} @row);
> 72 #my $line = join("|",@row);
> 73 print $line, "\n";
> 74 push(@DOCIDS, "$row[2]");
> 75 push(@DATA, $line);
> 76 }
>
> line 68 just does not want to work. I don't want to use
> $sth->fetchrow_arrayref() because I like to be able to build the @DATA
> and @DOCID arrays simultaneously. I am sure there is a better way to
> do this but this should work.
>
> No where in my script do I ever change the current working directory of
> the script's process to another directory. That is the only
> concievable way I can think of that would return an error of "File not
> found". The other is maybe the $worksheet1 data structure has fallen
> out of scope but that doesn't make any sense since it is a globaly
> defined variable. And also I think the error would be worse than a
> file not found.
>
> Any ideas?
>
> Dave
>
> On Oct 31, 12:29 pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
> wrote:
>
> > dave wrote:
> > > But
> > > my Excel doc is not being written. If I were to put a die statement at
> > > line 62 it would say "File not found". What does this mean. It means the file it's trying to write isn't found. Put those die
> > statements back in, and add them to the rest of your code to help you
> > debug it. You have it for your DBI related code, so why not the rest?
> > Also, you could narrow down your issue by working only the
> > WriteExcel related code, once it writes row 0, then add in
> > some fake data for the rows, then add in the DBI.
>
> > > 30 my $workbook = Spreadsheet::WriteExcel->new('test.xls')or die "Can't create text.xls: $!";
>
> > > 31 $worksheet1 = $workbook->add_worksheet("test 1");Hang on.. you have enabled strict but it doesn't complain about
> > $worksheet1 not being defined?
>
> > > 53 my $sqlStmt = &sql_statement_setup( $opt{S}, $opt{P} );Don't use '&', also give placeholders a try, in your SQL.
>
> > > 54
> > > 55 my $sth = $DBH->prepare($sqlStmt) || die "\nPrepare error: $DBI::err
> > > ... $DBI::err str\n";
> > > 56 $sth->execute() || die "\nExecute error: $DBI::err ...
> > > $DBI::errstr\n";
> > > 57
> > > 58 my $row_num = 1;
> > > 59 while ( my @row = $sth->fetchrow_array() ) {Could use fetchrow_arrayref.
>
> > > 60 my $array_ref = \@row;No reason to do that.
>
> > > 61 $worksheet1->keep_leading_zeros();Possibly you only need to call this once.
>
> > > 62 $worksheet1->write_row( $row_num, 0, $array_ref );
> > > 63 $row_num = $row_num + 1;$worksheet1->write_row( $row_num++, 0, \@row );> 64
> > > 65 my $line = join("|", map{defined($_) ? $_ : ""} @row);What is the map doing that join('|', @row) doesn't do?
------------------------------
Date: Tue, 31 Oct 2006 13:32:48 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Problem using Spreadsheet::Excel
Message-Id: <311020061332487518%jgibson@mail.arc.nasa.gov>
In article <1162316942.608563.58050@e64g2000cwd.googlegroups.com>, dave
<jdbarcelo@gmail.com> wrote:
> Hello all,
>
> I have been searching the archives for a while and can't seem to find a
> solution to my problem. The quick and dirty of it is I am pulling data
> from an Oracle database via DBI. I am using the following code to pull
> data and write it into an excel spreadsheet:
>
> 59 while ( my @row = $sth->fetchrow_array() ) {
> 60 my $array_ref = \@row;
> 61 $worksheet1->keep_leading_zeros();
> 62 $worksheet1->write_row( $row_num, 0, $array_ref );
> 63 $row_num = $row_num + 1;
> 64
> 65 my $line = join("|", map{defined($_) ? $_ : ""} @row);
> 66 print $line, "\n";
> 67 push(@DOCIDS, "$row[2]");
> 68 push(@DATA, $line);
> 69 }
>
> After executing this I fill up the array @DATA and @DOCIDS and I print
> each row (ie line 66) so I know I am fetching the data correctly. But
> my Excel doc is not being written. If I were to put a die statement at
> line 62 it would say "File not found". What does this mean. This is
> really driving me nuts! It should work. Let me know if I need to
> supply more info.
The documentation for Spreadsheet::WriteExcel describes what is
returned by the write methods. It does not mention setting $!, if that
is what you are printing to get "File not found". Since you don't show
us the code that produced that warning, we cannot tell. The value of $!
is not being set and is misleading. You should save and print the
actual numerical value returned by write_row and consult the
documentation as to what it means.
Since you are having trouble creating the spreadsheet and not
apparently the DBI module, you should first create a program that
writes some fixed data to a spreadsheet. If that program does not work,
someone here will be able to tell you why.
If the spreadsheet program does work, then you can add the database
code to fetch the data from the database. If that combination doesn't
work, then feel free to post the program here. But you should first
make sure you can create a spreadsheet successfully before suspecting
an interaction between the spreadsheet and the database statements.
Please make an attempt to follow the guidelines for this group,
including 1) not top-posting and 2) posting real code that compiles.
Thank you.
------------------------------
Date: 31 Oct 2006 19:50:58 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: Regex question
Message-Id: <slrnekfa8e.k33.abigail@alexandra.abigail.be>
Mirco Wahab (wahab@chemie.uni-halle.de) wrote on MMMMDCCCIX September
MCMXCIII in <URL:news:ei7ub2$12k$1@mlucom4.urz.uni-halle.de>:
|| Thus spoke DJ Stunks (on 2006-10-31 17:25):
|| > Mirco Wahab wrote:
|| >> my $rg = '\%\%([^\%]+)\%\%';
|| >
|| > ????
|| >
|| > maybe you should start trying some of this stuff before posting?
||
|| Upps, yes ... I should at least have tried it once ...
||
|| a simple
|| ...
|| my $text = 'text text %%slotname%% text text %%slotname%% text';
|| my @slots = $text =~ /%%([^%]+)%%/g;
|| ...
||
|| would, of course, have done it ...
No, because that assumes there cannot be any %'s in slotname.
Abigail
--
A perl rose: perl -e '@}-`-,-`-%-'
------------------------------
Date: 31 Oct 2006 19:52:51 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: Regex question
Message-Id: <slrnekfabv.k33.abigail@alexandra.abigail.be>
Boerni (john@doe.com) wrote on MMMMDCCCIX September MCMXCIII in
<URL:news:ei7q9f$p4h$1@atlas.ip-plus.net>:
== Hi
==
== I have a string which looks like this
==
== text text %%slotname%% text text %%slotname%%...
==
== What I would like to do, is to get the values between the '%%' signs. The
== Problem is, I dont know how many of those %%slotname%% occur in the string.
==
== Is there any way to solve this problem with a regex?
==
== what i've tried is something like:
== ($slot1, $slot2, $slot3, $slot4, $slot5) = $a_msg =~
== /.*?%%(.*?)%%.*?%%(.*?)%%.*?%%(.*?)%%.*?%%(.*?)%%.*?%%(.*?)%%/;
==
== (assuming there are no more than 5 slots in a string)... the Problem is,
== this only works if there are exactly 5 slots.
==
== What i'd like is something like:
==
== (@slots) = $a_msg =~ /some regex/
==
== Is there any way to do this?
Untested:
@slots = $a_msg =~ /%%([^%]*(?:%[^%]+)*)%%/g;
Abigail
--
perl -wle'print"Κυστ αξοτθες Πεςμ Θαγλες"^"\x80"x24'
------------------------------
Date: Tue, 31 Oct 2006 22:19:46 +0100
From: Mirco Wahab <wahab@chemie.uni-halle.de>
Subject: Re: Regex question
Message-Id: <ei8etn$5mi$1@mlucom4.urz.uni-halle.de>
Thus spoke Abigail (on 2006-10-31 20:52):
> Untested:
>
> @slots = $a_msg =~ /%%([^%]*(?:%[^%]+)*)%%/g;
>
If we know /exactly/ what the boundaries are
happen to be (%%searched%%), why wouldn't a
simple:
my @slots = $a_msg =~ /%%(.+?)%%/g;
do (even on '%%some%wiered%stuff%%')?
Regards
M.
------------------------------
Date: 31 Oct 2006 22:10:44 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: Regex question
Message-Id: <slrnekfied.k33.abigail@alexandra.abigail.be>
Mirco Wahab (wahab@chemie.uni-halle.de) wrote on MMMMDCCCIX September
MCMXCIII in <URL:news:ei8etn$5mi$1@mlucom4.urz.uni-halle.de>:
$$ Thus spoke Abigail (on 2006-10-31 20:52):
$$
$$ > Untested:
$$ >
$$ > @slots = $a_msg =~ /%%([^%]*(?:%[^%]+)*)%%/g;
$$ >
$$
$$ If we know /exactly/ what the boundaries are
$$ happen to be (%%searched%%), why wouldn't a
$$ simple:
$$
$$ my @slots = $a_msg =~ /%%(.+?)%%/g;
$$
$$ do (even on '%%some%wiered%stuff%%')?
Two reasons. My solution doesn't use any backtracking, so in general
it should be faster, although in this specific case, your solution
is more likely to be dealt with by the optimizer.
The other reason is that '%%([^%]*(?:%[^%]+)*)%%' will never match
anything with an internal '%%', even if you interpolate that pattern
in a larger pattern. Something you cannot say of '%%(.+?)%%' - context
may force it to match an internal '%%'.
Abigail
--
#!/opt/perl/bin/perl -w
$\ = $"; $; = $$; END {$: and print $:} $SIG {TERM} = sub {$ := $_}; kill 15 =>
fork and ($; == getppid and exit or wait) foreach qw /Just another Perl Hacker/
------------------------------
Date: 31 Oct 2006 14:53:16 -0800
From: "Xicheng Jia" <xicheng@gmail.com>
Subject: Re: Regex question
Message-Id: <1162335196.898493.185550@e64g2000cwd.googlegroups.com>
Abigail wrote:
> Mirco Wahab (wahab@chemie.uni-halle.de) wrote on MMMMDCCCIX September
> MCMXCIII in <URL:news:ei8etn$5mi$1@mlucom4.urz.uni-halle.de>:
> $$ Thus spoke Abigail (on 2006-10-31 20:52):
> $$
> $$ > Untested:
> $$ >
> $$ > @slots = $a_msg =~ /%%([^%]*(?:%[^%]+)*)%%/g;
> $$ >
> $$
> $$ If we know /exactly/ what the boundaries are
> $$ happen to be (%%searched%%), why wouldn't a
> $$ simple:
> $$
> $$ my @slots = $a_msg =~ /%%(.+?)%%/g;
> $$
> $$ do (even on '%%some%wiered%stuff%%')?
>
> Two reasons.
> My solution doesn't use any backtracking,
No, this is not about "backtracking". check a string that doesnot match
your pattern, it will use backtracking anyway. In Perl, unless you use
(?>...) construct, your pattern will always try to save some status
information and then backtrack it later. :)
A similar comparion might be between "[^"]*" and ".*?", which is all
about how different quantifiers work instead of whether backtracking is
involved.
Regards,
Xicheng
> so in general
> it should be faster, although in this specific case, your solution
> is more likely to be dealt with by the optimizer.
>
> Abigail
> --
> #!/opt/perl/bin/perl -w
> $\ = $"; $; = $$; END {$: and print $:} $SIG {TERM} = sub {$ := $_}; kill 15 =>
> fork and ($; == getppid and exit or wait) foreach qw /Just another Perl Hacker/
------------------------------
Date: 31 Oct 2006 11:07:31 -0800
From: kalahari@gmail.com
Subject: Re: SIGCHLD not called under Windows
Message-Id: <1162321651.279108.32530@e64g2000cwd.googlegroups.com>
In case anyone else is interested, the solution I found is to use
waitpid(). This works on both platforms:
use POSIX ":sys_wait_h";
print time . "[$$] parent starting\n";
my $cur_children = 0;
my $total_children = 0;
my $max_concurrent_children = 3;
my $max_total_children = 10;
while(1) {
if($cur_children < $max_concurrent_children and $total_children <
$max_total_children) {
print time . "[$$] parent spawnding new child\n";
my $frk = fork;
if($frk == 0) {
&child_work;
} else {
$cur_children++;
$total_children++;
}
}
print time . "[$$] parent current children: $cur_children\n";
print time . "[$$] parent total children: $total_children\n";
if($cur_children <= 0 and $total_children >= $max_total_children) {
print time . "[$$] parent exiting\n";
exit;
}
waitpid(-1,WNOHANG);
if($? == 0) {
print time . "[$$] parent saw child exit\n";
$cur_children--;
}
sleep 1;
}
sub child_work {
print time . "[$$] child starting\n";
my $sleep = int(rand(9)) + 1;
print time . "[$$] child sleeping for $sleep seconds\n";
sleep $sleep;
print time . "[$$] child exiting\n";
exit;
}
------------------------------
Date: 31 Oct 2006 14:28:03 -0800
From: kevinwhite@bellsouth.net
Subject: Use of uninitialized variable with print << HTMLEND (and $ENV)
Message-Id: <1162333683.856582.98790@e64g2000cwd.googlegroups.com>
I am trying to fix some code I inherited, and there are many warnings I
would like to clean up. The biggest offenders are as follows:
print <<"HTMLEND"; #<= this causes an uninitialized variable warning...
<html><head></head><body></body></html>
HTMLEND
The second example is:
my $htmlfile = "";
my $hide = "";
$htmlfile = param('filename');
$hide = param('hide') || "no"; #<= what does this do?
if ($htmlfile eq "default.html") #<= this causes an uninitialized
variable warning...
{ #do some stuff
}
The third problem I have is printing environment variables when I have
"use strict" enabled. What is the legal way to do this?
I have:
my $n;
my @keys;
my @values;
my $Item;
@keys = keys %ENV;
@values %ENV;
$n = 0;
foreach $Item (@keys) { $$Item = $values[$n++];}
foreach $Item (@keys) { print "$Item=$$Item"; }
Thanks in advance,
-Kevin
------------------------------
Date: 31 Oct 2006 14:38:20 -0800
From: kevinwhite@bellsouth.net
Subject: Re: Use of uninitialized variable with print << HTMLEND (and $ENV)
Message-Id: <1162334300.144254.278430@i42g2000cwa.googlegroups.com>
kevinwh...@bellsouth.net wrote:
> I am trying to fix some code I inherited, and there are many warnings I
> would like to clean up. The biggest offenders are as follows:
>
> print <<"HTMLEND"; #<= this causes an uninitialized variable warning...
>
> <html><head></head><body></body></html>
> HTMLEND
>
> The second example is:
> my $htmlfile = "";
> my $hide = "";
>
> $htmlfile = param('filename');
> $hide = param('hide') || "no"; #<= what does this do?
>
> if ($htmlfile eq "default.html") #<= this causes an uninitialized
> variable warning...
> { #do some stuff
> }
>
> The third problem I have is printing environment variables when I have
> "use strict" enabled. What is the legal way to do this?
>
> I have:
> my $n;
> my @keys;
> my @values;
> my $Item;
>
> @keys = keys %ENV;
> @values %ENV;
> $n = 0;
> foreach $Item (@keys) { $$Item = $values[$n++];}
> foreach $Item (@keys) { print "$Item=$$Item"; }
Got it:
foreach my $key (sort keys %ENV) { print "$key => $ENV{$key}\n" }
>
> Thanks in advance,
>
> -Kevin
------------------------------
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 9913
***************************************