Fwd: cgi Pain!!!

2007-10-05 Thread Pat Rice
hi all
have the following problem

I cannot for the life of me get the following working:

 28 testing the header
 29 print header;
 30 print start_html(Thank You);
 31 print h2(Thank You);
 32
 33 my $i;
 34 foreach $i (@vmsupportTest)
 35 {
 36print h3($i );
 37 }

excuse comments (#) as I was trouble shooing the problem but to no avail, it
does work when I run it from the commnad line.
I am not sure if I am implementing line 36 correctly, as I can see that
there is informaion in the array, but it is not being sent to the screen.

Thanks
Pat



  1 #!/usr/bin/perl -w
  2 use CGI qw(-debug :standard);
  3 use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  4 use strict;
  5  adding the bits from teat.pl
  6 use warnings;
  7 #use LWP::Simple;
  8 use LWP::Simple qw(!head);
  9 use diagnostics;
 10 use URI;
 11
 12
 13
 14
 15
 16 ## setting up the variables for the form
 17 my $query;
 18 my $p;
 19 my $variable_name = param(srNumber);
 20 #my $variable_name = 194951021;
 21
 22
 23 [EMAIL PROTECTED] = `/usr/bin/perl mysecondperlscript.pl` ;
 24
 25 my @vmsupportTest=`/usr/bin/perl
/var/www/cgi-bin/downLoadTeatbook4.pl
http://test.test.com/pending/$variable_name`http://test.test.com/pending/$variable_name%60
;
 26
 27 print @supportTest;
 28 #testing the header
 29 #print header;
 30 #print start_html(Thank You);
 31 #print h2(Thank You);
 32
 33 #my $i;
 34 #foreach $i (@vmsupportTest)
 35 #{
 36 #   print h3($i );
 37 #}
 38


Perl OBDC Segmentation fault

2007-10-05 Thread [EMAIL PROTECTED]
Hi all,
I have a script about query record from MS Access database.
This database contains a table named Lengths.
The list of field names is Sample_ID, Taxon_ID, Lenght.

Question is coming:
1. When I use the query command: select * from Lengths
It works well!
2. When I use the query command: select  * from Lengths where Taxon_ID
=1
It gives the following:
18-1-16
23-1-17
23-1-15
23-1-14
24-1-15
Segmentation fault
3. When I use the query command: select  * from Lengths where
Sample_ID = 1
The result is :
[EMAIL PROTECTED] msaccess]$ perl test.pl
Segmentation fault

Does anyone meet this problem?
My code is like this:
my $dbh = DBI- connect( dbi:ODBC:DSN=mdb1) or die $DBI::errstr;
my $query = select * from Lengths;

my $tp=$dbh-prepare(select  * from Lengths where Sample_ID = 1) or
die cna nont $dbh-err $dbh-errstr \n;
$tp-execute();

#print $tp;

while (my @tmp_l=$tp-fetchrow_array())
{ print join('-',@tmp_l),\n;}

The operating system is Red Hat Enterprise 3.
Perl : v5.8.3 built for i386-linux-thread-multi
ODBC: unixODBC 2.2.12
DBD::ODBC: 1.14

Best Regards,
Xu


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Perl OBDC Segmentation fault

2007-10-05 Thread Rob Coops
That should be: PERL_DL_NONLAZY=1

(fat fingers or a slow brain, quite possibly both, cause me to make those
kinds of mistakes)


On 10/5/07, Rob Coops [EMAIL PROTECTED] wrote:

 Here is one solution that seems to work for me.

 Set the following environment variable: PERL_DL_NOnLAZY=1

 The explanation as to what it does and why this might work you can fine at
 this link:  http://www.perl.com/lpt/a/520

 Regards,

 Rob


 On 10/4/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 
  Hi all,
 I have a script about query record from MS Access database.
  This database contains a table named Lengths.
  The list of field names is Sample_ID, Taxon_ID, Lenght.
 
  Question is coming:
  1. When I use the query command: select * from Lengths
  It works well!
  2. When I use the query command: select  * from Lengths where Taxon_ID
  =1
  It gives the following:
  18-1-16
  23-1-17
  23-1-15
  23-1-14
  24-1-15
  Segmentation fault
  3. When I use the query command: select  * from Lengths where
  Sample_ID = 1
  The result is :
  [EMAIL PROTECTED] msaccess]$ perl test.pl
  Segmentation fault
 
  Does anyone meet this problem?
  My code is like this:
  my $dbh = DBI- connect( dbi:ODBC:DSN=mdb1) or die $DBI::errstr;
  my $query = select * from Lengths;
 
  my $tp=$dbh-prepare(select  * from Lengths where Sample_ID = 1) or
  die cna nont $dbh-err $dbh-errstr \n;
  $tp-execute();
 
  #print $tp;
 
  while (my @tmp_l=$tp-fetchrow_array())
  { print join('-',@tmp_l),\n;}
 
  The operating system is Red Hat Enterprise 3.
  Perl : v5.8.3 built for i386-linux-thread-multi
  ODBC: unixODBC 2.2.12
  DBD::ODBC: 1.14
 
  Best Regards,
  Xu
 
 
  --
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]
  http://learn.perl.org/
 
 
 



