[18327] in Perl-Users-Digest
Perl-Users Digest, Issue: 495 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 15 18:06:11 2001
Date: Thu, 15 Mar 2001 15:05:24 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <984697523-v10-i495@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Thu, 15 Mar 2001 Volume: 10 Number: 495
Today's topics:
Re: "uninitiatlized value" errors? <peter.sundstrom-eds@eds.com>
@a->[8] vs. $a[8] <trondmm-usenet@crusaders.no>
Re: @a->[8] vs. $a[8] (Randal L. Schwartz)
Re: @a->[8] vs. $a[8] <krahnj@acm.org>
Re: @a->[8] vs. $a[8] <bart.lateur@skynet.be>
Re: [Q] Extracting a 'carriage return' from a string <mjcarman@home.com>
Re: [Q] Extracting a 'carriage return' from a string <mjcarman@home.com>
Re: [Q] Extracting a 'carriage return' from a string <bart.lateur@skynet.be>
Re: [Q] Generating a random number nobull@mail.com
`pwd` doesn't work on all servers? <jazrant@zsc.nrcan.zc.ca>
Re: `pwd` doesn't work on all servers? <krahnj@acm.org>
Re: `pwd` doesn't work on all servers? <mjcarman@home.com>
Re: `pwd` doesn't work on all servers? <jazrant@zsc.nrcan.zc.ca>
Re: binary files <ciaran.mccreesh@useaddressbelow.please>
Re: binary files <flavell@mail.cern.ch>
Re: binary files (Gwyn Judd)
Re: binary files <todd@designsouth.net>
Re: binary files <todd@designsouth.net>
Re: binary files <bart.lateur@skynet.be>
Re: binary files <mjcarman@home.com>
Re: Does Perl have an un-getc <mischief@velma.motion.net>
Re: Duplicate entry problem in mysql using DBI (Garry Williams)
Re: Duplicate entry problem in mysql using DBI (Garry Williams)
Re: Exp. Perl/CGI programmer needed <webmaster@webdragon.munge.net>
Re: Exp. Perl/CGI programmer needed (David H. Adler)
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 16 Mar 2001 10:58:12 +1300
From: "Peter Sundstrom" <peter.sundstrom-eds@eds.com>
Subject: Re: "uninitiatlized value" errors?
Message-Id: <98rdu0$mlt$1@hermes.nz.eds.com>
"John A. Grant" <jazrant@zsc.nrcan.zc.ca> wrote in message
news:98qqkn$5ps19@nrn2.NRCan.gc.ca...
> > Why not?
> [...determined to convince me that csh is bad...]
>
> I'm not programming the shell, nor do I live & breathe the
> shell. I really don't care what shell I use to launch Perl while
> developing - the Perl program is only for CGI and the shell
> is simply a way to launch it while testing/compiling/developing.
> Well, for me anyway. I figured csh/bourne comments were
> more appropriate for a shell.advocacy group rather than
> here in a Perl group. It's more likely that being a non-guru,
> I don't appreciate the Perl-shell symbiosis.
>
> I was surprised however, that I could not redirect the warning
> messages using csh, whether I tried stderr or stdout:
> $ perl -w xxx.cgi > err
> $ perl -w xxx.cgi 1>err
> $ perl -w xxx.cgi 2>err
>
> None of these 3 work on csh. The latter (2) works fine on NT.
>
> From that I would surmise that Perl is a tool was designed to work
> only with Bourne (Korn?) and not with csh. I don't believe that.
> A more likely possibility is that I just don't know how to use csh
> well enough to redirect the warnings. Switching shells just so I
> could have a hard copy of the warning messages seems a little
> extreme. Then again, what do I know - I'm not a Unix shell guru
> nor do I want to be - I just have too many other things to do.
> However, I should be free to choose my preferred shell (wrongly
> or rightly) and use my preferred tool(s). For the record, I didn't
> actually "choose" csh - it's the default shell for the system to
> which I have access.
Read up on http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
It will give you the low down on stderr and stdout with csh. Once you've
read that,
get you admin to change your shell, or add:
exec /bin/ksh
to your profile.
------------------------------
Date: Thu, 15 Mar 2001 21:50:26 GMT
From: "Trond Michelsen" <trondmm-usenet@crusaders.no>
Subject: @a->[8] vs. $a[8]
Message-Id: <COas6.7715$8c.886082@news01.chello.no>
Hi.
I was recently reviewing some code for a friend of mine, and I noticed that
he constantly used @a->[8] as a notation to access individual
array-elements. At first I was surprised that the programs worked at all,
but after some testing it seems to me that @a->[8] behaves exactly like
$a[8].
Anyway - I can't find this notation documented, and I'm just a little bit
curious why (and how) it works.
a couple of examples:
$ perl -le '$, = " - "; @a = ("000".."999");' \
-e 'print @a->[4], $a[4];' \
-e 'print @a->[6,7,8], $a[6,7,8];'
004 - 004
008 - 008
$ perl -le 'sub want {print wantarray ? "ARRAY" : "SCALAR"}' \
-e '@a->[4] = want()'
SCALAR
--
Trond Michelsen
------------------------------
Date: 15 Mar 2001 14:11:04 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: @a->[8] vs. $a[8]
Message-Id: <m166ha8yyv.fsf@halfdome.holdit.com>
>>>>> "Trond" == Trond Michelsen <trondmm-usenet@crusaders.no> writes:
Trond> I was recently reviewing some code for a friend of mine, and I
Trond> noticed that he constantly used @a->[8] as a notation to access
Trond> individual array-elements.
That's illegal syntax that's currently unable to be distinguished by
the Perl compiler from legal syntax, thanks to deep mojo in the
compiler.
Use at your own peril.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: Thu, 15 Mar 2001 22:28:30 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: @a->[8] vs. $a[8]
Message-Id: <3AB1439A.C610E534@acm.org>
Trond Michelsen wrote:
>
> Hi.
>
> I was recently reviewing some code for a friend of mine, and I noticed that
> he constantly used @a->[8] as a notation to access individual
> array-elements. At first I was surprised that the programs worked at all,
> but after some testing it seems to me that @a->[8] behaves exactly like
> $a[8].
> Anyway - I can't find this notation documented, and I'm just a little bit
> curious why (and how) it works.
perldoc perlfaq4
What is the difference between $array[1] and @array[1]?
The former is a scalar value, the latter an array slice,
which makes it a list with one (scalar) value. You should
use $ when you want a scalar value (most of the time) and
@ when you want a list with one scalar value in it (very,
very rarely; nearly never, in fact).
perldoc perldata
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 15 Mar 2001 22:40:38 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: @a->[8] vs. $a[8]
Message-Id: <36h2btsug3sir84cq3jjdept3157co6m00@4ax.com>
Randal L. Schwartz wrote:
>Trond> I was recently reviewing some code for a friend of mine, and I
>Trond> noticed that he constantly used @a->[8] as a notation to access
>Trond> individual array-elements.
>
>That's illegal syntax that's currently unable to be distinguished by
>the Perl compiler from legal syntax, thanks to deep mojo in the
>compiler.
I tried O=Deparse on a test script, and to my surprise, really, it came
out the same way it went in.
Not only is it illegal syntax that hapens to work, but B::Deparse is
capable of handling it properly, too.
--
Bart.
------------------------------
Date: Thu, 15 Mar 2001 14:33:10 -0600
From: Michael Carman <mjcarman@home.com>
Subject: Re: [Q] Extracting a 'carriage return' from a string
Message-Id: <3AB12706.A0973EA9@home.com>
Tom Scheper wrote:
> [...]
> Use multiline regexps or use chomp instead. Chop will just take out
> the last character, beit a carriage return or linefeed or any
> character. Chomp will take out any eol characters but nothing else and
> should do what you want it to do.
You're only half right. chop() removes and returns the last character
from a string. chomp() is the "safe" form of chop(), in that it will
only remove the last character if it matches $/ ("\n" by default).
chomp() will *not* remove "any eol characters."
> Or you could $x=~s/[\n\r]/" "/gs;
>
> You need an s for multiline,
No he doesn't. All /s does is change '.' from "match any char except
newline" to "match any char including newline." He's not trying to match
anything across lines here, so it's completely useless.
> and you don't need the e as you're not executing any code.
Actually, yes he is. He's executing {" "} which evaluates to a single
space. If you just get rid of the /e, "\n" would be replaced with
'" "' (double-quote space double-quote). The OP should drop both.
-mjc
------------------------------
Date: Thu, 15 Mar 2001 14:36:38 -0600
From: Michael Carman <mjcarman@home.com>
Subject: Re: [Q] Extracting a 'carriage return' from a string
Message-Id: <3AB127D6.4B96D6D9@home.com>
giuseppe wrote:
>
> I'm updating a flat-file database from a web based form.
> While parsing my data, certain strings can contain many
> carriage returns in them. I need to get rid of these, but chop
> won't get all of them.
> I tried: $x = ~ s/\n/" "/eg;
> &
> $x =~ s/x0a/" "/eg; #I think this is the correct hex
> value (UNIX)
>
> What am I doing wrong?
Forgetting about the CR that Win/DOS platforms use as part of the EOL
sequence. Try this:
$x =~ s/\015?\012/ /g;
It should strip Unix and DOS newlines. Macs (and any other platforms)
may still be a problem.
Are you using CGI.pm? (You should be.) I would think that it would
handle this for you. I don't do CGI programming though, so I could be
wrong. Maybe someone else knows.
-mjc
------------------------------
Date: Thu, 15 Mar 2001 22:47:16 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: [Q] Extracting a 'carriage return' from a string
Message-Id: <6ch2bt8ft2r54jvabje9m90hkvhm87inoj@4ax.com>
giuseppe wrote:
>I'm updating a flat-file database from a web based form.
>While parsing my data, certain strings can contain many
>carriage returns in them. I need to get rid of these, but chop
>won't get all of them.
>I tried: $x = ~ s/\n/" "/eg;
> &
> $x =~ s/x0a/" "/eg; #I think this is the correct hex
>value (UNIX)
>
>What am I doing wrong?
The fact you're using the wrong Ascii code to get rid of them? CR is
"\x0D", not "\x0A".
You should remember that for form submission of multiline data in CGI,
the result contains both chr(0x0D) and chr(0x0A). At least, get rid of
the ones you don't need:
tr/\r//d;
But likely, you want to unwrap the text into one line. So simply convert
all whitespace to a single space -- CR and LF both are whitespace too.
tr/ \t\n\r/ /s;
or
s/\s+/ /g;
p.s. that's a weird thought, putting quotes around your replacement
string and then add the /e modifier.
--
Bart.
------------------------------
Date: 15 Mar 2001 18:51:05 +0000
From: nobull@mail.com
Subject: Re: [Q] Generating a random number
Message-Id: <u93dceg92e.fsf@wcl-l.bham.ac.uk>
"giuseppe" <joedrum414@hotmail.com> writes:
> Subject: [Q] Generating a random number
Your question has nothing whatever to do with random numbers.
Please use a subject line that relates to your question.
> Each time the form is submitted, I need a "unique" number assigned to that
> transaction.
For a single server counter (stored in an interlocked file) will do
this. There's a module to implement a counter file on CPAN[1] or if
you prefer you can just cut and paste the sample code from the FAQ.
Most web servers also provide (or can be persuaded to provide) a
transaction ID somewhere in %ENV which will be unique even if you are
load-ballencing across a server-farm. Details not Perl-related.
[1] Guessing the right keywords to use in your search is left as an
exercise for the reader. (Hint: 'Random' is _not_ one of them).
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Thu, 15 Mar 2001 16:29:43 -0500
From: "John A. Grant" <jazrant@zsc.nrcan.zc.ca>
Subject: `pwd` doesn't work on all servers?
Message-Id: <98rcfn$5ps21@nrn2.NRCan.gc.ca>
I have this test program:
#!/usr/bin/perl
$|=1;
my $cwd=`pwd`;
print "Content type: text/html\n\n";
print "<html><body>cwd=$cwd</body></html>";
There are 4 cases:
1. server #1, from command-line - works fine
2. server #1, run by URL - works fine
3. server #2, from command-line - works fine
4. server #2, from command-line - does not work
"works fine" means it prints a valid cwd. "does not work"
means "$cwd" is 'blank' - nothing prints.
I've asked the server #2 admin if this fails because of some
security setting on server #2, but I don't have an answer yet.
Is there a way to look up the cwd internally without capturing
the output of `pwd` from the system?
--
John A. Grant * I speak only for myself * (remove 'z' to reply)
Radiation Geophysics, Geological Survey of Canada, Ottawa
If you followup, please do NOT e-mail me a copy: I will read it here
------------------------------
Date: Thu, 15 Mar 2001 22:17:07 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: `pwd` doesn't work on all servers?
Message-Id: <3AB140EF.9C5A8D3E@acm.org>
"John A. Grant" wrote:
>
> Is there a way to look up the cwd internally without capturing
> the output of `pwd` from the system?
perldoc Cwd
SYNOPSIS
use Cwd;
$dir = cwd;
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 15 Mar 2001 16:29:09 -0600
From: Michael Carman <mjcarman@home.com>
Subject: Re: `pwd` doesn't work on all servers?
Message-Id: <3AB14235.F1AB9F49@home.com>
"John A. Grant" wrote:
>
> Is there a way to look up the cwd internally without capturing
> the output of `pwd` from the system?
use Cwd;
-mjc
------------------------------
Date: Thu, 15 Mar 2001 17:54:36 -0500
From: "John A. Grant" <jazrant@zsc.nrcan.zc.ca>
Subject: Re: `pwd` doesn't work on all servers?
Message-Id: <98rhes$5tm27@nrn2.NRCan.gc.ca>
"John W. Krahn" <krahnj@acm.org> wrote in message
news:3AB140EF.9C5A8D3E@acm.org...
> "John A. Grant" wrote:
> >
> > Is there a way to look up the cwd internally without capturing
> > the output of `pwd` from the system?
>
> perldoc Cwd
>
> SYNOPSIS
> use Cwd;
> $dir = cwd;
Thanks. Sometimes I'm blind.
--
John A. Grant * I speak only for myself * (remove 'z' to reply)
Radiation Geophysics, Geological Survey of Canada, Ottawa
If you followup, please do NOT e-mail me a copy: I will read it here
------------------------------
Date: Thu, 15 Mar 2001 18:37:39 +0000
From: Ciaran McCreesh <ciaran.mccreesh@useaddressbelow.please>
Subject: Re: binary files
Message-Id: <VTgfJNAzvQs6IwKq@harlawroad.freeserve.co.uk>
In article <ZR7s6.6979$Q47.1869553@news1.rdc1.tn.home.com> (whatever
that means), Todd Smith <todd@designsouth.net> writes
>Is there a way to convert any file (picture, mp3, text, or anything) to a
>binary file that I can read (with a bunch of 1's and 0's) and convert it
>back to the original file from the binary? A Perl module maybe?
A binary file is just one which contains the full character set (as in
0..255)... You might want to look at MIME64 if you need to transmit a
binary file as text...
Ciaran
--
Ciaran McCreesh
mail: keesh@users.sourceforge.net
web: http://www.opensourcepan.com/
------------------------------
Date: Thu, 15 Mar 2001 20:34:30 +0100
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: binary files
Message-Id: <Pine.LNX.4.30.0103152026180.17630-100000@lxplus003.cern.ch>
On Thu, 15 Mar 2001, Ciaran McCreesh wrote:
> A binary file is just one which contains the full character set
I can't agree. If we know that those octets (bytes) represent
characters in some character set, then what we have would seem to be a
text file, rather than a binary file.
The point of a binary file is that the octets could take any value
(which I suspect is the idea which you were aiming to express), and
those values could represent pretty much anything - some of them may
even represent text characters, but often not.
> You might want to look at MIME64 if you need to transmit a
> binary file as text...
'transmit' by what means? Some transmission methods are binary-clean
(e.g HTTP), whereas others aren't (e.g mail). Some support both
kinds of transmission (e.g FTP's 'image' versus so-called 'ascii'
mode).
Use of MIME-coding would be suboptimal if it wasn't necessary.
Fact is, I don't really understand the original question, since the
hon. Usenaut seems to be trying to solve a non-problem - or has
omitted to include some vital part of their problem situation. I'm
reasonably confident that your answer isn't it, though. It'll be
interesting to see if someone's PSI::ESP module manages to tease out
what the real question was meant to be.
ttfn
------------------------------
Date: Thu, 15 Mar 2001 20:32:26 GMT
From: tjla@guvfybir.qlaqaf.bet (Gwyn Judd)
Subject: Re: binary files
Message-Id: <slrn9b29mo.aa7.tjla@thislove.dyndns.org>
I was shocked! How could Todd Smith <todd@designsouth.net>
say such a terrible thing:
>Is there a way to convert any file (picture, mp3, text, or anything) to a
>binary file that I can read (with a bunch of 1's and 0's) and convert it
>back to the original file from the binary? A Perl module maybe?
I don't really understand what it is you are asking. All files are able
to be accessed as a stream of 1's and 0's. Can you explain this a little
more clearly?
--
Gwyn Judd (print `echo 'tjla@guvfybir.qlaqaf.bet' | rot13`)
A ski jump is a leap made by a person on the way to the hospital.
------------------------------
Date: Thu, 15 Mar 2001 21:25:13 GMT
From: "Todd Smith" <todd@designsouth.net>
Subject: Re: binary files
Message-Id: <Zqas6.7139$Q47.1933917@news1.rdc1.tn.home.com>
I just want to be able to translate a file into a file containing 1's and
0's (Binary). Then I'll take that file in perl and play with it later.
uuencode seems to do a good job but how do i get only the 1's and 0's of
real binary data?
"Gwyn Judd" <tjla@guvfybir.qlaqaf.bet> wrote in message
news:slrn9b29mo.aa7.tjla@thislove.dyndns.org...
> I was shocked! How could Todd Smith <todd@designsouth.net>
> say such a terrible thing:
> >Is there a way to convert any file (picture, mp3, text, or anything) to a
> >binary file that I can read (with a bunch of 1's and 0's) and convert it
> >back to the original file from the binary? A Perl module maybe?
>
> I don't really understand what it is you are asking. All files are able
> to be accessed as a stream of 1's and 0's. Can you explain this a little
> more clearly?
>
> --
> Gwyn Judd (print `echo 'tjla@guvfybir.qlaqaf.bet' | rot13`)
> A ski jump is a leap made by a person on the way to the hospital.
------------------------------
Date: Thu, 15 Mar 2001 21:46:37 GMT
From: "Todd Smith" <todd@designsouth.net>
Subject: Re: binary files
Message-Id: <1Las6.7143$Q47.1942101@news1.rdc1.tn.home.com>
it looks like I can go from an executable to ascii with uuencode, the ascii
to 0's and 1's with perl's unpack..
Thanks Anyway!
"Todd Smith" <todd@designsouth.net> wrote in message
news:Zqas6.7139$Q47.1933917@news1.rdc1.tn.home.com...
> I just want to be able to translate a file into a file containing 1's and
> 0's (Binary). Then I'll take that file in perl and play with it later.
> uuencode seems to do a good job but how do i get only the 1's and 0's of
> real binary data?
>
>
>
> "Gwyn Judd" <tjla@guvfybir.qlaqaf.bet> wrote in message
> news:slrn9b29mo.aa7.tjla@thislove.dyndns.org...
> > I was shocked! How could Todd Smith <todd@designsouth.net>
> > say such a terrible thing:
> > >Is there a way to convert any file (picture, mp3, text, or anything) to
a
> > >binary file that I can read (with a bunch of 1's and 0's) and convert
it
> > >back to the original file from the binary? A Perl module maybe?
> >
> > I don't really understand what it is you are asking. All files are able
> > to be accessed as a stream of 1's and 0's. Can you explain this a little
> > more clearly?
> >
> > --
> > Gwyn Judd (print `echo 'tjla@guvfybir.qlaqaf.bet' | rot13`)
> > A ski jump is a leap made by a person on the way to the hospital.
>
>
------------------------------
Date: Thu, 15 Mar 2001 22:51:53 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: binary files
Message-Id: <unh2btocs1qaqt3dp76otf6rskddk54i1h@4ax.com>
Todd Smith wrote:
>Is there a way to convert any file (picture, mp3, text, or anything) to a
>binary file that I can read (with a bunch of 1's and 0's) and convert it
>back to the original file from the binary? A Perl module maybe?
So you want to turn "AB" into "0100000101000010"?
Sometimes, necessary Perl code can be amazingly small, but not
necessarily understandable.
print unpack 'B*', 'AB';
-->
0100000101000010
--
Bart.
------------------------------
Date: Thu, 15 Mar 2001 16:47:22 -0600
From: Michael Carman <mjcarman@home.com>
Subject: Re: binary files
Message-Id: <3AB1467A.6F9F8685@home.com>
Todd Smith wrote:
>
> Is there a way to convert any file (picture, mp3, text, or anything)
> to a binary file that I can read (with a bunch of 1's and 0's) and
> convert it back to the original file from the binary? A Perl module
> maybe?
All data contained within a computer is binary. (Well, until the quantum
PCs start coming out, at least ;) You should of course always use
binmode() when processing a file that contains anything other than pure
text, but that doesn't "convert" the file, it just affects the I/O
processing.
PSI::ESP is overloading. The closest thing to sense I can make out of
your question (if it can be called sense) is that you want to convert
the file to a textual representation of binary. I.e. you open it up in a
text editor and it looks something like this:
10001011010110010001011000100101010110010101001011110110101
You could do it, but I don't know what the point would be, and it would
balloon your file -- every bit would turn into a byte!
-mjc
------------------------------
Date: Thu, 15 Mar 2001 22:28:41 -0000
From: Chris Stith <mischief@velma.motion.net>
Subject: Re: Does Perl have an un-getc
Message-Id: <tb2ggpfjn0729d@corp.supernews.com>
Tim Tracy <ttracy@houston.rr.com> wrote:
> This is a newbie question.
> I am reading from a text file one byte at a time useing getc().
> I want to be able to look ahead in the file a few characters and still keep
> my place in the file.
> For example,
> if the text file had '123abc'
> I would like to be able to read in the "1", then read in the "2" then the
> "3". Under certain circumstances I would like to be able to put the "2" and
> the "3" back in the file buffer and reposition the file pointer so it looks
> like I just read in the "1" again.
> Is there a way to do this in Perl?
> I have looked in perldoc -f (getc, read, syread)
Using your own buffers, sysread, and sysseek, it is possible.
Consider using Parse::RecDescent, Text::Balanced, Text::DelimMatch,
Parse::Lex, or Parse::Yapp as well. Parse::Lex looks like a winner
for your task.
Chris
--
Christopher E. Stith
Get real! This is a discussion group, not a helpdesk. You post
something, we discuss its implications. If the discussion happens to
answer a question you've asked, that's incidental. -- nobull, clp.misc
------------------------------
Date: Thu, 15 Mar 2001 21:36:04 GMT
From: garry@zvolve.com (Garry Williams)
Subject: Re: Duplicate entry problem in mysql using DBI
Message-Id: <8Bas6.93$tI3.4238@eagle.america.net>
On Wed, 14 Mar 2001 23:02:56 +0000, Meyrick
<meyrick@NOSPAMhedge-analytics.com> wrote:
>My perl script generates the following error on accessing mysql:
>
>DBD::mysql::db do failed: Duplicate entry '0000-00-00' for key 1 at
>./minky.pl line 120
This means that you are attempting to insert a row in a table that has
a value in a column that is the same as the value in already existing
row and that the column has been defined as a unique value constraint
to mysql.
This may be an error in your program, an error in your data or an
error in the definition of the table you are attempting an insert on.
>Now, I know from using shell scripts to access this database that mysql
>likes to have date format wrapped in single quotes - so that is what I
>have done in perl - but it complains!
^^^^^^^^^^^^
Do you mean the error message you have quoted above?
>Here is the code fragment from my script:
>
> #first create a nice date format
> $my_date=`date -d yesterday -I`;
> chomp $my_date;
>
> # then add the single quotes
> $db_date="'".$my_date."'";
>
> # do the same for another variable added to the database
> $db_expiry="'".$months[$i+1]."'";
>
> # create the database object
> $dbh=DBI->connect("DBI:mysql:database=test;host=localhost",
> "root",undef,{RaiseError=>1});
> $dbh->do("INSERT INTO ED1 VALUES(date,2output,2op_strike,
> 1output,1op_strike,atm,atm_strike,1outcall,
> 1oc_strike,2outcall,2oc_strike,expiry,underlying)",
> ($db_date,$out_put2,$dn_strike2,$out_put1,
> $dn_strike2,$atm_vol,$atm_strike,$out_call1,$up_strike1,
> $out_call2,$up_strike2,$db_expiry,$close)
> ) || die $dbh->errstr;
>
> $dbh->disconnect();
The error above could have occurred because this fragment executed
twice or it may be because there's already a row in the table with
that value. It's impossible to tell from the data you have presented.
A possible work-around is to wait a day and try again. :-) Maybe the
real problem is the table definition.
You should either prepare an insert statement with place holders (`?')
and then pass your variables to the execute method on the prepared
statement or you should use the $dbh->quote() method to properly quote
data. The quote() method will always get it right; you may not.
(Although, in this case, that's not an issue.) In general, the
prepare/execute is the way to go, when inserting more than one row.
You should review the DBI manual page. It goes into lots of detail
about proper quoting and it discusses a number of ways to avoid the
problem of quoting altogether by binding variables to SQL statements.
--
Garry Williams
------------------------------
Date: Thu, 15 Mar 2001 21:46:17 GMT
From: garry@zvolve.com (Garry Williams)
Subject: Re: Duplicate entry problem in mysql using DBI
Message-Id: <JKas6.97$tI3.4238@eagle.america.net>
On Thu, 15 Mar 2001 10:26:52 +0000, Paul Boardman <peb@bms.umist.ac.uk> wrote:
>Meyrick wrote:
>> My perl script generates the following error on accessing mysql:
>>
>> DBD::mysql::db do failed: Duplicate entry '0000-00-00' for key 1 at
>> ./minky.pl line 120
>> Database handle destroyed without explicit disconnect.
>>
>> Now, I know from using shell scripts to access this database that mysql
>> likes to have date format wrapped in single quotes - so that is what I
>> have done in perl - but it complains!
>
>> Here is the code fragment from my script:
><snip>
>
>I don't think the mysql server is griping about the single quotes.
It isn't. It's gripping about a duplicate key.
>Do you have the date field set as a primary key? The above error looks
>like it's generated by an attempt to insert either the same data over
>the top of itself (i.e. you've run the script twice without going into
>the database and deleting the previous attempt).
>
>> $dbh->do("INSERT INTO ED1 VALUES(date,2output,2op_strike,
> ^^^^^^
>don't you need this to come later? You are referring to columns not
>values at this point.
That's not true.
Although this is a bad technique because changes to table definitions
can break this program, it is perfectly valid to insert values into a
table without explicitly naming the columns as long as all of the
columns are given values *and* the values are specified in the correct
order.
But this is really an SQL language or mysql issue -- not a Perl issue.
--
Garry Williams
------------------------------
Date: 15 Mar 2001 20:25:38 GMT
From: "Scott R. Godin" <webmaster@webdragon.munge.net>
Subject: Re: Exp. Perl/CGI programmer needed
Message-Id: <98r8g2$ho0$0@216.155.32.198>
In article <slrn9au0t9.ov7.dha@panix6.panix.com>, dha@panix6.panix.com
(David H. Adler) wrote:
| There is a Perl Jobs Announce list that may be more helpful to you. See
| <http://www.pm.org/mailing_lists.shtml> for details.
David, you may want to adjust your responder text a bit, as that link
reports the following:
perl-jobs-announce
This moderated list for Perl job announcements has moved.
Please refer to http://jobs.perl.org/
perl-jobs-wanted
This moderated list for persons seeking Perl jobs has moved.
Please refer to http://jobs.perl.org/
Regards, :)
--
unmunge e-mail here:
#!perl -w
print map {chr(ord($_)-3)} split //, "zhepdvwhuCzhegudjrq1qhw";
# ( damn spammers. *shakes fist* take a hint. =:P )
------------------------------
Date: 15 Mar 2001 22:19:50 GMT
From: dha@panix6.panix.com (David H. Adler)
Subject: Re: Exp. Perl/CGI programmer needed
Message-Id: <slrn9b2g06.6lh.dha@panix6.panix.com>
On 15 Mar 2001 20:25:38 GMT, Scott R. Godin
<webmaster@webdragon.munge.net> wrote:
>In article <slrn9au0t9.ov7.dha@panix6.panix.com>, dha@panix6.panix.com
>(David H. Adler) wrote:
>
> | There is a Perl Jobs Announce list that may be more helpful to you. See
> | <http://www.pm.org/mailing_lists.shtml> for details.
>
>David, you may want to adjust your responder text a bit, as that link
>reports the following:
Just done. Thanks! Strangely, you were not the first to notice... :-)
dha
--
David H. Adler - <dha@panix.com> - http://www.panix.com/~dha/
Despite his tendency to become sidetracked in debates with invisible
giraffes, he was an exemplary player, and rarely licked anyone without
prior permission - Diablo on Shazam Twix
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 495
**************************************