[21968] in Perl-Users-Digest
Perl-Users Digest, Issue: 4190 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Nov 27 14:06:25 2002
Date: Wed, 27 Nov 2002 11:05:15 -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 Wed, 27 Nov 2002 Volume: 10 Number: 4190
Today's topics:
Re: Apache/MySQL/DBI problem (Tad McClellan)
Re: Apache/MySQL/DBI problem <cpryce@pryce.net>
Re: back quotes in NT (Marc)
beginner in perl <regus_nospam@pandora.be>
Re: beginner in perl <nospam@nospam.org>
Re: beginner in perl (Tad McClellan)
CGI.pm table question <agw@cs.columbia.edu>
Re: CGI.pm table question <dave@dave.org.uk>
Re: Counting the number of records in @rows (Helgi Briem)
Re: Counting the number of records in @rows <ubl@schaffhausen.de>
Re: Counting the number of records in @rows <clay@panix.com>
Re: Getopt::Std - more info. (qanda)
instantiating HTML::TokeParser with scalar from LWP <japhauthor@yahoo.com>
Please take my two-minute software engineering survey! <jnleipzi@unity.ncsu.edu>
Re: regexp (multiple lines) substitution <peter_news@web.de>
Re: scripts run by cron.sh script can't open files <nobody@dev.null>
Re: scripts run by cron.sh script can't open files <tomweeks@charter.net>
Re: scripts run by cron.sh script can't open files (Tad McClellan)
Re: scripts run by cron.sh script can't open files <tomweeks@charter.net>
Re: scripts run by cron.sh script can't open files (Gary E. Ansok)
Re: splice() is fast when shrinking and slow when growi ctcgag@hotmail.com
using strict and warnings <bryan@akanta.com>
Re: using strict and warnings <kurzhalsflasche@netscape.net>
Re: using strict and warnings <dave@dave.org.uk>
Re: using strict and warnings (Tad McClellan)
Re: using strict and warnings <cpryce@pryce.net>
Windows multiple process questions with either Win32:Pr (Mark)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 27 Nov 2002 10:24:06 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Apache/MySQL/DBI problem
Message-Id: <slrnau9sd6.2vv.tadmc@magna.augustmail.com>
cp <cpryce@pryce.net> wrote:
> Using warnings (use warnings or #!/path/to/perl -w) will print
> those warnings to STDOUT. STDOUT for a script invoked from the server is the
^^^^^^ ^^^^^^
> Web server's error log. Those warnings are an enormous help to debugging.
> They come free with Perl. Why not use them?
You meant STDERR there.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 27 Nov 2002 12:25:09 -0600
From: cp <cpryce@pryce.net>
Subject: Re: Apache/MySQL/DBI problem
Message-Id: <BA0A6A25.12818%cpryce@pryce.net>
in article slrnau9sd6.2vv.tadmc@magna.augustmail.com, Tad McClellan at
tadmc@augustmail.com wrote on 11/27/02 10:24 AM:
> cp <cpryce@pryce.net> wrote:
>
>> Using warnings (use warnings or #!/path/to/perl -w) will print
>> those warnings to STDOUT. STDOUT for a script invoked from the server is the
> ^^^^^^ ^^^^^^
>> Web server's error log. Those warnings are an enormous help to debugging.
>> They come free with Perl. Why not use them?
>
>
> You meant STDERR there.
>
Thank you. Yes I did.
cp
------------------------------
Date: 27 Nov 2002 10:33:38 -0800
From: brownjenkn@aol.com (Marc)
Subject: Re: back quotes in NT
Message-Id: <28b74d11.0211271033.166137f6@posting.google.com>
Thanks for the help everyone. Turns out that the .exe I was using,
db2cmd.exe, for db2connect was causing a cmd.exe to start up for every
user that triggered it. That is, whenever the script was called,
there was a line:
system "db2cmd db2 connect to . . .etc."
getting run. Over time, there were several cmd.exe processes started
up on the server, sucking up memory. By adding a /c, the command I
wanted to use executes and then terminates the cmd.exe. That is
system "db2cmd /c db2 connect to.... etc." fixes the problem. The
recommendation over here to use backticks wasn't a solution at all.
Thanks again.
Marc
------------------------------
Date: Wed, 27 Nov 2002 17:54:20 GMT
From: "regus" <regus_nospam@pandora.be>
Subject: beginner in perl
Message-Id: <gF7F9.22987$Ti2.4663@afrodite.telenet-ops.be>
Hi,
I'm a beginner in Perl. I have written a simple script. Now i want it to run
each day once, for example each morning How can i do that in perl ?
Bart
------------------------------
Date: Wed, 27 Nov 2002 12:58:07 -0500
From: "Christian Caron" <nospam@nospam.org>
Subject: Re: beginner in perl
Message-Id: <as313f$90v2@nrn2.NRCan.gc.ca>
"regus" <regus_nospam@pandora.be> wrote in message
news:gF7F9.22987$Ti2.4663@afrodite.telenet-ops.be...
> Hi,
>
> I'm a beginner in Perl. I have written a simple script. Now i want it to
run
> each day once, for example each morning How can i do that in perl ?
>
> Bart
>
>
This is not a Perl related question.
If on Unix, you can use cron jobs. On Windows, I think there is something
similar to cron jobs.
Christian
------------------------------
Date: Wed, 27 Nov 2002 12:15:07 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: beginner in perl
Message-Id: <slrnaua2tb.3c7.tadmc@magna.augustmail.com>
regus <regus_nospam@pandora.be> wrote:
> I'm a beginner in Perl. I have written a simple script. Now i want it to run
> each day once, for example each morning How can i do that in perl ?
Get up each morning and type the name of the program at
a command prompt. :-)
What you are asking for is the job of the Operating System,
not the job of a programming language.
You have an OS question, not a Perl question.
Ask in an OS newsgroup such as:
comp.os.linux.*
comp.os.ms-windows.*
comp.os.apple
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 27 Nov 2002 12:21:16 -0500
From: Art Werschulz <agw@cs.columbia.edu>
Subject: CGI.pm table question
Message-Id: <ylafztnrks3.fsf@smolyak.cs.columbia.edu>
Hi.
How can one use CGI.pm to produce the tag "<table border>"?
Printing $cgi->start_table(-border->undef) only produces "<table>".
Thanks.
--
Art Werschulz (8-{)} "Metaphors be with you." -- bumper sticker
GCS/M (GAT): d? -p+ c++ l u+(-) e--- m* s n+ h f g+ w+ t++ r- y?
Internet: agw@cs.columbia.edu<a href="http://www.cs.columbia.edu/~agw/">WWW</a>
ATTnet: Columbia U. (212) 939-7061, Fordham U. (212) 636-6325
------------------------------
Date: Wed, 27 Nov 2002 18:36:08 +0000
From: "Dave Cross" <dave@dave.org.uk>
Subject: Re: CGI.pm table question
Message-Id: <pan.2002.11.27.18.36.07.881362@dave.org.uk>
On Wed, 27 Nov 2002 12:21:16 +0000, Art Werschulz wrote:
> Hi.
>
> How can one use CGI.pm to produce the tag "<table border>"?
> Printing $cgi->start_table(-border->undef) only produces "<table>".
The attributes for an HTML tag are usually passed to the CGI.pm function
in an anonymous hash.
$cgi->start_table({-border => 1})
hth,
Dave...
--
And crawling on the planet's face, some insects called the human race
Lost in time, and lost in space. And meaning.
------------------------------
Date: Wed, 27 Nov 2002 16:15:08 GMT
From: helgi@decode.is (Helgi Briem)
Subject: Re: Counting the number of records in @rows
Message-Id: <3de4ef65.3676009962@news.cis.dfn.de>
On Wed, 27 Nov 2002 15:56:43 +0000 (UTC), Clay Irving
<clay@panix.com> wrote:
>In article <as2mjp$4a5@netnews.proxy.lucent.com>, Tulan W. Hu wrote:
>> "Rubber Duck" <rubberducky703@hotmail.com> wrote in...
>
>>> How can i tell how many rows (i.e. records that there are in @rows??
>>
>>> I tried my $numberofrecords = @rows but it always gives me 1.
>
>> my $numberofrecords = $#rows + 1;
>
>I'm surprised no on mentioned:
>
> print scalar(@rows), "\n";
Why should they?
$numberofrecords = @rows works just fine IF
there is more than one record in @rows.
--
Regards, Helgi Briem
helgi AT decode DOT is
A: Top posting
Q: What is the most irritating thing on Usenet?
- "Gordon" on apihna
------------------------------
Date: Wed, 27 Nov 2002 17:23:14 +0100
From: Malte Ubl <ubl@schaffhausen.de>
Subject: Re: Counting the number of records in @rows
Message-Id: <as2ur2$6hh$1@news.dtag.de>
Clay Irving wrote:
>>my $numberofrecords = $#rows + 1;
>
>
> I'm surprised no on mentioned:
>
> print scalar(@rows), "\n";
Because that is just another way to do it. Effect is totally equivalent.
->malte
------------------------------
Date: Wed, 27 Nov 2002 16:36:55 +0000 (UTC)
From: Clay Irving <clay@panix.com>
Subject: Re: Counting the number of records in @rows
Message-Id: <slrnau9t57.9b.clay@panix1.panix.com>
In article <3de4ef65.3676009962@news.cis.dfn.de>, Helgi Briem wrote:
> On Wed, 27 Nov 2002 15:56:43 +0000 (UTC), Clay Irving
><clay@panix.com> wrote:
>>In article <as2mjp$4a5@netnews.proxy.lucent.com>, Tulan W. Hu wrote:
>>> "Rubber Duck" <rubberducky703@hotmail.com> wrote in...
>>
>>>> How can i tell how many rows (i.e. records that there are in @rows??
>>>
>>>> I tried my $numberofrecords = @rows but it always gives me 1.
>>
>>> my $numberofrecords = $#rows + 1;
>>
>>I'm surprised no on mentioned:
>>
>> print scalar(@rows), "\n";
>
> Why should they?
>
> $numberofrecords = @rows works just fine IF
> there is more than one record in @rows.
Hmm. Maybe I'm confoosed.
Why does it not work if there are no records in @rows?
Code:
#!/usr/local/bin/perl5.6.1 -w
my @rows;
my $numberofrecords = $#rows + 1;
print "$numberofrecords\n";
my $anotherway = @rows;
print "$anotherway\n";
print scalar(@rows), "\n";
Result:
0
0
0
Code:
#!/usr/local/bin/perl5.6.1 -w
my @rows = "This is a record";
my $numberofrecords = $#rows + 1;
print "$numberofrecords\n";
my $anotherway = @rows;
print "$anotherway\n";
print scalar(@rows), "\n";
Result:
1
1
1
--
Clay Irving <clay@panix.com>
------------------------------
Date: 27 Nov 2002 08:18:46 -0800
From: fumail@freeuk.com (qanda)
Subject: Re: Getopt::Std - more info.
Message-Id: <62b4710f.0211270818.4facff33@posting.google.com>
Thanks for all the replies but I'm still confused.
I thought of using @ARGV but still don't see how I can KNOW when I
have the extra argument, ARGV seems to change after getopts.
Here's a sample script. (critism appreciated - come from C background
- still learning perl). Sorry for the length, I've cut out most of
the comments and body to keep it shorter.
*** START OF SCRIPT ***
#!/usr/bin/perl -w
use warnings;
use strict;
use vars qw( $arg_a $arg_b $ec );
init();
parse_args();
# Use end block to set exit value for program.
END
{
$? = $ec;
}
# Initialise global exit code.
sub init
{
$ec = 0;
}
sub usage($)
{
$ec = $_[0];
# Print man page if -m option was given.
if( $ec == 4 )
{
use Pod::Usage;
my $pv = 2;
my $pf = \*STDOUT;
$ec = $_[0];
pod2usage( -verbose => $pv ,
-output => $pf );
}
else
{
die <<EOF
Usage: extra_args.pl options
extra_args.pl [-h|-m]
Options:
-a <starting ciruit_id>
-b <ending circuit_id>
-h display this help screen
-m display the manual page
Examples:
extra_args.pl -a 123 -b 456
extra_args.pl -h|-m
EOF
}
}
sub parse_args
{
use Getopt::Std;
our( $opt_a, $opt_b, $opt_h, $opt_m );
# Print usage if we have no arguments.
if( @ARGV == 0 )
{
print STDERR "Arguments are mandatory.\n";
usage(1);
}
# Print usage if we have invalid options.
getopts('a:b:hm') or usage(2);
# Print usage if we have help option.
if( defined $opt_h )
{
usage(3);
}
# Print manual page if we have man option.
if( defined $opt_m )
{
usage(4);
}
# Print usage if we have missing options.
if( !(defined $opt_a &&
defined $opt_b) )
{
print STDERR "Mandatory options were missing.\n";
usage(5);
}
# Check that all arguments are numeric.
if( $opt_a =~ /^(\d+)$/ &&
$opt_b =~ /^(\d+)$/ )
{
# Set our globals to the supplied arguments.
$arg_a = $opt_a;
$arg_b = $opt_b;
}
else
{
# Print usage if we have invalid arguments..
print STDERR "Some arguments were invalid.\n";
usage(6);
}
}
# POD documentation.
=pod
=head1 NAME
extra_args.pl - Simple tool for simple person - me!
=head1 SYNOPSIS
blah, blah, blah
=cut
*** END OF SCRIPT ***
Example runs
extra_args.pl
ok, exit code in system is 1
extra_args.pl -z
ok, exit code is 2
extra_args.pl -h
ok, exit code is 3
extra_args.pl -m
ok, exit code is 4
extra_args.pl -a 123
ok, exit code is 5
extra_args.pl -a 123 -b 456A
ok, exit code is 6
extra_args.pl -a 123 -b 456
ok, correct usage
extra_args.pl -a 123 -b 456 EXTRA_ARGUMENT
*** THIS IS THE PROBLEM - how to I detect this extra argument.
thanks again all.
------------------------------
Date: Wed, 27 Nov 2002 12:22:39 -0600
From: "George Matthew" <japhauthor@yahoo.com>
Subject: instantiating HTML::TokeParser with scalar from LWP
Message-Id: <uua3b88uqbe13e@corp.supernews.com>
Config is ActivePerl 5.6.1 Build 633 on Windows XP Home. LWP version is 5.64
as recommended in Burke's book "Perl and LWP". Chapter 2, Section 7 of said
tome states:
"Create a token stream object using one of these two constructors:
my $stream = HTML::TokeParser->new($filename)
|| die "Couldn't read HTML file $filename: $!";
or:
my $stream = HTML::TokeParser->new( \$string_of_html );"
My code uses the second form of constructor:
use strict;
use LWP;
use HTML::TokeParser;
use constant BATCH_SIZE => 1;
my $names_url = http://www.kleimo.com/random/name.cfm;
my $scraper = LWP::UserAgent->new;
for (my $i=0; $i < BATCH_SIZE; $i++)
{
my $names_html = $scraper->post($names_url,
[
"type" => "3",
"obscurity" => "1",
"number" => "30",
"GO" => "Generate Random Name(s)"
]);
die "Error: ", $names_html->status_line, "\n"
unless $names_html->is_success;
my $parser = HTML::TokeParser->new( \$names_html ) || die $!;
while(my $token = $parser->get_token)
{
; #results in error regardless of being NOOP or not
}
}
This results in the message "Not a GLOB reference at
C:/Perl/site/lib/HTML/PullParser.pm line 71", but does not die if I entirely
get rid of the while block, which, however, is the whole point ;) Regarding
the second form of TokeParser constructor, the docs say:
"If the argument is a reference to a plain scalar, then this scalar is taken
to be the literal document to parse. The value of this scalar should not be
changed before all tokens have been extracted."
First I assume it is user error on my part, since the example from the book
nicely fleshes out the docs. Could my problem be that the html hasn't been
fully returned?! If so how do I check for this? Or am I having a DOH moment?
Happy Thanksgiving in Advance
G
------------------------------
Date: Wed, 27 Nov 2002 17:11:54 GMT
From: Jeremy Leipzig <jnleipzi@unity.ncsu.edu>
Subject: Please take my two-minute software engineering survey!
Message-Id: <BA0A671A.14C10%jnleipzi@unity.ncsu.edu>
http://zigster.com/survey.html
This two-minute survey will be used in my course term paper to study various
work issues as they relate to software engineering.
This survey is geared toward developers who are directly involved in
software engineering. All questions on the survey are optional.
Thank you for your time!
Jeremy Leipzig
NCSU Computer Science
------------------------------
Date: Wed, 27 Nov 2002 17:28:26 +0100
From: Peter Schaffrath <peter_news@web.de>
Subject: Re: regexp (multiple lines) substitution
Message-Id: <c2r9uu8kb30tm6mod3ooufj7rfevd6uubp@4ax.com>
On Wed, 27 Nov 2002 08:08:20 -0600, tadmc@augustmail.com (Tad
McClellan) wrote:
>Using a regex to "parse" HTML is most often a really bad idea,
>there are a thousand-and-one gotchas.
OK, in general your right. But regarding the end of your post: I
produced the HTML code myself, and there are exactly 4 instances of
this <th>total:</th> construct in each file (and all lower case). I
only wanted to use perl so that I didn't have do to these changes with
an editor. Now use for perfect HTML parsing :-)
I should have pointed this out in my original post. Maybe this was
misleading.
To emphesize what I'm trying to do: I want to change the th tag to be
an td tag. But only, if the tag contains "total:", not other th tags.
And I used the \s for whitespace, as the all lines may start or end
with whitespace (the code is produced and intended by a program).
>Don't capture the newlines if you don't want to put them back in:
>
> \s*(total:)\s*
>
>> |gxs;
Actually I don't care weather the newlines are deleted or not. The
tags should be changed - that's all.
>You do not need the s///s option. It does not change anything
>for the pattern you are using.
>> What is weird, when I count the number
>> of substitutions with
>> $count = ($data =~ s|... |);
>> it says 4 occurences have been changed.
Should have said this better: Even if the tags hadn't been changed,
the count said it changed 4 places. So the code seems to change
somthing, but nothing visible.
>All your s/// does is change some of the "h" characters
>it finds into "d" characters. :-)
That's what I want it to do :-) But it doesn't. I try to understand
why not and how to do it.
>You probably missed that the s/// had actually substituted something.
Don't think so. I used sdiff all the time and searched for differences
(sdiff old-file new-file | grep "|", sdiff uses | to show differing
lines).
>If you control the generation of this HTML that you are processing,
>and/or don't mind if it doesn't work on 100% of all data, only
>then might you consider pattern matching against HTML data
>rather than doing it properly with a module.
See above...
Peter
------------------------------
Date: Wed, 27 Nov 2002 16:16:10 GMT
From: Andras Malatinszky <nobody@dev.null>
Subject: Re: scripts run by cron.sh script can't open files
Message-Id: <3DE4EF76.3000803@dev.null>
Tom Weeks wrote:
[...]
> I have files on a site I maintain that need to be backed up every night. The
> web host has set up a cron job to run every night. Using their template I
> wrote [a] shell script (cron.sh)[...], but
> the backup script is not doing its job, and there are no entries in the
> error log for the site. When I run cron.sh from my browser, the files are
> updated just as they should be, but cron.sh returns a "premature end of
> scriipt headers" error. (I would have expected a "malformed header" error).
>
> To check that the cron daemon was actually calling cron.sh, I wrote the
> following test script (crontest.pl) which writes to a .dat file if
> successful, and writes returned HTML to cronlog.log.
>
> ______________________________________
>
> #!/usr/local/bin/perl
>
>
> if (!open T, ">>$ENV{DOCUMENT_ROOT}/crontest.dat"){
> print "Content-type: text/html\n\n";
> print "Couldn't Open File!\n";
> exit;
> }
> print T "Cron Ran".`date`."\n";
> close T;
> print "Content-type: text/html\n\n";
> print "done ".`date`."\n";
> ___________________________________________
>
> I rewrote cron.sh as follows:
> __________________________________
> #!/bin/sh
>
> /u/web/(our_usename)/cgi-local/crontest.pl
>
>>>/u/web/(our_username)/cgi-local/cronlog.log
>>>
> ___________________________________
>
> When I run this cron.sh from my browser, "Cron Ran (the date & time)" is
> written to crontest.dat, and, "done (date & time)" is written to
> cronlog.log. (The "premature end of script headers" error is written to the
> site's errror.log file as expected--ostensibly because the shell script was
> run from a browser and the shell script returned either no data or improper
> CGI data).
>
> When the cron daemon runs cron.sh my little error trap above writes
> "Couldn't open file" to my cronlog.log file. Of course, nothing is written
> to crontest.dat because it couldn't be opened. And that is the
> question---WHY can crontest.pl open and write to crontest.dat when it is
> called by cron.sh run by a browser, but NOT when it is called by cron.sh run
> by my server's cron daemon? I have tried it using both
> $ENV{DOCUMENT_ROOT}/etc. as the path to crontest.dat and the literal system
> path (/u/web/etc./etc.). I believe if I figure out how to make this "test"
> script work I will have figured out how to make the "real" backup script
> work.
Your scripts run as different users when you run them via a browser or
via cron. Check who has write permission to crontest.dat; the reason for
cron's failure to write to it is probably that the cron daemon does
not have permission to write it.
A useful way to find out what's happening when you open files is to do
something along the lines of
open FILE, '>/usr/memyselfi/myfiles/the_file.dat' or die "Couldn't open
the_file.dat; reason: $!\n";
$! will contain a brief error description and so if the open fails, the
program will stop and you will have an entry in the error log saying
something like
Couldn't open the_file.dat; reason: Disk quota exceeded.
die is documented in perlfunc; $! is documented in perlvar
Assuming you will ultimately succeed, think about the security
implications: if you can run your backup script via a browser, so can
anybody else, and you should think about what what would happen if
someone were to run that script at 2:41 pm on a Tuesday afternoon. Is
that a problem? Is that going to wipe out something important?
------------------------------
Date: Wed, 27 Nov 2002 11:34:10 -0800
From: "Tom Weeks" <tomweeks@charter.net>
Subject: Re: scripts run by cron.sh script can't open files
Message-Id: <uu9sto6ov9grf8@corp.supernews.com>
"Andras Malatinszky" <nobody@dev.null> wrote in message
news:3DE4EF76.3000803@dev.null...
>
>
> Your scripts run as different users when you run them via a browser or
> via cron. Check who has write permission to crontest.dat; the reason for
> cron's failure to write to it is probably that the cron daemon does
> not have permission to write it.
>
> A useful way to find out what's happening when you open files is to do
> something along the lines of
>
> open FILE, '>/usr/memyselfi/myfiles/the_file.dat' or die "Couldn't open
> the_file.dat; reason: $!\n";
>
> $! will contain a brief error description and so if the open fails, the
> program will stop and you will have an entry in the error log saying
> something like
>
> Couldn't open the_file.dat; reason: Disk quota exceeded.
>
> die is documented in perlfunc; $! is documented in perlvar
Thanks for this help. I will use "die" for error-trapping. That should tell
me WHY the files can't be opened for writing.
>
> Assuming you will ultimately succeed, think about the security
> implications: if you can run your backup script via a browser, so can
> anybody else, and you should think about what what would happen if
> someone were to run that script at 2:41 pm on a Tuesday afternoon. Is
> that a problem? Is that going to wipe out something important?
All the scripts which read from & write to the data files to be backed up
use flock. Since the backup script also uses flock, there shouldn't be a
problem with data loss because of "simultaneous" access.
------------------------------
Date: Wed, 27 Nov 2002 10:48:53 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: scripts run by cron.sh script can't open files
Message-Id: <slrnau9trl.2vv.tadmc@magna.augustmail.com>
Tom Weeks <tomweeks@charter.net> wrote:
> This is a long post, but I hope someone will take the time to read it and
> can help me. I am getting desperate.
You have lots of help available to you that you seem to have
avoided (see below). That has cost you.
If you follow good practices, then you will experience a great
deal less desperation. :-)
> #!/usr/local/bin/perl
Ask perl to help you find common programming mistakes:
#!/usr/local/bin/perl
use warnings;
use strict;
and if this is a CGI program, you should probably have taint
checking (-T) turned on too.
and if this is a CGI program *that you are debugging*, you
might also want:
use CGI::Carp qw(fatalsToBrowser); # take this out before production!
But anyone using Perl for CGI programming would already know
that, because they would have already tried this:
perldoc -q CGI
and seen:
How can I get better error messages from a CGI program?
:-)
> open W, "$ENV{DOCUMENT_ROOT}/artwork_database/workorders/artindex.dat";
You should always, yes *always*, check the return value from open():
open W, "$ENV{DOCUMENT_ROOT}/artwork_database/workorders/artindex.dat" or
die 'could not open ',
"$ENV{DOCUMENT_ROOT}/artwork_database/workorders/artindex.dat",
" reason: $!";
Then check your server logs to see what the value of the $!
special variable was (file not found, permission denied...).
Be careful! $ENV{DOCUMENT_ROOT} is tainted...
Does your cron daemon even set the value of that env var?
It is "normally" added by a web server.
There is no web server involved in your cron job, is there?
> According to tech support cron.sh is running every night as scheduled, but
> the backup script is not doing its job, and there are no entries in the
^^^^^^^^^^^^^
> error log for the site.
^^^
You did not ask for any.
You ask for error messages by first checking open()'s return
value, and then taking "appropriate action" if the open()
failed. Calling die() or carp() is often "appropriate".
> if (!open T, ">>$ENV{DOCUMENT_ROOT}/crontest.dat"){
> print "Content-type: text/html\n\n";
> print "Couldn't Open File!\n";
> When the cron daemon runs cron.sh my little error trap above writes
> "Couldn't open file" to my cronlog.log file.
See what it says when you modify your print() to:
print "Couldn't Open File! $!\n";
^^
> And that is the
> question---WHY can crontest.pl open and write to crontest.dat
and this is the answer: output the value of $!
:-)
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 27 Nov 2002 12:47:51 -0800
From: "Tom Weeks" <tomweeks@charter.net>
Subject: Re: scripts run by cron.sh script can't open files
Message-Id: <uua17tg1sfjfdf@corp.supernews.com>
"Tad McClellan" <tadmc@augustmail.com> wrote in message
news:slrnau9trl.2vv.tadmc@magna.augustmail.com...
> Tom Weeks <tomweeks@charter.net> wrote:
>
> > This is a long post, but I hope someone will take the time to read it
and
> > can help me. I am getting desperate.
>
>
> You have lots of help available to you that you seem to have
> avoided (see below). That has cost you.
>
> If you follow good practices, then you will experience a great
> deal less desperation. :-)
>
>
> > #!/usr/local/bin/perl
>
>
> Ask perl to help you find common programming mistakes:
>
> #!/usr/local/bin/perl
> use warnings;
> use strict;
>
> and if this is a CGI program, you should probably have taint
> checking (-T) turned on too.
>
> and if this is a CGI program *that you are debugging*, you
> might also want:
>
> use CGI::Carp qw(fatalsToBrowser); # take this out before production!
>
>
> But anyone using Perl for CGI programming would already know
> that, because they would have already tried this:
>
> perldoc -q CGI
>
> and seen:
>
> How can I get better error messages from a CGI program?
>
> :-)
>
>
> > open W, "$ENV{DOCUMENT_ROOT}/artwork_database/workorders/artindex.dat";
>
>
> You should always, yes *always*, check the return value from open():
>
> open W, "$ENV{DOCUMENT_ROOT}/artwork_database/workorders/artindex.dat"
or
> die 'could not open ',
> "$ENV{DOCUMENT_ROOT}/artwork_database/workorders/artindex.dat",
> " reason: $!";
>
> Then check your server logs to see what the value of the $!
> special variable was (file not found, permission denied...).
>
>
> Be careful! $ENV{DOCUMENT_ROOT} is tainted...
>
>
> Does your cron daemon even set the value of that env var?
>
> It is "normally" added by a web server.
>
> There is no web server involved in your cron job, is there?
>
>
> > According to tech support cron.sh is running every night as scheduled,
but
> > the backup script is not doing its job, and there are no entries in the
> ^^^^^^^^^^^^^
> > error log for the site.
> ^^^
>
> You did not ask for any.
>
> You ask for error messages by first checking open()'s return
> value, and then taking "appropriate action" if the open()
> failed. Calling die() or carp() is often "appropriate".
>
>
> > if (!open T, ">>$ENV{DOCUMENT_ROOT}/crontest.dat"){
> > print "Content-type: text/html\n\n";
> > print "Couldn't Open File!\n";
>
>
> > When the cron daemon runs cron.sh my little error trap above writes
> > "Couldn't open file" to my cronlog.log file.
>
>
> See what it says when you modify your print() to:
>
> print "Couldn't Open File! $!\n";
> ^^
>
>
> > And that is the
> > question---WHY can crontest.pl open and write to crontest.dat
>
>
> and this is the answer: output the value of $!
>
> :-)
>
>
> --
> Tad McClellan SGML consulting
> tadmc@augustmail.com Perl programming
> Fort Worth, Texas
All of your advice is, of course, on the money. I was not using all the
tools at my disposal. I will now. However, since the problem is most likely
caused by file permissions, I wonder if the only solution will be to make
the files in question "world" writable (chmod 666). This carries its own
security issues.
------------------------------
Date: Wed, 27 Nov 2002 18:18:45 +0000 (UTC)
From: ansok@alumni.caltech.edu (Gary E. Ansok)
Subject: Re: scripts run by cron.sh script can't open files
Message-Id: <as32a5$d2r$1@naig.caltech.edu>
In article <uu9prap3qu0kf0@corp.supernews.com>,
Tom Weeks <tomweeks@charter.net> wrote:
>
>I have files on a site I maintain that need to be backed up every night. The
>web host has set up a cron job to run every night. Using their template I
>wrote the following shell script (cron.sh):
>_____________________________
>#!/bin/sh
>
>/u/web/(our_username)/cgi-local/artwork_daily_backup.pl
>_______________________________
>
>artwork_daily_backup.pl resides in /cgi-local/. Here is the code:
>_____________________________
>#!/usr/local/bin/perl
>
>open W, "$ENV{DOCUMENT_ROOT}/artwork_database/workorders/artindex.dat";
When you run in a cron process, you do not have the normal environment
variables that you get in an interactive or CGI session.
Who is setting the DOCUMENT_ROOT environment variable? Probably nobody,
which would account for your problems. You might want to modify cron.sh
to set this and any other environment variables you depend on.
I always write my open statements like this:
open W, $file or die "Cannot open '$file': $!";
which (assuming you redirect the output or let cron email it to you)
might have given you a clue as to what file it was trying to open
(and why it couldn't).
Alternatively, it could be a permissions problem or a current-directory
problem (though those look like absolute paths, which is good).
My next steps towards debugging a process like this would be to start
artwork_daily_backup.pl off with lines like
my $log_file = '/tmp/artwork_daily_backup.out';
open LOG, ">$log_file" or die "Cannot open '$log_file': $!";
print LOG "Starting artwork daily backup\n";
print LOG "DOCUMENT_ROOT is '$ENV{DOCUMENT_ROOT}'\n";
This will help verify a) that artwork_daily_backup.pl is in fact
started properly; and b) what the value we're seeing for the
environment variable is.
Gary Ansok
--
It's depressing that the words "secret agent"
have become synonymous with "sex maniac."
-- Sir James Bond
------------------------------
Date: 27 Nov 2002 16:39:34 GMT
From: ctcgag@hotmail.com
Subject: Re: splice() is fast when shrinking and slow when growing an array. Why?
Message-Id: <20021127113934.918$3G@newsreader.com>
ditchen@web.de (Patrick Ditchen) wrote:
> Thanks for your help.
> I know, that Perl doesn't have to move the real values stored in the
> array, when splicing. It only concerns the Array of pointers. An array
> is a list of pointers to seperate skalar values. I wonder, how Perl
> manages to reorganize this pointer-array after a splice().
I think (no real knowledge, just using how I would do it if I were writing
it) that perl uses the memmove function in C to slide the entire list of
pointers around. memmove is probably coded in machine language (and I
think on many machines it even uses special hardware support features) to
make it remarkably fast, much faster than doing
for($i=$start,$i<$end;$i++) a[$i]=a[$i+1];
Now all that thinking if for dense arrays. For sparse arrays, like we
have here, I don't know if perl tries to do something fancy or not.
(like knowing the last real element is 10_000_000, but the array is
20_000_000, so it doesn't have to store the last 10_000_000 undef, it just
knows they are there.)
> #!/usr/bin/perl
> print "Start: ", (localtime)[0], "\n" ;
> $#arr = 20_000_000 ;
> print "After building: ", (localtime)[0], "\n" ;
> @arr[2,2000,20_000,900_000] = (1,2,3,4) ;
> print "After initial.: ", (localtime)[0], "\n" ;
> splice @arr,10_000_000, 2, (5,6,7);
> print "After one splice:", (localtime)[0], "\n" ;
> -------------
>
> Result:
>
> bash-2.03# test_splice.pl
> Start: 7
> After building: 8
> After initial.: 8
> After one splice:15
>
> Replacing
> splice @arr,10_000_000, 2, (5,6,7);
> with
> splice @arr,10_000_000, 5, (5,6,7);
>
> Result:
>
> bash-2.03# test_splice.pl
> Start: 39
> After building: 40
> After initial.: 40
> After one splice:40
>
> In the second case, the splice is done immediately!
I don't get these results. For me, both are done immediately.
I assume your perl needs to reallocate your array on the increasing
splice, which means every pointer has to be copied into the new memory
area, and that your computer is too small to hold both copies in memory
simultaneously, so you forcing the memory to swap/page to disk.
Does a second extending splice immediately after the first also
take 7 seconds?
What happens if you add $#arr = 20_000_100 ; before $#arr = 20_000_000 ;?
>
> Whyyyy ?
>
Please don't whine.
We already covered why. Growing an array takes longer on average because
occasionally you have to find a bigger sandbox and then move all your toys
into it. When it does this, it find the new sandbox bigger than it
immediately needs, so there is some room for growth. You never have to do
that upon shrinking an array. Apparently your program is feeling the full
brunt of this movement during it's solitary extending splice, without
averaging it over the many future extending splices which wouldn't require
a reallocation.
Why do you care? Is it a common occurence that you need to start a
program, allocate a large array, splice something into the middle of it,
then end the program?
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service New Rate! $9.95/Month 50GB
------------------------------
Date: Wed, 27 Nov 2002 17:29:35 GMT
From: Bryan <bryan@akanta.com>
Subject: using strict and warnings
Message-Id: <3DE500FE.30901@akanta.com>
Hi,
My company recently contracted out a website, and we just got the code
in house. The code, by the way, does not work. In looking through the
code, I noticed that that 98% of it is static html, and the remaining 2%
appears to be old and crusty perl code. All 155 lines of it.
These guys used some cgi_lib.pl file, or rather hardly used it... it
looks like they use 2 methods that are included with this little lib.
As far as I know, cgi_lib.pl is extremely old and very outdated... most
people use CGI.pm now via CPAN if I am correct.
But what really troubles me is that the code they are using does not use
strict or warnings. If I add the strict pragma and turn warnings on, I
get a !*@#load of warnings of course and the scripts will not run.
I have always used warnings and strict in my cgi based code, but my
understanding of exactly why this is important is less than thorough.
For example, what could happen to a website that utilizes a perl script
that is not using strict? Is this just bad programming practice, or
are there real security risks here? Can someone give me some examples
of what could happen? Or point me to some docs? I looked through the
faq and perldocs and found some info about what strict does, but there
is no explanation of why that is so important... ( or I missed it ).
Thanks,
Bryan
------------------------------
Date: Wed, 27 Nov 2002 19:13:42 +0100
From: Dominik Seelow <kurzhalsflasche@netscape.net>
Subject: Re: using strict and warnings
Message-Id: <3DE50B56.5040506@netscape.net>
Bryan announced:
> Hi,
>
> My company recently contracted out a website, and we just got the code
> in house. The code, by the way, does not work. In looking through the
> code, I noticed that that 98% of it is static html, and the remaining 2%
> appears to be old and crusty perl code. All 155 lines of it.
>
> These guys used some cgi_lib.pl file, or rather hardly used it... it
> looks like they use 2 methods that are included with this little lib.
> As far as I know, cgi_lib.pl is extremely old and very outdated... most
> people use CGI.pm now via CPAN if I am correct.
>
> But what really troubles me is that the code they are using does not use
> strict or warnings. If I add the strict pragma and turn warnings on, I
> get a !*@#load of warnings of course and the scripts will not run.
>
> I have always used warnings and strict in my cgi based code, but my
> understanding of exactly why this is important is less than thorough.
> For example, what could happen to a website that utilizes a perl script
> that is not using strict? Is this just bad programming practice, or
> are there real security risks here? Can someone give me some examples
> of what could happen? Or point me to some docs? I looked through the
> faq and perldocs and found some info about what strict does, but there
> is no explanation of why that is so important... ( or I missed it ).
>
> Thanks,
> Bryan
>
Hello Bryan,
IMHO it is a very good idea always to use strict and warnings -
especially in a 'production system'. Although it is no security risk
/per se/ not to use strict, these modules will help you to avoid any
structures that might lead to problems. In any case, strict and warnings
will force you to write clean programs and help you to discover possible
errors before they occur.
By the way, if you read some threads in this newsgroups, you'll have
discovered that usually nobody shares "Perl Gurl"'s points of views (oh,
she will 'Frank' me for this...).
HTH,
Dominik
------------------------------
Date: Wed, 27 Nov 2002 18:46:45 +0000
From: "Dave Cross" <dave@dave.org.uk>
Subject: Re: using strict and warnings
Message-Id: <pan.2002.11.27.18.46.45.29932@dave.org.uk>
On Wed, 27 Nov 2002 17:29:35 +0000, Bryan wrote:
> Hi,
>
> My company recently contracted out a website, and we just got the code
> in house. The code, by the way, does not work. In looking through the
> code, I noticed that that 98% of it is static html, and the remaining 2%
> appears to be old and crusty perl code. All 155 lines of it.
>
> These guys used some cgi_lib.pl file, or rather hardly used it... it
> looks like they use 2 methods that are included with this little lib.
> As far as I know, cgi_lib.pl is extremely old and very outdated... most
> people use CGI.pm now via CPAN if I am correct.
>
> But what really troubles me is that the code they are using does not use
> strict or warnings. If I add the strict pragma and turn warnings on, I
> get a !*@#load of warnings of course and the scripts will not run.
>
> I have always used warnings and strict in my cgi based code, but my
> understanding of exactly why this is important is less than thorough.
> For example, what could happen to a website that utilizes a perl script
> that is not using strict? Is this just bad programming practice, or
> are there real security risks here? Can someone give me some examples
> of what could happen? Or point me to some docs? I looked through the
> faq and perldocs and found some info about what strict does, but there
> is no explanation of why that is so important... ( or I missed it ).
Not using "strict" and "warnings" won't make your code any less secure,
but it is possible that some little bugs might have crept in that would be
easier to find with "strict" and "warnings" turned on.
Not using those pragmas, however, should be a large red flag that these
people don't know as much Perl as they'd like you to believe that they do.
The vast majority of respected Perl experts recommend the use of "strict"
and "warnings" in all but the smallest of programs. The use of cgi-lib.pl
is more evidence that they are not very good (or, at least, very up to date)
Perl programmers.
Most of the errors you're seeing are probably because none of the variables
in the program have been declared - this is one of the things that "use
strict" checks for. If you want to know more about what "strict" and
"warnings" actually do try typing "perldoc strict" or "perldoc warnings"
at your command line.
What is more worrying from a security point of view would be if your CGI
programs didn't turn on taint mode on the shebang line. Anyone who writes
a CGI program that takes user input and doesn't use taint mode is just
asking for a huge security hole on thier server. You don't mention taint
mode in your posting so I assume that your contractors have done this.
--
Love is a fire of flaming brandy
Upon a crepe suzette
------------------------------
Date: Wed, 27 Nov 2002 12:44:41 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: using strict and warnings
Message-Id: <slrnaua4kp.3c7.tadmc@magna.augustmail.com>
Bryan <bryan@akanta.com> wrote:
>
> My company recently contracted out a website, and we just got the code
> in house. The code, by the way, does not work. In looking through the
> code, I noticed that that 98% of it is static html, and the remaining 2%
> appears to be old and crusty perl code. All 155 lines of it.
>
> These guys used some cgi_lib.pl file, or rather hardly used it... it
> looks like they use 2 methods that are included with this little lib.
Put them in your "never do business with them again" file and
proceed with cleaning up the mess.
There are a lot of shyster "web developers" out there. Looks
like you have found one of them...
> As far as I know, cgi_lib.pl is extremely old and very outdated... most
^
I hope you meant "cgi-lib.pl" there.
> people use CGI.pm now via CPAN if I am correct.
You are correct.
See also the "COMPATIBILITY WITH CGI-LIB.PL" section in:
perldoc CGI
> But what really troubles me is that the code they are using does not use
> strict or warnings.
The fact that it troubles you is a very good sign. :-)
> If I add the strict pragma and turn warnings on, I
> get a !*@#load of warnings of course and the scripts will not run.
Try just warnings first.
use warnings; # the new(ish) lexical warnings
When it makes warnings, see if you can fix each straightaway.
If not, then "mark" them with:
{ no warnings "uninitialized"; # or whatever
# code that makes warnings
}
and come back to them later. Ask here if you don't see
why the warning is being generated or how you can
accomplish the same thing in a warnings-free manner.
After it makes no warnings, then try adding "use strict".
You don't need to run the program to check for compliance
with strictures, just compiling will do as a course
first-level check:
perl -c sillyprog
You will mostly just need to put "my " infront of the first
use of every variable in the program.
You may also benefit from:
"Coping with Scoping":
http://perl.plover.com/FAQs/Namespaces.html
If you run afoul of "use strict refs", then curse the shysters
some more, roll up your sleeves, and get the symbolic
references out of there.
> I have always used warnings and strict in my cgi based code, but my
> understanding of exactly why this is important is less than thorough.
I have always eaten my vegetables, but my understanding of exactly
why this is important is less than thorough.
But I trust Mom.
:-)
> For example, what could happen to a website that utilizes a perl script
> that is not using strict?
You could spend hours debugging a silly typo that a machine
would have pointed out in milliseconds.
Leaving strict off costs your employer money. Some might even
count that (spending more debugging time than is absolutely required)
as being unethical.
$bamm_bamm = 'Flintstone';
... several hundred lines of code and a few days later
in development:
print "Bamm Bamm is a $bammbamm\n";
ie. you forgot that you spelled the variable name with an underscore.
without strictures, you scratch your head wondering about the
incorrect output:
Bamm Bamm is a
and launch into a debugging session.
with strictures, perl tells you immediately that you've made
a mistake there.
> Is this just bad programming practice, or
Bad programming practice costs money.
Wasting your employer's money unnecessarily may have a negative
effect on your future employment prospects. :-)
> are there real security risks here?
Yes.
You mention security now, but there is no mention of taint checking.
Did the shysters enable taint checking?
Seems unlikely given their evident lack of clue.
> Can someone give me some examples
> of what could happen?
The above is an example of "use strict vars" saving time (money).
> Or point me to some docs?
For examples of where "use strict refs" violations can
cost you, see:
http://www.plover.com/~mjd/perl/varvarname.html
http://www.plover.com/~mjd/perl/varvarname2.html
http://www.plover.com/~mjd/perl/varvarname3.html
> I looked through the
> faq and perldocs and found some info about what strict does, but there
> is no explanation of why that is so important... ( or I missed it ).
Saves debugging time. Avoids common mistakes.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 27 Nov 2002 12:56:34 -0600
From: cp <cpryce@pryce.net>
Subject: Re: using strict and warnings
Message-Id: <BA0A7182.12828%cpryce@pryce.net>
in article 3DE500FE.30901@akanta.com, Bryan at bryan@akanta.com wrote on
11/27/02 11:29 AM:
> My company recently contracted out a website, and we just got the code
> in house. The code, by the way, does not work. In looking through the
> code, I noticed that that 98% of it is static html, and the remaining 2%
> appears to be old and crusty perl code. All 155 lines of it.
>
> These guys used some cgi_lib.pl file, or rather hardly used it... it
> looks like they use 2 methods that are included with this little lib.
> As far as I know, cgi_lib.pl is extremely old and very outdated... most
> people use CGI.pm now via CPAN if I am correct.
Yes. CGI.pm is a superior replacement to the old cgi_lib.pl. There is a
suggested migration path in the CGI docs. You might want to see:
http://stein.cshl.org/WWW/software/CGI/#migrating
>
> But what really troubles me is that the code they are using does not use
> strict or warnings. If I add the strict pragma and turn warnings on, I
> get a !*@#load of warnings of course and the scripts will not run.
>
> I have always used warnings and strict in my cgi based code, but my
> understanding of exactly why this is important is less than thorough.
Good practice. Warnings turn on helpful debugging messages. You can catch a
myriad of potential problems simply by running a cgi from the command line
with warnings enabled. When you invoke a CGI script from a Web browser,
warnings print to STDERR, which is the Web server error log in most cases,
so some practitioners suggest turning them off once the script is debugged
and "production ready." YMMV.
Strictures force many safe programming practices, including the practice of
declaring variables before they can be used. This will catch typos
($variable when you meant $variables), incorrectly scoped variables, or the
wrong prefix (i.e., you asked for keys $data when you meant keys %{$data}).
Use of strictures and warnings will catch dozens of errors for you, that is
why their use is highly recommended.
> For example, what could happen to a website that utilizes a perl script
> that is not using strict? Is this just bad programming practice, or
> are there real security risks here? Can someone give me some examples
> of what could happen? Or point me to some docs? I looked through the
> faq and perldocs and found some info about what strict does, but there
> is no explanation of why that is so important... ( or I missed it ).
Taint checking is the security mechanism for a CGI script that you are
looking for, I believe. You might want to review perldoc perlsec. Or have a
look at the documentation online at:
http://www.perldoc.com/perl5.8.0/pod/perlsec.html
cp
------------------------------
Date: 27 Nov 2002 09:44:51 -0800
From: mpilk25@hotmail.com (Mark)
Subject: Windows multiple process questions with either Win32:Process or "Start"
Message-Id: <613963ff.0211270944.570f3d60@posting.google.com>
I have a perl script that will download one file at a time from my ftp
program. I want to be able to download multiple files at once and
when all of the files have completed downloading, then run the rest of
my perl script with the rest of my utilities. I have used "start
myprogram" to download mulitple files at once, but I do not know how
to make Perl wait on the programs to finish downloading so I can then
run other utilities from my one perl script.
I tried:
until (-e myfile){
but it did not work either.
I have looked at Win32::Process, but I am not sure that it will make
the rest of my script wait until my files are downloaded. Thanks for
any help anyone could give.
------------------------------
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.
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 4190
***************************************