Re: Perl OBDC Segmentation fault

2007-10-05 Thread Jeff Pang
2007/10/4, [EMAIL PROTECTED] [EMAIL PROTECTED]:
 2. When I use the query command: select  * from Lengths where Taxon_ID
 =1
 It gives the following:
 18-1-16
 23-1-17
 23-1-15
 23-1-14
 24-1-15
 Segmentation fault
 3. When I use the query command: select  * from Lengths where
 Sample_ID = 1
 The result is :
 [EMAIL PROTECTED] msaccess]$ perl test.pl
 Segmentation fault


Hi,

This is hard to debug it via common perl way.
You may refer this DBI debug document or ask it on DBI mailing list.

http://www.easysoft.com/developer/languages/perl/dbi-debugging.html

-- 
Jeff Pang - [EMAIL PROTECTED]
http://www.rwweb.co.cc

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Display test in MAN page way

2007-10-05 Thread Sundeep
I have a script to do several operations, based on the various options
provided from command line.

For that I have created the help text (script.pl --help). The text is
huge (exceeding 1 page) and I can not cut down on that.

So when users (one who uses my script) asks for help, it just scrolls
off (expected).

Now I've been asked to display the text in the way man pages are
displayed (scrollable up  down), or atleast in script.pl --help |
more way... i.e., scrolling in one direction.

Is this possible using perl and how? Any sample code (or link) would
help me do things quick


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Perl OBDC Segmentation fault

2007-10-05 Thread [EMAIL PROTECTED]
Hi all,
I have a script about query record from MS Access database.
This database contains a table named Lengths.
The list of field names is Sample_ID, Taxon_ID, Lenght.

Question is coming:
1. When I use the query command: select * from Lengths
It works well!
2. When I use the query command: select  * from Lengths where Taxon_ID
=1
It gives the following:
18-1-16
23-1-17
23-1-15
23-1-14
24-1-15
Segmentation fault
3. When I use the query command: select  * from Lengths where
Sample_ID = 1
The result is :
[EMAIL PROTECTED] msaccess]$ perl test.pl
Segmentation fault

Does anyone meet this problem?
My code is like this:
my $dbh = DBI- connect( dbi:ODBC:DSN=mdb1) or die $DBI::errstr;
my $query = select * from Lengths;

my $tp=$dbh-prepare(select  * from Lengths where Sample_ID = 1) or
die cna nont $dbh-err $dbh-errstr \n;
$tp-execute();

#print $tp;

while (my @tmp_l=$tp-fetchrow_array())
{ print join('-',@tmp_l),\n;}

The operating system is Red Hat Enterprise 3.
Perl : v5.8.3 built for i386-linux-thread-multi
ODBC: unixODBC 2.2.12
DBD::ODBC: 1.14

Best Regards,
Xu


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Display test in MAN page way

2007-10-05 Thread John W. Krahn

Sundeep wrote:

I have a script to do several operations, based on the various options
provided from command line.

For that I have created the help text (script.pl --help). The text is
huge (exceeding 1 page) and I can not cut down on that.

So when users (one who uses my script) asks for help, it just scrolls
off (expected).

Now I've been asked to display the text in the way man pages are
displayed (scrollable up  down), or atleast in script.pl --help |
more way... i.e., scrolling in one direction.

Is this possible using perl and how? Any sample code (or link) would
help me do things quick


Assuming that your help text is in POD format:

#!/usr/bin/perl
use warnings;
use strict;

# Your code here


sub help {
0 == system perldoc = $0   # Run perldoc on myself
or die system perldoc = $0 failed: $?;
}


