[19322] in Perl-Users-Digest
Perl-Users Digest, Issue: 1517 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Aug 14 06:10:31 2001
Date: Tue, 14 Aug 2001 03:10:12 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <997783812-v10-i1517@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Tue, 14 Aug 2001 Volume: 10 Number: 1517
Today's topics:
Re: SQL error with s/// <goldbb2@earthlink.net>
SQLServer HowTo <paul@net366.com>
Re: SQLServer HowTo <sky@mail.lviv.ua>
Re: SQLServer HowTo <ilya@martynov.org>
Stateful CGI <sh@planetquake.com>
Re: string extraction (Anno Siegel)
Re: string extraction <krahnj@acm.org>
Re: string extraction (Anno Siegel)
Re: System Call Question <goldbb2@earthlink.net>
Re: TK:Date and Access database problem <goldbb2@earthlink.net>
Re: VIER/NEUN problem <cberry@cinenet.net>
Re: VIER/NEUN problem (John J. Trammell)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 14 Aug 2001 03:03:54 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: SQL error with s///
Message-Id: <3B78CD5A.B7D8E98@earthlink.net>
Daniel Gibby wrote:
>
> I am trying to use the s/// modifier to replace any single quotes or
> double quotes with a backslash+quote so that I can insert the quote
> into my SQL database.
>
> This is how I am doing it:
>
> $single_quote = "\\'";
> $form{$each_var} =~ s/\'\"/$single_quote/g;
>
> For some reason it doesn't seem to be working correctly. Am I missing
> something?
As John J. Trammell said, use the quote method. Or better yet, use
question marks as placeholders and either use bind_param or pass the
things to execute. This will completely avoid the need for quoting.
my $sth = $dbi->prepare(
q[ SELECT * FROM table WHERE ] .
join(" AND ", map {"$_ = ?"} keys %form)
);
$sth->execute( values %form );
Or
my @colnames = ... ;
$dbi->do(
q[ INSERT INTO table VALUES = (] .
join(", ", ("?" x @colnames)) . q[)],
undef, @form{@colnames}
);
Or:
my $sth = $dbi->prepare(
q[ SELECT * FROM table WHERE ] .
join(" AND ", map {"$_ = ?"} keys %form)
);
my $i = 1;
$sth->bind_param( $i++, $_, SQL_VARCHAR ) for values %form;
$sth->execute();
Or if you insist on quoting the things:
my $sth = $dbi->prepare(
q[ SELECT * FROM table WHERE ] .
join(" AND ", map {
my ($k,$v) = ($_, $dbi->quote($_));
"$k = '$v'"
} keys %form)
);
$sth->execute();
or:
my @colnames = ... ;
$dbi->do(
q[ INSERT INTO table VALUES = ('] .
join("', '", map $dbi->quote($_), @form{@colnames} ) .
q[')]
);
I, of course, prefer the quote-less versions.
--
The Swedish Chef has been assimilated. "Borg borg borg!"
------------------------------
Date: Tue, 14 Aug 2001 08:36:12 +0100
From: "Paul Fortescue" <paul@net366.com>
Subject: SQLServer HowTo
Message-Id: <997774500.620.0.nnrp-13.d4f094e4@news.demon.co.uk>
I can program SQL/ODBC stuff in C. I am new but getting better at Perl. I
need to access a SQLSERVER DB in Perl on Linux, I have a DB but I don't know
what to do next.
From reading the manual/newsgroups, it looks like I need some DBI module.
Am I right? Can I get this from anywhere?
Help, please?
------------------------------
Date: Tue, 14 Aug 2001 10:46:41 +0300
From: Roman Khutkyy <sky@mail.lviv.ua>
Subject: Re: SQLServer HowTo
Message-Id: <3B78D761.55DDB810@mail.lviv.ua>
Yes, you need.
To operate with DB in Perl you need at least three modules:
DBI, DBD-SQL(mySQL), Tk.
You can load it from CPAN: www.CPAN.org
------------------------------
Date: 14 Aug 2001 12:51:36 +0400
From: Ilya Martynov <ilya@martynov.org>
Subject: Re: SQLServer HowTo
Message-Id: <873d6vf2hz.fsf@abra.ru>
RK> Yes, you need.
RK> To operate with DB in Perl you need at least three modules:
RK> DBI, DBD-SQL(mySQL), Tk.
RK> You can load it from CPAN: www.CPAN.org
It is not correct. Tk have no relation to database stuff and there is
no DBD-SQL module.
You need Perl module DBI (it is a generic library for for database
access - it is like ODBC in C) and database driver (it is also Perl
module).
I don't know which database you are using so I don't know which driver
do you need. Here incomplete listing of databases and their drivers.
MySQL - DBD::mysql
Postgres - DBD::Pg
Oracle - DBD::Oracle
Sybase - DBD::Sybase
In general all database drivers have names starting with DBD::. If
your database is not in this list check this URL and find driver you
need:
http://search.cpan.org/Catalog/Database_Interfaces/
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Ilya Martynov (http://martynov.org/) |
| GnuPG 1024D/323BDEE6 D7F7 561E 4C1D 8A15 8E80 E4AE BE1A 53EB 323B DEE6 |
| AGAVA Software Company (http://www.agava.com/) |
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
------------------------------
Date: Tue, 14 Aug 2001 06:29:13 GMT
From: "Sean Hamilton" <sh@planetquake.com>
Subject: Stateful CGI
Message-Id: <Zy3e7.56809$b_3.5259463@news0.telusplanet.net>
Greetings,
Firstly I apologise if this is the incorrect group... the only CGI and Perl
related groups I could find had very few posts, and seemed to be abandoned.
Anyhow.
I was wondering what the best way of having a user authentication system is.
I was thinking one of the following:
- Server keeps a list of session IDs, stating the client's IP address.
Client supplies that ID with every request. Problem is that then if the
client idles for a long time, the session times out, and the client has to
re-authenticate, and then it becomes diffiult to know (from the client's
perspective) whether or not the last request registered. NTMail does this,
and it's extremely frustrating writing out a lengthy e-mail, hitting submit,
having to reauthenticate, and not knowing if that email went through --
which it didn't.
- Client sends login and password with every request. Has the advantage of
having pages bookmarked, but if the system is accessed from a public system,
people can refer to the history, and bring up a session.
- Cookies, etc, etc...
Is there a universally accepted solution to this problem, or am I just going
to have to pick one of the above situations and live with the consequences?
sh
------------------------------
Date: 14 Aug 2001 07:16:00 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: string extraction
Message-Id: <9laj7g$9gb$1@mamenchi.zrz.TU-Berlin.DE>
According to Ren Maddox <ren@tivoli.com>:
[...]
> There is one more subtlety hiding here, and it is mentioned in
> perlop(1) under Assignment Operators:
>
> Similarly, a list assignment in list context produces the
> list of lvalues assigned to, and a list assignment in
> scalar context returns the number of elements produced by
> the expression on the right hand side of the assignment.
>
> This can be surprising when you do something like:
>
> my $count = ($a, $b) = (1, 2, 3);
>
> Some might expect $count to be 2, but it is 3. This naturally extends
> to create the above idiom. It sure is a funny way to write "list",
> but since Perl doesn't have a list(), only a scalar(), "() =" has to
> serve.
Speaking of which, if the list on the right hand side is the result of
a split(), you may be surprised again.
my $count = ($a, $b) = split " ", "1 2 3";
sets $count to 2. This is because split() in a list assignment peeks
at the list it is being assigned to and splits only as many fields
as needed. Therefore the "$count = () = ..." trick can't be used to
find the number of fields a string will split in.
Anno
------------------------------
Date: Tue, 14 Aug 2001 07:28:27 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: string extraction
Message-Id: <3B78D3BC.B9E7CA15@acm.org>
Anno Siegel wrote:
>
> According to Ren Maddox <ren@tivoli.com>:
>
> [...]
>
> > There is one more subtlety hiding here, and it is mentioned in
> > perlop(1) under Assignment Operators:
> >
> > Similarly, a list assignment in list context produces the
> > list of lvalues assigned to, and a list assignment in
> > scalar context returns the number of elements produced by
> > the expression on the right hand side of the assignment.
> >
> > This can be surprising when you do something like:
> >
> > my $count = ($a, $b) = (1, 2, 3);
> >
> > Some might expect $count to be 2, but it is 3. This naturally extends
> > to create the above idiom. It sure is a funny way to write "list",
> > but since Perl doesn't have a list(), only a scalar(), "() =" has to
> > serve.
>
> Speaking of which, if the list on the right hand side is the result of
> a split(), you may be surprised again.
>
> my $count = ($a, $b) = split " ", "1 2 3";
>
> sets $count to 2. This is because split() in a list assignment peeks
> at the list it is being assigned to and splits only as many fields
> as needed. Therefore the "$count = () = ..." trick can't be used to
> find the number of fields a string will split in.
$ perl -le'my $count = ($a, $b) = split " ", "1 2 3";print $count'
3
John
--
use Perl;
program
fulfillment
------------------------------
Date: 14 Aug 2001 08:03:12 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: string extraction
Message-Id: <9lam00$cru$2@mamenchi.zrz.TU-Berlin.DE>
According to John W. Krahn <krahnj@acm.org>:
> Anno Siegel wrote:
> >
> > According to Ren Maddox <ren@tivoli.com>:
> >
> > [...]
> >
> > > There is one more subtlety hiding here, and it is mentioned in
> > > perlop(1) under Assignment Operators:
> > >
> > > Similarly, a list assignment in list context produces the
> > > list of lvalues assigned to, and a list assignment in
> > > scalar context returns the number of elements produced by
> > > the expression on the right hand side of the assignment.
> > >
> > > This can be surprising when you do something like:
> > >
> > > my $count = ($a, $b) = (1, 2, 3);
> > >
> > > Some might expect $count to be 2, but it is 3. This naturally extends
> > > to create the above idiom. It sure is a funny way to write "list",
> > > but since Perl doesn't have a list(), only a scalar(), "() =" has to
> > > serve.
> >
> > Speaking of which, if the list on the right hand side is the result of
> > a split(), you may be surprised again.
> >
> > my $count = ($a, $b) = split " ", "1 2 3";
> >
> > sets $count to 2. This is because split() in a list assignment peeks
> > at the list it is being assigned to and splits only as many fields
> > as needed. Therefore the "$count = () = ..." trick can't be used to
> > find the number of fields a string will split in.
>
> $ perl -le'my $count = ($a, $b) = split " ", "1 2 3";print $count'
> 3
perl -le 'my $count = ($a) = split " ", "1 2 3";print $count'
2
split() behaves as if a third (LIMIT) parameter of one more than the
length of the list being assigned to had been given. It must be one
more so that the final element is actually split, instead of containing
the rest of the string.
Oh, and the workaround is to give an explicit limit of -1:
perl -le'my $count = () = split " ", "1 2 3", -1; print $count'
3
Anno
------------------------------
Date: Tue, 14 Aug 2001 02:45:24 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: System Call Question
Message-Id: <3B78C904.9AEBAE4A@earthlink.net>
Mark Riehl wrote:
>
> All - I've got a Win2k box and I'd like to make a system call
> (ipconfig) and determine my own IP address (trying to debug an ISP
> issue with changing IP addresses).
Have you considered using Net::Domain or Sys::Hostname modules?
> Is there a way to redirect the results of the system call into
> an array (similar to an ARGV)? This way, I could walk through the
> array and find out my IP address. Or, would it be easier to redirect
> the results of a system call into a file, open the file, and find the
> IP address.
my @ipconfig = qx[ipconfig];
my ($hostname) = $ipconfig[whatever] =~ m/blah(blah)blah/;
It would be better to use one of the modules I suggested, though.
Especially since, if it's smart, Sys::Hostname should try and call
ipconfig if it's on windows. The docs for it says that one of the
various things it tries is `uname -n` which is likely to only work on
unix -- this could easily be replaced by an appropriate ipconfig call.
--
The Swedish Chef has been assimilated. "Borg borg borg!"
------------------------------
Date: Tue, 14 Aug 2001 02:09:51 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: TK:Date and Access database problem
Message-Id: <3B78C0AF.53A63F87@earthlink.net>
Peter Mann wrote:
>
> Dear All,
>
> I am stuck on a problem relating to inserting a date into an Access
> database. I am correctly retrieving the date from a TK::Date widget.
> However, when constructing the SQL statement and inserting a record
> into a database, a date of '30/12/1899' is always displayed. I suspect
> this might be due to the widget returning a 'string' and i need to
> insert a variable of type 'date' fromat within the record.
>
> Does anyone know if this is the correct reason and if so are there are
> functions available to do this converison. Or am i completely wrong in
> my assumptions?
>
> The code extract of the relevant section is:
>
> -----------------------------------------------------------------------------
> my $start = $startDate->get("%d/%m/%y");
> my $complete = $endDate->get("%d/%m/%y");
>
> print $start; # For debug purposes! "THIS DISPLAYS THE CORRECT DATE"
>
> #construct SQL statement (BUT THIS DOESN'T)
> $sqlstatement= "INSERT INTO tasks (taskName, start, complete) VALUES
> ('Objective', '$start', $complete) ";
What exact types are taskName, start, complete? integer, varchar, what?
Also, does it make any difference if you do:
my $sth = $dbi->prepare( q[
INSERT INTO tasks (taskName, start, complete)
VALUES ( 'Objective', ?, ?)
] );
$sth->execute( $start, $complete );
# or
$sth->bind_values( 1, $start, SQL_DATE ); # or whatever
$sth->bind_values( 2, $complete, SQL_DATE ); # same here
$sth->execute();
If that fails, try to use Date::Parse or timelocal or strftime or
whatever to turn it into the correct format.
--
The Swedish Chef has been assimilated. "Borg borg borg!"
------------------------------
Date: Tue, 14 Aug 2001 04:22:17 -0000
From: Craig Berry <cberry@cinenet.net>
Subject: Re: VIER/NEUN problem
Message-Id: <Xns90FCD963CEF51cberrycinenetnet1@207.126.101.92>
gbacon@HiWAAY.net (Greg Bacon) wrote in news:tnh3k1oivjjia8
@corp.supernews.com:
> In article <Xns90FCA3C10E077cberrycinenetnet1@207.126.101.92>,
> Craig Berry <cberry@cinenet.net> wrote:
>: and simplifying a great deal, [...]
>
> You misspelled 'oversimplifying'. Reread the problem statement.
I did, several times...what did I miss? For the record, my script
yields the solution
VIER = 1764
NEUN = 5625
--
Craig Berry <http://www.cinenet.net/~cberry/>
"That which is now known, was once only imagined." - William Blake
------------------------------
Date: 14 Aug 2001 04:34:21 GMT
From: trammell@bayazid.hypersloth.invalid (John J. Trammell)
Subject: Re: VIER/NEUN problem
Message-Id: <slrn9ngopn.s88.trammell@haqq.hypersloth.net>
On Tue, 14 Aug 2001 04:22:17 -0000, Craig Berry <cberry@cinenet.net> wrote:
> gbacon@HiWAAY.net (Greg Bacon) wrote in news:tnh3k1oivjjia8
> @corp.supernews.com:
>
> > In article <Xns90FCA3C10E077cberrycinenetnet1@207.126.101.92>,
> > Craig Berry <cberry@cinenet.net> wrote:
> >: and simplifying a great deal, [...]
> >
> > You misspelled 'oversimplifying'. Reread the problem statement.
>
> I did, several times...what did I miss? For the record, my script
> yields the solution
>
> VIER = 1764
> NEUN = 5625
An answer with neun=5625 is not unique; there are three candidates:
neun=5625 vier=1369
neun=5625 vier=1764
neun=5625 vier=4761
--
Salad: it's what's for dinner, for what's for dinner.
------------------------------
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 1517
***************************************