[15444] in Perl-Users-Digest
Perl-Users Digest, Issue: 2854 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Apr 24 18:05:33 2000
Date: Mon, 24 Apr 2000 15:05:18 -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: <956613918-v9-i2854@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Mon, 24 Apr 2000 Volume: 9 Number: 2854
Today's topics:
Re: Archiving emails for later Web browsing <gellyfish@gellyfish.com>
Re: Best way to hide source <gellyfish@gellyfish.com>
Re: call sub from browser <gellyfish@gellyfish.com>
Can I use perl to report free disk space in NT? <dbmNOdbSPAM@mainlylinux.dhs.org.invalid>
Re: Critique My Code nobull@mail.com
Re: Critique My Code <lr@hpl.hp.com>
Re: Critique My Code <sariq@texas.net>
Crypt module suggestions <adams1015@worldnet.att.net>
Re: Date standardization without Date::Manip drode@my-deja.com
Re: Date standardization without Date::Manip <sariq@texas.net>
DBM files and security on the web... but86@my-deja.com
Re: How can I delay "use"ing a module until the module <gellyfish@gellyfish.com>
Re: How can I delay "use"ing a module until the module <gellyfish@gellyfish.com>
Re: How can I delay "use"ing a module until the module <DNess@Home.Com>
How to get from STDOUT to browser download? <todd@ti.com>
Re: JPEG and GIF MIME type decoding <gellyfish@gellyfish.com>
Matching "[" ? psyshrike@my-deja.com
Re: Matching "[" ? <aperrin@davis.DEMOG.Berkeley.EDU>
Re: Matching "[" ? <lr@hpl.hp.com>
Re: more string handling (Greg Bacon)
Re: more string handling (Craig Berry)
mysql <john@princenaseem.com>
Re: mysql <jeff@vpservices.com>
Re: mysql <lr@hpl.hp.com>
Re: mysql <schapel@breakthr.com>
Re: mysql (Randal L. Schwartz)
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 24 Apr 2000 08:53:20 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: Archiving emails for later Web browsing
Message-Id: <8e0uhg$f0$1@orpheus.gellyfish.com>
In comp.lang.perl.misc UnixMan <unixman@cfl.rr.com> wrote:
> I want to create a perl script that will add any incoming emails to a
> database that will be browseable/searchable on the web. I just need to know
> how the relationship is set between an email and a reply to it. In other
> words, when you reply to an email, is there any "flag" (other that leaving
> the Re: + original subject) that will "bind" those two messages as original
> amd reply?
>
Some mail user agents will supply a 'References' or 'In-Reply-To' header
but what this has to do with Perl I dont know. You might want to look at
MHonarc which is an existing program that does this and is written in Perl.
/J\
--
Uh huh. Uh huh. Okay. Um, can you repeat the part of the stuff where
you said all about the...things? Uh... the things?
--
fortune oscar homer
------------------------------
Date: 24 Apr 2000 19:00:23 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: Best way to hide source
Message-Id: <8e223n$366$1@orpheus.gellyfish.com>
On Mon, 24 Apr 2000 17:26:04 GMT System Administrator wrote:
>
> Ok first of all I don't want to get in any arguments about open source here:)
OK. Perhaps you come back when you learn to have your newsreader wrap
at a socially acceptable 72 characters.
/J\
--
Hmm...ow, pointy! Eww, slimy. Oh, moving! Ah-ha! Awww, 20 dollars?!? I
wanted a peanut.
--
fortune oscar homer
------------------------------
Date: 24 Apr 2000 08:47:02 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: call sub from browser
Message-Id: <8e0u5m$ed$1@orpheus.gellyfish.com>
On Sun, 23 Apr 2000 16:41:02 GMT dmayo2@yahoo.com wrote:
> Hello...
>
> I was wondering if there is a way to call a subroutine in a perl script
> directly from the browsers location bar.
>
> e.g.
>
> /cgi-bin/perlsrcipt.cgi?subroutine3
>
dispatch tables :
use CGI qw(:standard);
print header('text/plain');
my %subroutines = (
sub1 => \&sub1,
sub2 => \&sub2,
sub3 => \&sub3
);
if (ref($subroutines{param('action')) eq 'CODE' )
{
$subroutines{param('action')}->();
}
else
{
print "Not a valid action";
}
sub1
{
print "This is sub 1\n";
}
sub2
{
print "This is sub 2\n";
}
sub3
{
print "This is sub 3\n";
}
You will reference this as <http://whatever/cgi-bin/script?action=sub1>
/J\
--
This is absolutely the last funeral we ever take you kids to.
--
fortune oscar homer
------------------------------
Date: Mon, 24 Apr 2000 13:20:28 -0700
From: dbm <dbmNOdbSPAM@mainlylinux.dhs.org.invalid>
Subject: Can I use perl to report free disk space in NT?
Message-Id: <07380bda.9b4ea534@usw-ex0104-033.remarq.com>
I am trying to write a (please don't laugh) Batch file to check
the available disk space on about 45 servers. Since I do this
at each and every console every morning this could save me a
considerable amount of time. I have checked out all of the
commands I can think of and their switches. I have heard great
things about perl and was thinking maybe it could overcome some
of the batch file deficiencies.
I am going to run this machine on an NT domain, using an account
with admin rights. All of the disk space queries will be done
using unc names, not hard mapping.
The output will be dumped into a text file, which will then be
displayed on the console.
I have no experience with perl
Dan
* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!
------------------------------
Date: 24 Apr 2000 19:04:36 +0100
From: nobull@mail.com
Subject: Re: Critique My Code
Message-Id: <u9g0sbml3f.fsf@wcl-l.bham.ac.uk>
"Gabe" <grichards@flashcom.net> writes:
> I'm a relative novice. I've never had any formal programming training, so I
> wonder if my programs are just horribly inefficient. I'm looking for
> efficiency and style advice.
Looks fairly damn good to me. I've offered a bit of style advice. Of
course there are no absolutes in style and many people may disagree
with me.
> Are there better methods to do what I'm trying
> to do?
GCI.pm provides methods to compose HTML and I notice that you've not
used them and generate HTML on-the-fly bit by bit in loops. Generally
I prefer to build lists then use the fuctions in GCI.pm to build the
HTML. Looks neater (runs slower).
> My code is below.
>
> #!/usr/local/bin/perl
> #This program searches a flat-file text database for record matches on
Spaces after the # make comments more readable.
> $request and $page.
And keep lines down to about 70 characters - especially if you ever
post code to Usenet.
> #The program then displays the thumbnail images the record refers to as a
> link to the larger image.
>
> #Standard stuff.
This sort of comment is not generally a good thing.
> use strict;
> use lib '/home/mopicmag/www/modules';
Usually better to manipulate PERL5LIB outside Perl if you can.
Depends on you HTTP server s/w. If you can't manipulate PERL5LIB then
"use lib" is the next best thing.
> use CGI;
>
> my $cgi = new CGI;
>
> #These variables are what we are searching our database for.
> #$request is the name of the event the image is categorized under.
> my $request = lc($cgi->param('req'));
> #$page is the page on the site we assigned the picture in the database.
> my $page = $cgi->param('page');
>
> main: {
> &event;
It's generally considered better style to declare your subroutines
before you use them rather than need to put in an explict & in the
call.
What's that label for anyhow?
> }
>
> #Return images if user is searching by event category (i.e. not a keyword
> search).
> sub event {
>
> #The upper template of our standard HTML page.
> print $cgi->header;
> open (HTML, '/home/mopicmag/www/templates/headertemplate1.txt') or die
> &error;
> print while (<HTML>);
> close HTML;
>
> #Some flags I'm going to use.
> my $more = 0;
> my $counter = 0;
> my $counter2 = 0;
>
> #Open the database file with records of all the images stored on the web
> server.
> open (DBASE, '/home/mopicmag/www/picbase/picbase') or die &error;
> my
> ($picid,$picdes,$path,$eventid,$dir,$eventname,$eventdes,$eventdate,$loc);
It's probably better to my() these within the loop -- loose the above line.
> while (<DBASE>) {
>
> ($picid,$picdes,$path,$eventid,$dir,$eventname,$eventdes,$eventdate,$loc)=sp
> lit("\t", lc($_));
Prepend my above.
> #If 3 thumbnails have been displayed end this table row and begin a new
> one.
> if ($counter == 3)
>
> #This $counter2 thing is so that if there are ONLY 3 images on this page,
> a bunch of empty rows won't be created.
> $counter2++;
> if ($counter2 == 1) { print "</td></tr><tr align=center><td
> align=center>";}
> }
> #In case we ever allow more than 5 thumbs per page align them in rows of
> 3.
> elsif ($counter > 3) {
> if (($counter%3) == 0) { print "</td></tr><tr align=center><td
> align=center>";}
> }
>
> #If $request matches, and there are more "pages" flag $more.
> if ($dir eq $request) {
> if ($loc == ($page+1)) { $more = 1;}
> }
>
> #If 5 images have printed and there are more pages print the link and exit
> the loop.
> if ($counter == 5) {
> if ($more == 1) {
As far as I can see $more is being treated as Boolean. If this is the
case then it would be more readable to simply say:
if ($more) {
> my $nextpage = ($page + 1);
> print "</td></tr><tr><td align=center><a
> href=http://www.mopicmag.com/cgi-bin/findpic.cgi?req=$request&page=$nextpage
> >Page $nextpage</a></td>";
> last;
> }
> }
>
> #If we haven't yet found 5 pictures check if $request and $page match and
> if so print the image and link.
> if ($counter <= 4) {
> if ($dir eq $request) {
> if ($page == $loc) {
> my $lnk = "$path.jpg";
> my $img = ($path . 'a' . '.jpg');
Most people would say:
my $img = "${path}a.jpg";
> print "<a href=$lnk><img border=0 src=$img></a>\ \;\ \;";
> $counter++;
> }
> }
> }
>
> #If we're at the end of file check whether there are more "pages" in this
> category and display the appropriate link.
> if (eof) {
> if ($more == 1) {
> my $nextpage = ($page + 1);
> print "</td></tr><tr><td align=center><a
> href=http://www.mopicmag.com/cgi-bin/findpic.cgi?req=$request&page=$nextpage
> >Page $nextpage</a></td>";
> last;
> }
> else { print "</td></tr><tr><td align=center><a
> href=http://www.mopicmag.com/gallery.html>Back to Gallery</a></td>";}
> }
> }
> close DBASE;
>
> #The lower template to our standed HTML page.
> open (HTML, '/home/mopicmag/www/templates/footertemplate1.txt') or die
> &error;
> print while (<HTML>);
> close HTML;
> }
>
> sub error {
> print $cgi->header;
> print $cgi->start_html(-title=>'Error');
> print $cgi->h2("Error Message: $!");
> print $cgi->end_html;
> }
------------------------------
Date: Mon, 24 Apr 2000 11:42:23 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Critique My Code
Message-Id: <MPG.136e3567812366f998a96a@nntp.hpl.hp.com>
In article <u9g0sbml3f.fsf@wcl-l.bham.ac.uk> on 24 Apr 2000 19:04:36
+0100, nobull@mail.com <nobull@mail.com> says...
> "Gabe" <grichards@flashcom.net> writes:
>
> > I'm a relative novice. I've never had any formal programming training, so I
> > wonder if my programs are just horribly inefficient. I'm looking for
> > efficiency and style advice.
>
> Looks fairly damn good to me. I've offered a bit of style advice. Of
> course there are no absolutes in style and many people may disagree
> with me.
Why 'may'? I'd call it a sure thing. :-)
> > Are there better methods to do what I'm trying
> > to do?
>
> GCI.pm provides methods to compose HTML and I notice that you've not
> used them and generate HTML on-the-fly bit by bit in loops. Generally
> I prefer to build lists then use the fuctions in GCI.pm to build the
> HTML. Looks neater (runs slower).
See, first disagreement. I've written programs either way, and don't
see much advantage to the HTML-generating routines in CGI.pm. You still
have to understand HTML and how to write it.
> > My code is below.
> >
> > #!/usr/local/bin/perl
I would add the '-w' flag and (unless you prefer to have diagnostics go
to the server log):
use CGI::Carp 'fatalsToBrowser';
...
> > main: {
> > &event;
>
> It's generally considered better style to declare your subroutines
> before you use them rather than need to put in an explict & in the
> call.
That way lies Pascal (upside-down programs). If you object to
'&event;', just use
event();
...
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Mon, 24 Apr 2000 13:59:40 -0500
From: Tom Briles <sariq@texas.net>
Subject: Re: Critique My Code
Message-Id: <3904999C.821D0FA9@texas.net>
Gabe wrote:
>
In addition to what others have said:
>
> #!/usr/local/bin/perl
You'll want to turn on warnings and taint checking here (-wT).
> #If we haven't yet found 5 pictures check if $request and $page match and
> if so print the image and link.
> if ($counter <= 4) {
> if ($dir eq $request) {
> if ($page == $loc) {
> my $lnk = "$path.jpg";
> my $img = ($path . 'a' . '.jpg');
> print "<a href=$lnk><img border=0 src=$img></a>\ \;\ \;";
> $counter++;
> }
> }
> }
Take a look at the '&&' operator in the perlop page of the manual.
- Tom
------------------------------
Date: Mon, 24 Apr 2000 19:26:24 GMT
From: "Veronica Adams" <adams1015@worldnet.att.net>
Subject: Crypt module suggestions
Message-Id: <Ad1N4.32945$WF.1511925@bgtnsc04-news.ops.worldnet.att.net>
I will be encryoting credit card information. Anybody have any suggestions
on which of the various crypt modules to use? Which ones to avoid? or a
favorite for any of you old hats?
thanks
------------------------------
Date: Mon, 24 Apr 2000 19:41:14 GMT
From: drode@my-deja.com
Subject: Re: Date standardization without Date::Manip
Message-Id: <8e280o$6n8$1@nnrp1.deja.com>
I am not asking if I should use the mod, I am stating that I cannot use
it.
Therefore, I would like to know if anyone else has sloved a similar
problem WITHOUT using Date:Manip.
>
> Have you seen the advice in the Date::Manip docs about "SHOULD I USE
> DATE::MANIP"? Cheers!
>
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Mon, 24 Apr 2000 15:00:27 -0500
From: Tom Briles <sariq@texas.net>
Subject: Re: Date standardization without Date::Manip
Message-Id: <3904A7DB.C197B71A@texas.net>
[Jeopardectomy performed.]
drode@my-deja.com wrote:
>
> >
> > Have you seen the advice in the Date::Manip docs about "SHOULD I USE
> > DATE::MANIP"? Cheers!
> >
>
> Therefore, I would like to know if anyone else has sloved a similar
> problem WITHOUT using Date:Manip.
Place your response *after* the quoted text. Properly quote the message
so that the author is attributable.
Did you bother following Tom Phoenix's advice? Well, you should.
HTH. HAND.
- Tom
------------------------------
Date: Mon, 24 Apr 2000 19:41:53 GMT
From: but86@my-deja.com
Subject: DBM files and security on the web...
Message-Id: <8e281v$6nb$1@nnrp1.deja.com>
Hello Perl People!
I am developing a larger web based system. In order to save
configuration data, I would like to use the standard sdbm file class.
but how can i prevent bad hackers from downloading the .dir and .pag
files since they have to be readable/writable?
requiring a normal file with a normal hash declaration would do the
same, but that isn`t so nice.
any suggestions?
thnx,
werner
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 24 Apr 2000 09:53:00 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: How can I delay "use"ing a module until the module is needed?
Message-Id: <8e121c$jr$1@orpheus.gellyfish.com>
On Mon, 24 Apr 2000 04:59:37 GMT alexandria_sarkut@my-deja.com wrote:
>
> You have stated,
>
> "Whatever is in the BEGIN block (_regardless_ of where
> the BEGIN block appears in your code) executes before
> everything else...."
>
> I also disagree with this statement along with previous
> statements of this nature posted within this thread.
>
You can disagree all you like but fortunately it doesnt make it any less
true as a small example will demonstrate :
print "This is a print number 1\n";
BEGIN
{
print "This is a print with a BEGIN block\n";
}
print "This is a print number 2\n";
You also appear to have ignored my previous example which shows that a 'use'
within a conditional block *is* evaluated at compile time irrespective of
any apparent flow of control within the program at run time.
It is noted that you present no counter-examples to support your assertions.
/J\
--
I bet Einstein turned himself all sorts of colors before he invented
the light bulb.
--
fortune oscar homer
------------------------------
Date: 24 Apr 2000 18:30:37 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: How can I delay "use"ing a module until the module is needed?
Message-Id: <8e20bt$34a$1@orpheus.gellyfish.com>
On Mon, 24 Apr 2000 17:40:49 GMT David Ness wrote:
> alexandria_sarkut@my-deja.com wrote:
>>
>> Technical journal writers should be subject to
>> a prerequisite of a degree in English before
>> being allowed to contribute technical articles
>> for publication.
>
> Well, given some people I know who _have_ degrees in English,
You dissing me sucka ;-}
<apologies to anyone without a sense of humour>
/J\
--
Marge, I agree with you -- in theory. In theory, communism works. In
theory.
--
fortune oscar homer
------------------------------
Date: Mon, 24 Apr 2000 21:15:37 GMT
From: David Ness <DNess@Home.Com>
Subject: Re: How can I delay "use"ing a module until the module is needed?
Message-Id: <3904B980.9574CBFB@Home.Com>
Jonathan Stowe wrote:
>
> >
> > Well, given some people I know who _have_ degrees in English,
>
> You dissing me sucka ;-}
>
> <apologies to anyone without a sense of humour>
>
> /J\
I suppose I should have added `But some of my favorite people have them...'
;-)...
------------------------------
Date: Mon, 24 Apr 2000 16:19:19 -0500
From: Todd Bair <todd@ti.com>
Subject: How to get from STDOUT to browser download?
Message-Id: <3904BA57.5B22DE93@ti.com>
How do I force a download without changing the page location?
Basically I need a button to download a text file for those
engineers who are right button challenged. I read some
place that if you open a file to stdout a "save as" box should
appear in the browser. However, I am unable to reproduce this.
So far I have...
&download if (param('getFile'));
sub download
{
open(DOWNLOAD, "</tmp/foo.txt);
close(DOWNLOAD);
}
Thanks,
Todd
------------------------------
Date: 24 Apr 2000 10:56:51 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: JPEG and GIF MIME type decoding
Message-Id: <8e15p3$184$1@orpheus.gellyfish.com>
On Mon, 24 Apr 2000 11:21:42 +0100 Nadja Herkova wrote:
> I've written a Perl program which allows user to upload a JPEG or GIF
> file through a web page form. I've got the upload working but as a
> final operation, I am trying to find out a way of reading the size in
> pixels of the image from the encoded data.. has anyone any references
> that would help me extract this data ?
The module Image::Size from CPAN does this.
/J\
--
Television! Teacher, mother, secret lover.
--
fortune oscar homer
------------------------------
Date: Mon, 24 Apr 2000 20:22:14 GMT
From: psyshrike@my-deja.com
Subject: Matching "[" ?
Message-Id: <8e2ade$921$1@nnrp1.deja.com>
Hey,
I want to match /\c[[A/ (up arrow) but \ does not escape [
-Any help appreciated.
-Matt
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 24 Apr 2000 13:59:22 -0700
From: Andrew Perrin - Demography <aperrin@davis.DEMOG.Berkeley.EDU>
Subject: Re: Matching "[" ?
Message-Id: <u5kg0sb43md.fsf@davis.DEMOG.Berkeley.EDU>
psyshrike@my-deja.com writes:
> Hey,
>
> I want to match /\c[[A/ (up arrow) but \ does not escape [
Yes it does. Please post code that shows it not working.
aperrin@davis ~/dt2test> perl -e'print qq{bingo\n} if q{foo[]} =~ /\[/;'
bingo
--
---------------------------------------------------------------------
Andrew J. Perrin - aperrin@demog.berkeley.edu - NT/Unix Admin/Support
Department of Demography - University of California at Berkeley
2232 Piedmont Avenue #2120 - Berkeley, California, 94720-2120 USA
http://demog.berkeley.edu/~aperrin --------------------------SEIU1199
------------------------------
Date: Mon, 24 Apr 2000 13:59:28 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Matching "[" ?
Message-Id: <MPG.136e558fdadbb65098a96f@nntp.hpl.hp.com>
In article <8e2ade$921$1@nnrp1.deja.com> on Mon, 24 Apr 2000 20:22:14
GMT, psyshrike@my-deja.com <psyshrike@my-deja.com> says...
> I want to match /\c[[A/ (up arrow) but \ does not escape [
Do you want to match three characters: CNTRL-[, [, A ?
/\c[\[A/
Why do you say \ does not escape [?
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 24 Apr 2000 18:58:52 GMT
From: gbacon@ruby.itsc.uah.edu (Greg Bacon)
Subject: Re: more string handling
Message-Id: <8e25hc$dmk$1@info2.uah.edu>
In article <MPG.136e23a491a3c9be98a967@nntp.hpl.hp.com>,
Larry Rosler <lr@hpl.hp.com> writes:
: In article <8e1upk$cc2$2@info2.uah.edu> on 24 Apr 2000 17:03:48 GMT,
: Greg Bacon <gbacon@ruby.itsc.uah.edu> says...
:
: > $str =~ s/^(.{77}).{4,}+/$1.../s;
: ^
: Nice, but the '+' quantifier is redundant.
Erroneous, even. :-( The substitution was originally
$str =~ s/^(.{77})....+/$1.../s;
but I decided I wanted more verbosity.
Greg
--
Kenny: hmm-hmm hmm hmm hmm hmm hmm-hmm!
Stan: Dude, how'd you know she has a cat?
------------------------------
Date: Mon, 24 Apr 2000 20:30:09 GMT
From: cberry@cinenet.net (Craig Berry)
Subject: Re: more string handling
Message-Id: <sg9bmhi0cdo148@corp.supernews.com>
red (redmonkey@madasafish.comx) wrote:
: okay, this might be a little hard to explain:
: i want to limit a string to say 80 characters,
: if its more, i want it cut, and add ... to the end.
: if its not that long, dont add 3 dots.
If you mean 'cut' as in 'discard' (as opposed to 'display on a new line'),
then:
$string =~ s/(.{80}).+/$1.../;
This assumes no internal newlines. If there are newlines and you want to
treat them like any other characters, add the /s qualifier.
--
| Craig Berry - cberry@cinenet.net
--*-- http://www.cinenet.net/users/cberry/home.html
| "The road of Excess leads to the Palace
of Wisdom" - William Blake
------------------------------
Date: Mon, 24 Apr 2000 18:23:19 GMT
From: "JohnShep" <john@princenaseem.com>
Subject: mysql
Message-Id: <3904a014.0@news2.cluster1.telinco.net>
I have a delimeter problem with the sub below. It works fine except if one
of the name vars contains an apostraphe eg. "O'Brien" when it falls over
quite nicely. Is there a simple way around this ?
Thanks, JohnShep
sub get_biog {
local $query="select * from biogs where (
first_name='$_[0]' ) and (
last_name='$_[1]' )";
local $sth=$dbh->prepare($query);
$sth->execute or die "failed in get biog $sth->errstr\n";
@vals=$sth->fetchrow;
$sth->finish;
return @vals;
}
------------------------------
Date: Mon, 24 Apr 2000 11:40:40 -0700
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: mysql
Message-Id: <39049528.7FAB468B@vpservices.com>
JohnShep wrote:
>
> I have a delimeter problem with the sub below. It works fine except if one
> of the name vars contains an apostraphe eg. "O'Brien" when it falls over
> quite nicely. Is there a simple way around this ?
> [snip]
> local $query="select * from biogs where (
> first_name='$_[0]' ) and (
> last_name='$_[1]' )";
> local $sth=$dbh->prepare($query);
> $sth->execute or die "failed in get biog $sth->errstr\n";
In order to protect against these errors, and others, use placeholders.
So, rewrite those lines as:
my $query = "select * from biogs where first_name = ? and last_name =
?";
my $sth = $dbh->prepare($query);
$sth->execute( $_[0],$_[1] ) or die "failed in get biog
$sth->errstr\n";
--
Jeff
------------------------------
Date: Mon, 24 Apr 2000 12:04:54 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: mysql
Message-Id: <MPG.136e3ab5b5b436998a96c@nntp.hpl.hp.com>
In article <3904a014.0@news2.cluster1.telinco.net> on Mon, 24 Apr 2000
18:23:19 GMT, JohnShep <john@princenaseem.com> says...
> I have a delimeter problem with the sub below. It works fine except if one
> of the name vars contains an apostraphe eg. "O'Brien" when it falls over
> quite nicely. Is there a simple way around this ?
There are two ways around this.
> sub get_biog {
> local $query="select * from biogs where (
> first_name='$_[0]' ) and (
> last_name='$_[1]' )";
> local $sth=$dbh->prepare($query);
> $sth->execute or die "failed in get biog $sth->errstr\n";
>
> @vals=$sth->fetchrow;
> $sth->finish;
> return @vals;
> }
Use 'my', not 'local'. perlfaq7: "What's the difference between dynamic
and lexical (static) scoping? Between local() and my()?"
Way 1: Before inserting the arguments into the SQL statement, use the
$dbh->quote() method to protect apostrophes and backslashes.
Way 2 (better by far, IMO): Use placeholders for the parameters, and
supply them with the call to execute(). UNTESTED:
my $query = # Note my style choice to capitalize SQL keywords.
'SELECT * FROM biogs WHERE first_name = ? AND last_name = ?';
my $sth = $dbh->prepare($query);
$sth->execute(@_) or die "failed in get biog $sth->errstr\n";
Your diagnostic won't print the error string, because that is a method
call, which doesn't get expanded within doble-quotes. Use DBI::errstr
instead.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Mon, 24 Apr 2000 13:30:38 -0500
From: "Steve Chapel" <schapel@breakthr.com>
Subject: Re: mysql
Message-Id: <8e23sg$gor$1@news.tribune.com>
"JohnShep" <john@princenaseem.com> wrote in message
news:3904a014.0@news2.cluster1.telinco.net...
> I have a delimeter problem with the sub below. It works fine except if
one
> of the name vars contains an apostraphe eg. "O'Brien" when it falls
over
> quite nicely. Is there a simple way around this ?
>
> local $query="select * from biogs where (
> first_name='$_[0]' ) and (
> last_name='$_[1]' )";
This is actually a SQL question; apostrophes in SQL quotes must be
doubled:
$_[1] =~ s/'/''/g;
------------------------------
Date: 24 Apr 2000 13:03:16 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: mysql
Message-Id: <m1n1mjl117.fsf@halfdome.holdit.com>
>>>>> "Steve" == Steve Chapel <schapel@breakthr.com> writes:
Steve> This is actually a SQL question; apostrophes in SQL quotes must be
Steve> doubled:
Steve> $_[1] =~ s/'/''/g;
And even then, you should never do that -- use DBI placeholders! No
need to ever execute the code above, or try to figure out what's
significant to a particular database backend.
--
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: 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 V9 Issue 2854
**************************************