__END__

=head1 My Help Document

More help stuff here.

=cut




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Weird Regular expression.

2007-10-05 Thread ash
Hello fellow scripters!

I've come across weird regex like this: (.{4}).(.{21}).(.{3})..(.{3}).
(.{10}).(.{9}).
Has anyone any idea about the syntax of these regex? Thank you for
your help.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Weird Regular expression.

2007-10-05 Thread John W. Krahn

ash wrote:

Hello fellow scripters!


Hello,


I've come across weird regex like this: (.{4}).(.{21}).(.{3})..(.{3}).
(.{10}).(.{9}).
Has anyone any idea about the syntax of these regex? Thank you for
your help.


The period (.) matches any character except the newline character unless the 
/s option is used and then it matches any character including the newline 
character.


So that expression matches 4 characters and stores them in $1 then matches 1 
character then matches 21 characters and stores them in $2 then matches 1 
character then matches 3 characters and stores them in $3 then matches 2 
characters then matches 3 characters and stores them in $4 then matches 1 
character then matches 10 characters and stores them in $5 then matches 1 
character then matches 9 characters and stores them in $6 then matches 1 
character.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: How to reinvent grep in perl?

2007-10-05 Thread John W. Krahn

siegfried wrote:



?? wrote:

siegfried wrote:


I need to search large amounts of source code and grep is not doing the
job. The problem is that I keep matching stuff in the comments of the
C++/Java/Perl/Groovy/Javascript source code.

Can someone give me some hints on where I might start on rewriting grep
in perl so that it ignores the contents of /* and */ comments?


Instead of rewriting grep, consider writing a comment filter.  Have it
read from standard input and write to standard output; pipe the file
that you want to grep into it, and pipe its output into grep.


Thanks, but if I am piping from stdin to stdout I see two problems:

(1) how do I implement the -n flags that tell me the line number and
file name where the matches are

(2) how do I make two passes: one to strip out the comments (and
preserve the original line breaks so I don't screw up the line
numbers) and the other to actually search for what I am looking for?


The only way I can see to do this is to make three passes:

  Pass #1: prepend the file name and current line number on to the
beginning of each line (is there a way to interrogate stdin to get the
file name?) So on a path with a long file and path name, that could
easly double the memory requirement to store all that stuff
redundantly on each line.

  Pass #2: change all comments to spaces except new-lines
  Pass #3: search for the pattern and print the line it is found on 


Now I could do this with pipes and 3 different instances of perl
running at the same time. Is there a better way?

So am I concerned about memory problems? The worst files are 16K lines
long and consume a megabyte. I'm running windows with 2GB RAM. Should
I be concerned about making multiple in memory passes over a 1MB
string (that becomes a 3MB string after I prepend the file name and
line number to the beginning of every line)?  How can I write to a
string instead stdout and make an additional pass using the technique
described in perldoc -q comments.

Now I have queried this mailing list previously when I had a scraper
that ran for six hours scraping web sites. If I recall correctly,
perl's memory management was a bit of a problem. Will perl recycle my
memory properly if I keep using the same 3MB string variables over and
over again?

How do I read an entire file into a string? I know how to do it record
by record. Is there a more efficient way?



Here is one way to do what you want:


local $/;   # slurp whole file

while ( @ARGV ) {

$_ = ;   # get current file - puts file name in $ARGV

# remove C/C++ comments (based on perlfaq)
# and remove everything except newlines

s!(/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*)|((\\.|[^\\])*|'(\\.|[^'\\])*'|.[^/'\\]*)! 
defined $3 ? $3 : do { ( my $x = $1 ) =~ y/\n//cd; $x } !gse;


my $line_num;

for my $line ( split /\n/ ) {

++$line_num;

print File name: $ARGV  Line number: $line_num  Line: $line\n
if $line =~ /grep pattern/;

}

}




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Compare two files

2007-10-05 Thread Jeff Pang
2007/10/5, [EMAIL PROTECTED]
[EMAIL PROTECTED]:
 Hi
 I would like to compare two files by comparing the files dates.
 If one file shows
 ls -la May 12 2003 filename
 and the other name shows the same date they are OK for me (I'm not
 interested in the time part only the date of the file)
 But if the dates are not the same I would like to copy one of the
 files.
 How do I do this in Perl?

Hi,

if ( int(-M file1.txt) != int(-M file2.txt) ) {
# copy the file
}

for copying files,you can use the module File::Copy from CPAN,
http://search.cpan.org/~nwclark/perl-5.8.8/lib/File/Copy.pm

-- 
Jeff Pang - [EMAIL PROTECTED]
http://www.rwweb.co.cc

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Compare two files

2007-10-05 Thread fredrik . y . andersson
Hi
I would like to compare two files by comparing the files dates.
If one file shows
ls -la May 12 2003 filename
and the other name shows the same date they are OK for me (I'm not
interested in the time part only the date of the file)
But if the dates are not the same I would like to copy one of the
files.
How do I do this in Perl?
if (file1 not equal in date file2) {
 cp file 1 someplace
}
Thanks in advance
Fredrik Andersson


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Compare two files

2007-10-05 Thread Jeff Pang
2007/10/5, Jeff Pang [EMAIL PROTECTED]:
 2007/10/5, [EMAIL PROTECTED]
 [EMAIL PROTECTED]:

  How do I do this in Perl?

 Hi,

 if ( int(-M file1.txt) != int(-M file2.txt) ) {
 # copy the file
 }


Forgot to tell you what's -M in perl,from `perldoc perlfunc`:

-M  Script start time minus file modification time, in days.

-- 
Jeff Pang - [EMAIL PROTECTED]
http://www.rwweb.co.cc

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: How to install perl Expect module under cygwin

2007-10-05 Thread Javi
On Sep 23, 3:01 pm, [EMAIL PROTECTED] (Juan Pablo Feria Gomez) wrote:
 Try installing manually IO::Pty before...
I have no problems compiling with cl.exe, but i can´t install this
module.
cl.exe works fine, paths are ok.
any suggest?


Thanks!


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Compare two files

2007-10-05 Thread Gunnar Hjalmarsson

Jeff Pang wrote:

2007/10/5, [EMAIL PROTECTED]
[EMAIL PROTECTED]:

I would like to compare two files by comparing the files dates.
If one file shows
ls -la May 12 2003 filename
and the other name shows the same date they are OK for me (I'm not
interested in the time part only the date of the file)
But if the dates are not the same I would like to copy one of the
files.
How do I do this in Perl?


if ( int(-M file1.txt) != int(-M file2.txt) ) {
# copy the file
}


That might be true also when the dates are the same. I would use stat(), 
localtime() and timelocal() instead.


use Time::Local;
my $date1 = timelocal 0,0,0,(localtime( (stat 'file1.txt')[9] ))[3..5];
my $date2 = timelocal 0,0,0,(localtime( (stat 'file2.txt')[9] ))[3..5];
if ( $date1 != $date2 ) {
# copy the file
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Compare two files

2007-10-05 Thread Jeff Pang
2007/10/5, Paul Lalli [EMAIL PROTECTED]:
 On Oct 5, 7:50 am, [EMAIL PROTECTED] (Jeff Pang) wrote:
  2007/10/5, [EMAIL PROTECTED]
  [EMAIL PROTECTED]:
 
   I would like to compare two files by comparing the files dates.
   If one file shows
   ls -la May 12 2003 filename
   and the other name shows the same date they are OK for me (I'm not
   interested in the time part only the date of the file)
   But if the dates are not the same I would like to copy one of the
   files.
   How do I do this in Perl?

  if ( int(-M file1.txt) != int(-M file2.txt) ) {
  # copy the file
  }

 That's a very naïve approach that will frequently fail.
 For example:
 File one modified 10/1/2007 9am
 File two modified 10/1/2007 3pm
 and the current time is 10/2/2007 12pm

 -M 'file1' will return 1.125
 -M 'file2' will return 0.875.

 int(1.25) == 1
 int(0.875) == 0

You're right here.


 sub m_to_date {
my $days_ago = shift;
my $ts = time() - ($days_ago * 24 * 60 * 60);
my $date = strftime('%Y-%m-%d', localtime($ts));
return $date;
 }

-M is not needed here.to get the date,one can just stat it,

sub get_date {
my $file = shift;
my $mtime = (stat $file)[9];
return strftime '%Y-%m-%d', localtime($mtime);
}


-- 
Jeff Pang - [EMAIL PROTECTED]
http://www.rwweb.co.cc

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Compare two files

2007-10-05 Thread Paul Lalli
On Oct 5, 7:50 am, [EMAIL PROTECTED] (Jeff Pang) wrote:
 2007/10/5, [EMAIL PROTECTED]
 [EMAIL PROTECTED]:

  I would like to compare two files by comparing the files dates.
  If one file shows
  ls -la May 12 2003 filename
  and the other name shows the same date they are OK for me (I'm not
  interested in the time part only the date of the file)
  But if the dates are not the same I would like to copy one of the
  files.
  How do I do this in Perl?

 if ( int(-M file1.txt) != int(-M file2.txt) ) {
 # copy the file
 }

That's a very naïve approach that will frequently fail.
For example:
File one modified 10/1/2007 9am
File two modified 10/1/2007 3pm
and the current time is 10/2/2007 12pm

-M 'file1' will return 1.125
-M 'file2' will return 0.875.

int(1.25) == 1
int(0.875) == 0

There are two possible correct solutions.  One is to actually
execute `ls -l` for each file and parse the output.  The other is to
take the return values of the -M calls, subtract them from the current
unix time stamp, convert both times to date strings, and then compare
the dates.  For example:

#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw/strftime/;
my $date1 = m_to_date(-M 'file1.txt');
my $date2 = m_to_date(-M 'file2.txt');
if ($date1 eq $date2) {
   # copy the file
}
sub m_to_date {
   my $days_ago = shift;
   my $ts = time() - ($days_ago * 24 * 60 * 60);
   my $date = strftime('%Y-%m-%d', localtime($ts));
   return $date;
}
__END__

Paul Lalli


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Unique elements in an array

2007-10-05 Thread Rob Dixon

Paul Lalli wrote:


On Oct 5, 9:09 am, [EMAIL PROTECTED] (Robert Hicks) wrote:


The scenario: I have a file with ship id numbers. It gets updated
several times a day (by database query) and I want to find all the
new_ships that have been added to it.

sub incremental_update {
 print Doing incremental update.\n;
 open $FH, '+', $ships_file or croak Cannot open $ships_file!\n;

 # Get all the ships in it
 my @ships = do { local $/; $FH };


Are you aware each element of @ships has a newline attached?  Is that
what you want?


Not quite! That puts the entire file's contents into one element of
@ships. Try this:

 open my $fh, '', $ships_file or croak Cannot open $ships_file: $!;
 chomp (my @ships = $fh);
 close $fh;

HTH,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Unique elements in an array

2007-10-05 Thread Robert Hicks

It might matter so:

I have a file with the ship numbers. I update it via a db table. If I 
update during the same day I only want to put the new ships into the file.


Hope that is clearer.

Robert

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Unique elements in an array

2007-10-05 Thread Robert Hicks
The scenario: I have a file with ship id numbers. It gets updated 
several times a day (by database query) and I want to find all the 
new_ships that have been added to it.


sub incremental_update {
print Doing incremental update.\n;
open $FH, '+', $ships_file or croak Cannot open $ships_file!\n;

# Get all the ships in it
my @ships = do { local $/; $FH };

my %seen = ( );
my @new_ships = ( );

foreach my $item (@ships) { $seen{$item} = 1 }

# Getall the LLY_NUMS from the table
$sql = qq{ SELECT LLY_NUM FROM AMVER_VESS WHERE ONPLOT_FLAG = 'T' };
$sth = $dbh-prepare( $sql );
$sth-execute or die Can't execute SQL statement: $DBI::errstr\n;

while ( my @row = $sth-fetchrow-array ) {
foreach my $lly_num (@row) {
unless ( $seen{$lly_num} ) {
# it isn't in %seen, so add it to @new_ships
push( @new_ships, $lly_num );
}
}
print $FH @new_ships\n;
}
close $FH;
}


I just want to make sure my logic is right on this. If $lly_num isn't in 
the %seen hash then it gets added to the @new_ships array. Then all the 
new ships are written to the file.


A secondary question: When opening the file is + correct if I want to 
write back to it or should I use + ?


Robert

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Unique elements in an array

2007-10-05 Thread Paul Lalli
On Oct 5, 9:09 am, [EMAIL PROTECTED] (Robert Hicks) wrote:
 The scenario: I have a file with ship id numbers. It gets updated
 several times a day (by database query) and I want to find all the
 new_ships that have been added to it.

 sub incremental_update {
  print Doing incremental update.\n;
  open $FH, '+', $ships_file or croak Cannot open $ships_file!\n;

  # Get all the ships in it
  my @ships = do { local $/; $FH };

Are you aware each element of @ships has a newline attached?  Is that
what you want?

  my %seen = ( );
  my @new_ships = ( );

There is no need to initialize arrays/hashes to ( ) explicitly.  Perl
does that automatically.


  foreach my $item (@ships) { $seen{$item} = 1 }

  # Getall the LLY_NUMS from the table
  $sql = qq{ SELECT LLY_NUM FROM AMVER_VESS WHERE ONPLOT_FLAG = 'T' };
  $sth = $dbh-prepare( $sql );
  $sth-execute or die Can't execute SQL statement: $DBI::errstr\n;

  while ( my @row = $sth-fetchrow-array ) {
  foreach my $lly_num (@row) {

This is redundant.  You have only one column in your SELECT clause.
Therefore @row will only contain one element.  Eliminate one of the
loops, and make this:

while (my ($lily_num) = $sth-fetchrow-array()) {

  unless ( $seen{$lly_num} ) {
  # it isn't in %seen, so add it to @new_ships
  push( @new_ships, $lly_num );
  }
  }
  print $FH @new_ships\n;
  }
  close $FH;

 }

 I just want to make sure my logic is right on this. If $lly_num isn't in
 the %seen hash then it gets added to the @new_ships array. Then all the
 new ships are written to the file.

Yes.

 A secondary question: When opening the file is + correct if I want to
 write back to it or should I use + ?

Neither.  See `perldoc -f open`:
You can put a '+' in front of the '' or '' to
 indicate that you want both read and write access to
 the file; thus '+' is almost always preferred for
 read/write updates--the '+' mode would clobber the
 file first.  You can't usually use either read-write
 mode for updating textfiles, since they have
 variable length records.

Just open the file once for reading, read whatever you need, and then
open the file again for writing (or appending), and print whatever you
want to print.

Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




cgi Pain!!!

2007-10-05 Thread Pat Rice
hi all
have the following problem

I cannot for the life of me get the following working:

 28 testing the header
 29 print header;
 30 print start_html(Thank You);
 31 print h2(Thank You);
 32
 33 my $i;
 34 foreach $i (@vmsupportTest)
 35 {
 36print h3($i );
 37 }

excuse comments (#) as I was trouble shooing the problem but to no avail, it
does work when I run it from the commnad line.
I am not sure if I am implementing line 36 correctly, as I can see that
there is informaion in the array, but it is not being sent to the screen.

Thanks
Pat



  1 #!/usr/bin/perl -w
  2 use CGI qw(-debug :standard);
  3 use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  4 use strict;
  5  adding the bits from teat.pl
  6 use warnings;
  7 #use LWP::Simple;
  8 use LWP::Simple qw(!head);
  9 use diagnostics;
 10 use URI;
 11
 12
 13
 14
 15
 16 ## setting up the variables for the form
 17 my $query;
 18 my $p;
 19 my $variable_name = param(srNumber);
 20 #my $variable_name = 194951021;
 21
 22
 23 [EMAIL PROTECTED] = `/usr/bin/perl mysecondperlscript.pl`;
 24
 25 my @vmsupportTest=`/usr/bin/perl
/var/www/cgi-bin/downLoadTeatbook4.pl
http://test.test.com/pending/$variable_name`;
 26
 27 print @supportTest;
 28 #testing the header
 29 #print header;
 30 #print start_html(Thank You);
 31 #print h2(Thank You);
 32
 33 #my $i;
 34 #foreach $i (@vmsupportTest)
 35 #{
 36 #   print h3($i );
 37 #}
 38


Re: Regular expression over multiple lines issues

2007-10-05 Thread [EMAIL PROTECTED]
Thanks!!!

On Sep 27, 9:05 pm, [EMAIL PROTECTED] (John W. Krahn) wrote:
 [EMAIL PROTECTED] wrote:
  Hello,

 Hello,



  I'm having a problem with a regular expression in some code that I
  inherited.

  I'm reading from a file and looking for certain parameters (mrn, enc,
  date, report_subtitle).

  The file looks like this:

  %%start
  %mrn%02333699
  %fac%A
  %gpi%
  %enc%972014
  %date%07/07/2007^14:29
  %report_type%amit
  %report_subtitle%OCR (nut
  %phys_id1%GLENN
  %phys_id2%RAJPAL
  %%Pages: 5
  %!PS-Adobe
  %%Title: (orderNotes_1)
  %%CreationDate: 09/11/07 14:29
  %%For: System Man PRINGLE, GLENN O
  showpage
  %%PageTrailer
  end
  %%Trailer
  %%thisWasThePreviousStart
  %mrn%02333699
  %enc%072014
  %date%09/30/2007^23:59
  %report_type%PCM
  %report_subtitle%Order Confirmation Report (nut
  %phys_id1%PRINGLE

  My problem is that the regular expression is only displaying the first
  item (mrn) but it won't display the others (enc, date,
  report_subtitle)

 You have a single pattern so it should display all of them or none of them.

  My code is on the following line

  #!/usr/bin/perl

 use warnings;
 use strict;

  #open(FILE, 3688001.ps);
  open(FILE, 1234567.txt);

 You should *always* verify that the file opened correctly:

 open FILE, '', '1234567.txt' or die Cannot open '1234567.txt' $!;

  while ($buf = FILE) {

 while ( my $buf = FILE ) {

 This reads a single line into the $buf variable.

  #print $buf\n;

  if ($buf =~ m/\%mrn\%(\d+).+
  \%enc\%(\d+).+
  \%date\%
  (\d{1,2})\/
  (\d{1,2})\/
  (\d{1,2})\^.+
  ^\%report_subtitle\%(.+)\(
  /xms) {

 Your regular expression is trying to match multiple lines but there is only
 one line in $buf.

  #
  ##debug messages for matches
  #
   print HERE -- 1=$1\n if (defined($1));
   print HERE -- 2=$2\n if (defined($2));
   print HERE -- 3=$3\n if (defined($3));
   print HERE -- 4=$4\n if (defined($4));
   print HERE -- 5=$5\n if (defined($5));
   print HERE -- 6=$6\n if (defined($6));
   print HERE -- 7=$7\n if (defined($7));

 You are using the numeric variables inside an if conditional and all the
 captures match one or more characters so the defined test is superfluous.

}
  }

  1;

 The last statement evaluates as true rule is only required for modules.

  I would appreciate if anybody can tell me what I'm doing wrong or
  missing.

 #!/usr/bin/perl
 use warnings;
 use strict;

 #open FILE, '', '3688001.ps' or die Cannot open '3688001.ps' $!;
 open FILE, '', '1234567.txt' or die Cannot open '1234567.txt' $!;

 while ( my $buf = FILE ) {
  #print $buf\n;

  print TEXT if $buf =~ /%mrn%(\d+)/;
 HERE -- 1=$1
 TEXT
  print TEXT if $buf =~ /%enc%(\d+)/;
 HERE -- 2=$1
 TEXT
  print TEXT if $buf =~ /%date%(\d+)\/(\d+)\/(\d+)/;
 HERE -- 3=$1
 HERE -- 4=$2
 HERE -- 5=$3
 TEXT
  print TEXT if $buf =~ /%report_subtitle%(.+)\(/;
 HERE -- 6=$1
 TEXT
  }

 __END__

 John
 --
 Perl isn't a toolbox, but a small machine shop where you
 can special-order certain sorts of tools at low cost and
 in short order.-- Larry Wall



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Compare two files

2007-10-05 Thread John W. Krahn

[EMAIL PROTECTED] wrote:

Hi


Hello,


I would like to compare two files by comparing the files dates.
If one file shows
ls -la May 12 2003 filename
and the other name shows the same date they are OK for me (I'm not
interested in the time part only the date of the file)
But if the dates are not the same I would like to copy one of the
files.
How do I do this in Perl?
if (file1 not equal in date file2) {
 cp file 1 someplace
}


( my $date1 = localtime +( stat 'file1' )[ 9 ] ) =~ s/\d+:\d+:\d+//;
( my $date2 = localtime +( stat 'file2' )[ 9 ] ) =~ s/\d+:\d+:\d+//;

if ( $date1 ne $date2 ) {
# copy 'file1', ...
}



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: cgi Pain!!!

2007-10-05 Thread Tom Phoenix
On 10/5/07, Pat Rice [EMAIL PROTECTED] wrote:

 it does work when I run it from the commnad line.

What's different between the command line and the actual environment?
Check that you're using the right environment variables and current
working directory, at least.

  19 my $variable_name = param(srNumber);

  25 my @vmsupportTest=`/usr/bin/perl
 /var/www/cgi-bin/downLoadTeatbook4.pl
 http://test.test.com/pending/$variable_name`;

Are you passing user parameters to the shell without checking them?
That's a security hole. Suppose the user's supplied variable name is
'something; rm -rf /'? Check that $variable_name matches a pattern
before you use it.

The rest of your code is too mangled to decipher. Is it what you're
actually running? You've commented out so much, and what's left
doesn't use its variables consistently. (What's @supportTest?) Next
time, try posting the actual code you're using.

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Hello and a question about perlform

2007-10-05 Thread Niels Will
Hello together,

my Name is Niels (26) and I'm from Germany being new on this mailinglist.

Right now I'm writing my final examination for my apprenticeship as an
IT Specialist and I have a question.

My program generates multiple reports with different filenames during a
foreach loop.

On the first file, the REPORT_TOP format is beeing used but not on the
second, third, and so on one.

I figured it out so far that it has something to do with the $- or $~
variable but I can't figure out how to 'reset' the filehandler to print
the _TOP format on the next generated file.

It would be really nice if someone could help me.

Thanks in advance

Niels





signature.asc
Description: OpenPGP digital signature


Re: Hello and a question about perlform

2007-10-05 Thread Chas. Owens
On 10/5/07, Niels Will [EMAIL PROTECTED] wrote:
snip
 On the first file, the REPORT_TOP format is beeing used but not on the
 second, third, and so on one.
snip

Perl formats are a pain, and probably should not be used.  Take a look
at the Perl6::Form module for a better solution.  The answer to your
question lies in how the format engine knows when to print a header.
It prints a header at the top of each page.  A page is defined as $=
lines - the number of lines in the header.  The current countdown is
stored in $-.  So if you want to force the next call to write to print
a header just set $- to 0.  You can also search perldoc perlvar for
more variables that control the format engine.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Unique elements in an array

2007-10-05 Thread Robert Hicks

Paul Lalli wrote:

On Oct 5, 9:09 am, [EMAIL PROTECTED] (Robert Hicks) wrote:

The scenario: I have a file with ship id numbers. It gets updated
several times a day (by database query) and I want to find all the
new_ships that have been added to it.

sub incremental_update {
 print Doing incremental update.\n;
 open $FH, '+', $ships_file or croak Cannot open $ships_file!\n;

 # Get all the ships in it
 my @ships = do { local $/; $FH };


Are you aware each element of @ships has a newline attached?  Is that
what you want?


 my %seen = ( );
 my @new_ships = ( );


There is no need to initialize arrays/hashes to ( ) explicitly.  Perl
does that automatically.



I actually did know this but I was looking at other code at the time and 
I brain farted.  : )




 foreach my $item (@ships) { $seen{$item} = 1 }

 # Getall the LLY_NUMS from the table
 $sql = qq{ SELECT LLY_NUM FROM AMVER_VESS WHERE ONPLOT_FLAG = 'T' };
 $sth = $dbh-prepare( $sql );
 $sth-execute or die Can't execute SQL statement: $DBI::errstr\n;

 while ( my @row = $sth-fetchrow-array ) {
 foreach my $lly_num (@row) {


This is redundant.  You have only one column in your SELECT clause.
Therefore @row will only contain one element.  Eliminate one of the
loops, and make this:

while (my ($lily_num) = $sth-fetchrow-array()) {


 unless ( $seen{$lly_num} ) {
 # it isn't in %seen, so add it to @new_ships
 push( @new_ships, $lly_num );
 }
 }
 print $FH @new_ships\n;
 }
 close $FH;

}



Cool, I learn something new every day with Perl.  : )

Thanks!

Robert

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Unique elements in an array

2007-10-05 Thread Robert Hicks

Rob Dixon wrote:

Paul Lalli wrote:


On Oct 5, 9:09 am, [EMAIL PROTECTED] (Robert Hicks) wrote:


The scenario: I have a file with ship id numbers. It gets updated
several times a day (by database query) and I want to find all the
new_ships that have been added to it.

sub incremental_update {
 print Doing incremental update.\n;
 open $FH, '+', $ships_file or croak Cannot open $ships_file!\n;

 # Get all the ships in it
 my @ships = do { local $/; $FH };


Are you aware each element of @ships has a newline attached?  Is that
what you want?


Not quite! That puts the entire file's contents into one element of
@ships. Try this:

 open my $fh, '', $ships_file or croak Cannot open $ships_file: $!;
 chomp (my @ships = $fh);
 close $fh;

HTH,

Rob


Thanks guys! That is enough for me to continue.

Robert

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/