[23771] in Perl-Users-Digest
Perl-Users Digest, Issue: 5975 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Dec 23 18:05:40 2003
Date: Tue, 23 Dec 2003 15:05:08 -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 Tue, 23 Dec 2003 Volume: 10 Number: 5975
Today's topics:
Re: \r disappear even in binary mode <pkent77tea@yahoo.com.tea>
Re: \r disappear even in binary mode <noone@nowhere.com>
Re: \r disappear even in binary mode <dwall@fastmail.fm>
Re: \r disappear even in binary mode <noone@nowhere.com>
Re: \r disappear even in binary mode <uri@stemsystems.com>
Re: help with file test <srsriram@sriram.com>
Re: help with file test <noreply@gunnar.cc>
Re: help with file test <srsriram@sriram.com>
Re: help with file test <noreply@gunnar.cc>
Re: help with file test <matthew.garrish@sympatico.ca>
Re: help with file test <gnari@simnet.is>
How to execute javascript from within perl? (Jon)
LWP with proxy problem (Woogie)
LWP::Simple and Cookies not working <webmaster@heavyharmonies.com>
monitor windows processes <wi@rjsolutions.org>
Multiple SELECT queries <mislam@spamless.uiuc.edu>
Re: Multiple SELECT queries <nospam@bigpond.com>
Re: Multiple SELECT queries <Paul@Hovnanian.com>
Re: newbie: Terminating while($line=<stdin>) on Windows <me@privacy.net>
Re: newbie: Terminating while($line=<stdin>) on Windows <matthew.garrish@sympatico.ca>
Re: Posting Guidelines for comp.lang.perl.misc ($Revisi <szinger@lanl.gov>
Re: Return all points with x km of y ctcgag@hotmail.com
Re: Running a New Process <No_4@dsl.pipex.com>
search interval <yhu@mail.nih.gov>
Re: Simple telnet-like access <me@privacy.net>
Re: Simple telnet-like access <pkent77tea@yahoo.com.tea>
Time out SSL request? (Michael Capone)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 23 Dec 2003 19:18:01 +0000
From: pkent <pkent77tea@yahoo.com.tea>
Subject: Re: \r disappear even in binary mode
Message-Id: <pkent77tea-0179AB.19180123122003@ptb-nnrpp01.plus.net>
In article <pan.2003.12.23.17.12.07.10434@nowhere.com>,
noone <noone@nowhere.com> wrote:
> I wrote the following perl script test.pl which generates another perl
> script:
>
> binmode(STDOUT);
> print "binmode(STDOUT);print \'\r\n\';"
>
> When I run the generated perl script, it only prints the \n:
>
> $ perl test.pl | perl - | hexdump -c
> 0000000 \n
> 0000001
>
> Is it a bug or a feature? How to avoid it?
It's doing the right thing. Literal CRs are skipped when perl reads the
source under some conditions, including this condition:
http://www.perldoc.com/perl5.005_03/pod/perldelta.html#More-generous-trea
tment-of-carriage-returns
( http://shrunk.net/?c=9801d4dd )
If you want the perl-program-that-was-generated to produce a CR on _its_
STDOUT you need:
#!/usr/local/bin/perl -w
use strict;
binmode(STDOUT);
print 'binmode(STDOUT);print "\r\n";';
and if I do:
perl x.pl | perl - | hexdump
I get:
00000000 : 0D 0A
which is what you want, I think.
P
--
pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
Remove the tea to reply
------------------------------
Date: Tue, 23 Dec 2003 21:26:06 +0100
From: noone <noone@nowhere.com>
Subject: Re: \r disappear even in binary mode
Message-Id: <pan.2003.12.23.20.26.05.840992@nowhere.com>
Hi P,
On Tue, 23 Dec 2003 19:18:01 +0000, pkent wrote:
> If you want the perl-program-that-was-generated to produce a CR on _its_
> STDOUT you need:
>
> #!/usr/local/bin/perl -w
> use strict;
> binmode(STDOUT);
> print 'binmode(STDOUT);print "\r\n";';
>
> and if I do:
> perl x.pl | perl - | hexdump
>
> I get:
>
> 00000000 : 0D 0A
>
> which is what you want, I think.
Thank you for your suggestion but I need a single-quoted string ('\r\n').
Indeed, the program I am writing is more complicate than the toy example I gave.
It does sth like that:
print "binmode(STDOUT);print \'",$buff,"\';"
And $buff might contain non backslashed double quotes or other special
characters that would be interpreted in a double-quote string.
David.
------------------------------
Date: Tue, 23 Dec 2003 20:48:41 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: \r disappear even in binary mode
Message-Id: <Xns945AA0D661EBFdkwwashere@216.168.3.30>
noone <noone@nowhere.com> wrote:
> Thank you for your suggestion but I need a single-quoted string
> ('\r\n'). Indeed, the program I am writing is more complicate than the
> toy example I gave. It does sth like that:
>
> print "binmode(STDOUT);print \'",$buff,"\';"
>
> And $buff might contain non backslashed double quotes or other special
> characters that would be interpreted in a double-quote string.
I don't know what you're trying to do.... You might look in perlop under
quote and quote-like operators, and possibly at the quotemeta() function.
For example:
my $buff = '\r\n';
print qq{binmode(STDOUT); print '$buff';};
--
David Wall
------------------------------
Date: Tue, 23 Dec 2003 22:22:54 +0100
From: noone <noone@nowhere.com>
Subject: Re: \r disappear even in binary mode
Message-Id: <pan.2003.12.23.21.22.53.673748@nowhere.com>
On Tue, 23 Dec 2003 20:48:41 +0000, David K. Wall wrote:
> I don't know what you're trying to do.... You might look in perlop under
> quote and quote-like operators, and possibly at the quotemeta() function.
Thank you but I cannot find any answer there. It seems it is a @$#%!^&
feature of perl: literal carriage returns are ignored if they occur paired
with linefeeds, or get interpreted as whitespace if they stand alone!
David.
------------------------------
Date: Tue, 23 Dec 2003 22:10:28 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: \r disappear even in binary mode
Message-Id: <x7smjbkwx7.fsf@mail.sysarch.com>
>>>>> "n" == noone <noone@nowhere.com> writes:
> On Tue, 23 Dec 2003 20:48:41 +0000, David K. Wall wrote:
>> I don't know what you're trying to do.... You might look in perlop under
>> quote and quote-like operators, and possibly at the quotemeta() function.
> Thank you but I cannot find any answer there. It seems it is a @$#%!^&
> feature of perl: literal carriage returns are ignored if they occur paired
> with linefeeds, or get interpreted as whitespace if they stand alone!
why don't you explain your real problem instead of the solution you
want. i smell an xy problem here. you are struggling too hard with the
only solution you think you have but we don't know what the underlying
goal really is.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Tue, 23 Dec 2003 13:06:25 -0600
From: "S.R.Sriram" <srsriram@sriram.com>
Subject: Re: help with file test
Message-Id: <3FE89231.DD43668D@sriram.com>
Matt Garrish wrote:
> =
> "Ragnar Hafsta=F0" <gnari@simnet.is> wrote in message
> news:bs84cl$v40$1@news.simnet.is...
> > "Matt Garrish" <matthew.garrish@sympatico.ca> wrote in message
> > news:98MFb.4677$d%1.1045837@news20.bellglobal.com...
> > >
> > > Perl does not interpolate variables when you use the file test
> operators,
> > so
> > > there's no need to be escaping any characters.
> >
> > can you explain what you mean by that. surely when executing:
> > $x=3D"foo";
> > print "$x is a file\n" if -f $x;
> > the $x is iterpolated
> >
> =
> As noted in my followup, it wasn't the best sentence I've ever written.=
My
> point was that there isn't any sort of recursive interpolation performe=
d on
> the value held by a variable. In your example, you could have just as e=
asily
> written $x =3D '$x' and then used $x to check for a directory called '$=
x'.
> Otherwise you would get stuck in an endless loop as the value of $x cou=
ld
> never be resolved.
> =
> Matt
Yes the name for the directory $BASEDIR is odd but well it exists.
Here is the directory structure that exists:
w-srsriram>> pwd
/home/srsriram/play/$BASEDIR
w-srsriram>> ls -l
total 0
-rw-rw-r-- 1 srsriram umtscc 11 Dec 23 12:36 foo
-rw-rw-r-- 1 srsriram umtscc 11 Dec 23 12:36 foo1
-rw-rw-r-- 1 srsriram umtscc 11 Dec 23 12:37 foo2
Here is my script:
##############start of Code##################
#!/opt/exp/bin/perl -w
#use strict;
my $myPath =3D '/home/srsriram/play/$BASEDIR';
$myPath =3D~ s/\$/\\\$/g;
chomp(my @myFiles =3D qx(ls $myPath 2>&1));
#print Dumper(@myFiles);
foreach my $myFile (@myFiles) {
my $fileName =3D "$myPath/$myFile";
print "*** $fileName\n";
if (-e $fileName) {
print " if case ";
print "*** $fileName\n";
}else {
print " else case ";
print "*** $fileName\n";
}
}
print "*** End ***\n";
##############End of Code##################
Here is the output:
w-srsriram>> /home/srsriram/play/testprog
*** /home/srsriram/play/\$BASEDIR/foo
else case *** /home/srsriram/play/\$BASEDIR/foo
*** /home/srsriram/play/\$BASEDIR/foo1
else case *** /home/srsriram/play/\$BASEDIR/foo1
*** /home/srsriram/play/\$BASEDIR/foo2
else case *** /home/srsriram/play/\$BASEDIR/foo2
*** End ***
I now changed the $myPath variable in testcode as below:
my $myPath =3D '/home/srsriram/play/base';
w-srsriram>> pwd
/home/srsriram/play/base
w-srsriram>> ls -l
total 0
-rw-rw-r-- 1 srsriram umtscc 11 Dec 23 12:36 bar
-rw-rw-r-- 1 srsriram umtscc 11 Dec 23 12:36 bar1
-rw-rw-r-- 1 srsriram umtscc 11 Dec 23 12:36 bar2
Output of testprog
w-srsriram>> /home/srsriram/playpen/memperl/t24
*** /home/srsriram/play/base/bar
if case *** /home/srsriram/play/base/bar
*** /home/srsriram/play/base/bar1
if case *** /home/srsriram/play/base/bar1
*** /home/srsriram/play/base/bar2
if case *** /home/srsriram/play/base/bar2
*** End ***
Would still appreciate feedback,
Thanks
Sriram
------------------------------
Date: Tue, 23 Dec 2003 20:20:17 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: help with file test
Message-Id: <bsa50t$b00jj$1@ID-184292.news.uni-berlin.de>
S.R.Sriram wrote:
>
> $myPath =~ s/\$/\\\$/g;
Just remove that line!
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Tue, 23 Dec 2003 15:38:32 -0600
From: "S.R.Sriram" <srsriram@sriram.com>
Subject: Re: help with file test
Message-Id: <3FE8B5D8.8CA2F8D6@sriram.com>
After commenting out the line below:
> >
> > $myPath =~ s/\$/\\\$/g;
>
> Just remove that line!
>
as suggested, here is the output for the various cases:
Case I:
-------
my $myPath = '/home/srsriram/play/$BASEDIR';
Output is:
----------
w-srsriram>> /home/srsriram/play/testprog
*** /home/srsriram/play/$BASEDIR/$BASEDIR
else case *** /home/srsriram/play/$BASEDIR/$BASEDIR
*** /home/srsriram/play/$BASEDIR/base
else case *** /home/srsriram/play/$BASEDIR/base
Case II:
--------
my $myPath = '/home/srsriram/play/\$BASEDIR';
Output is:
----------
w-srsriram>> /home/srsriram/play/testprog
*** /home/srsriram/play/\$BASEDIR/foo
else case *** /home/srsriram/play/\$BASEDIR/foo
*** /home/srsriram/play/\$BASEDIR/foo1
else case *** /home/srsriram/play/\$BASEDIR/foo1
*** /home/srsriram/play/\$BASEDIR/foo2
else case *** /home/srsriram/play/\$BASEDIR/foo2
Case III:
---------
my $myPath = '/home/srsriram/play/base';
Output is:
----------
w-srsriram>> /home/srsriram/play/testprog
*** /home/srsriram/play/base/bar
if case *** /home/srsriram/play/base/bar
*** /home/srsriram/play/base/bar1
if case *** /home/srsriram/play/base/bar1
*** /home/srsriram/play/base/bar2
if case *** /home/srsriram/play/base/bar2
Commenting out the line doesn't appear to help.
TIA,
Sriram
------------------------------
Date: Tue, 23 Dec 2003 23:22:09 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: help with file test
Message-Id: <bsafof$b7go6$1@ID-184292.news.uni-berlin.de>
S.R.Sriram wrote:
> After commenting out the line below:
>
>>>$myPath =~ s/\$/\\\$/g;
>>
>>Just remove that line!
>
> as suggested, here is the output for the various cases:
>
> Case I:
> -------
> my $myPath = '/home/srsriram/play/$BASEDIR';
>
> Output is:
> ----------
> w-srsriram>> /home/srsriram/play/testprog
>
> *** /home/srsriram/play/$BASEDIR/$BASEDIR
> else case *** /home/srsriram/play/$BASEDIR/$BASEDIR
> *** /home/srsriram/play/$BASEDIR/base
> else case *** /home/srsriram/play/$BASEDIR/base
<snip>
> Commenting out the line doesn't appear to help.
Well, I have to say that what you are trying to do appears to be
unnecessarily complicated.
Anyway, your script includes this line:
chomp(my @myFiles = qx(ls $myPath 2>&1));
Since you are grabbing the file names using a Unix command, you
probably need to escape $BASEDIR in that context, but only in that
context. When you do the file test in Perl a couple of lines further
down, the $BASEDIR part of the $myPath string should not be escaped.
One solution might be to use another variable:
(my $myPath_escaped = $myPath) =~ s/\$/\\\$/g;
chomp( my @myFiles = qx( ls $myPath_escaped 2>&1 ) );
But you'd better make it a habit to use Perl-ish solutions when such
solutions are easily available, which is the case here: I recommend
that you grab the file names using Perl's opendir() and readdir()
functions instead:
opendir DIR, $myPath or die $!;
my @myFiles = grep { !/^\./ } readdir DIR;
closedir DIR;
HTH
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Tue, 23 Dec 2003 17:22:48 -0500
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: help with file test
Message-Id: <Oe3Gb.7490$d%1.1641826@news20.bellglobal.com>
"S.R.Sriram" <srsriram@sriram.com> wrote in message
news:3FE8B5D8.8CA2F8D6@sriram.com...
>
> Case I:
> -------
> my $myPath = '/home/srsriram/play/$BASEDIR';
>
> Output is:
> ----------
> w-srsriram>> /home/srsriram/play/testprog
>
> *** /home/srsriram/play/$BASEDIR/$BASEDIR
> else case *** /home/srsriram/play/$BASEDIR/$BASEDIR
> *** /home/srsriram/play/$BASEDIR/base
> else case *** /home/srsriram/play/$BASEDIR/base
>
Are you sure you're reading the right directory? By the directory listing
you posted earlier, neither files nor directories named '$BASEDIR' or 'base'
are located in the $BASEDIR directory. It would appear that you are reading
the directory one level up and appending the subdirectories to the path to
$BASEDIR. Looks like it might be a quoting problem with your shell. Why not
just use Perl's readdir function instead and save yourself the headache:
my $myPath = '/home/srsriram/play/$BASEDIR/';
opendir(BASEDIR, $myPath) or die "Could not open basedir: $!\n";
my @files = grep(/^[^.]/, readdir(BASEDIR));
closedir(BASEDIR);
foreach my $file (@files) {
my $filePath = $myPath . $file;
if (-e $file) {
print "File $filePath exists!\n";
}
else {
print "File $filePath does not exist!\n";
}
}
------------------------------
Date: Tue, 23 Dec 2003 22:38:18 -0000
From: "Ragnar Hafstaš" <gnari@simnet.is>
Subject: Re: help with file test
Message-Id: <bsag5e$3l0$1@news.simnet.is>
My news propagation is so slow that my followups allways seem
to be posted after every question has been answered lots of times,
but just in case...
"S.R.Sriram" <srsriram@sriram.com> wrote in message
news:3FE89231.DD43668D@sriram.com...
> my $myPath = '/home/srsriram/play/$BASEDIR';
> $myPath =~ s/\$/\\\$/g;
> chomp(my @myFiles = qx(ls $myPath 2>&1));
here so far so good: you are quoting the $ for the qx (shell)
> my $fileName = "$myPath/$myFile";
> if (-e $fileName) {
but -e does not need/want the quoting here
so you might want to use another variable for the quoted version
($myQuoted=$myPath) =~ s/\$/\\\$/g;
chomp(my @myFiles = qx(ls $myQuoted 2>&1));
on the other hand, maybe you should drop the qx(ls) and go
for opendir. this way you avoid having to deal with any
idiosyncracies of ls and the shell whan dealing with weird characters
(and only need to deal with Perl's)
gnari
------------------------------
Date: 23 Dec 2003 11:27:14 -0800
From: reflous@hotmail.com (Jon)
Subject: How to execute javascript from within perl?
Message-Id: <497ffc4b.0312231127.71d35cb4@posting.google.com>
I've written a perl script which parses a variety of webmail sites.
Unfortunately most of the work is hardcoded and picked out in regex's
because I can't seem to figure out a good way to either parse or
execute JavaScript within perl.
There is a JavaScript.pm on CPAN which I tried to use but had to code
all the DHTML classes into it using the bind_class function and began
to run into problems with things like: parent.document.frames[1].href
= 'http://www.google.com'
Right now I'm thinking about creating my own module and trying to tie
in some of mozilla's c functions/modules to execute the javascript I'm
parsing out of webpages.
Anyone know of a project that already does this or can think of a good
way to solve this problem? Thanks for any help...
Jon
------------------------------
Date: 23 Dec 2003 12:43:54 -0800
From: kent_zunker@bmc.com (Woogie)
Subject: LWP with proxy problem
Message-Id: <4a31f52f.0312231243.25828446@posting.google.com>
When running the sample code below without a proxy the GET returns the
expected data. When run with the $proxy uncommented the GET returns
the content of the login page for the site being accessed. The site
in the code is valid for ease of testing. I also am including the LWP
debug info for each attempt.
Can anyone explain this behavior and what can I do to correct it?
Thanks in advance
Trace without proxy:
LWP::UserAgent::new: ()
LWP::UserAgent::request: ()
LWP::UserAgent::send_request: GET
https://squid.servebeer.com/getservices.do?user=Guest&password=JustLooking&format=csv
LWP::UserAgent::_need_proxy: Not proxied
LWP::Protocol::http::request: ()
LWP::Protocol::collect: read 28 bytes
LWP::UserAgent::request: Simple response: OK
Home
PE
Trace with proxy:
LWP::UserAgent::new: ()
LWP::UserAgent::proxy: https http://148.245.207.85:8080
LWP::UserAgent::request: ()
LWP::UserAgent::send_request: GET
https://squid.servebeer.com/getservices.do?user=Guest&password=JustLooking&format=csv
LWP::UserAgent::_need_proxy: Proxied to http://148.245.207.85:8080
LWP::Protocol::http::request: ()
LWP::Protocol::collect: read 236 bytes
LWP::Protocol::collect: read 594 bytes
LWP::Protocol::collect: read 416 bytes
LWP::Protocol::collect: read 450 bytes
LWP::Protocol::collect: read 1017 bytes
LWP::Protocol::collect: read 443 bytes
LWP::Protocol::collect: read 643 bytes
LWP::UserAgent::request: Simple response: OK
<html lang="en">
<!-- Start Head -->
<head>
<title>
Error
</title>
<script language="JavaScript">
...
Here is the sample code:
#!/usr/bin/perl -w
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
use Crypt::SSLeay;
LWP::Debug::level('+');
$url = "https://squid.servebeer.com/getservices.do?user=Guest&password=JustLooking&format=csv";
#$proxy="http://xxx.xxx.xxx.xxx:8080";
$ua = LWP::UserAgent->new();
if (defined $proxy)
{
$ENV{HTTPS_PROXY} = $proxy;
# initialize from environment variables
$ua->env_proxy;
}
$req = HTTP::Request->new(GET => $url);
$response = $ua->request($req);
if ($response->is_error())
{
printf " %s\n", $response->status_line;
}
else
{
$content = $response->content();
print $content;
}
exit;
------------------------------
Date: Tue, 23 Dec 2003 22:40:47 GMT
From: Dan <webmaster@heavyharmonies.com>
Subject: LWP::Simple and Cookies not working
Message-Id: <Pv3Gb.120414$8y1.373415@attbi_s52>
I have a script that does a search of completed Ebay auctions for
certain items. All was fine until a few weeks ago, when Ebay changed it
so that one must login in order to search completed items. I've modified
the script to support cookies for the login information after searching
on deja. The script seems fairly simple, but the get() statement returns
a page that claims that the browser does not support cookies. I can't
see what I'm missing here :(
Code follows. You'll need a valid Ebay USerID and password to test it.
Thanks in advance.
-Dan
# !/user/bin/perl
use LWP::Simple qw ($ua get);
use HTTP::Cookies;
$ua->cookie_jar(HTTP::Cookies->new);
# Modify the following as needed.
$Login = "login";
$Password = "password";
$URL="http://signin.ebay.com/aw-cgi/eBayISAPI.dll?MfcISAPICommand=SignInWelcome&siteid=0&co_partnerId=2&UsingSSL=0&pp=&pa1=&pa2=&pa3=&il=-1&pageType=1637&userid="
. $Login . "&pass=" . $Password .
"&keepMeSignInOption=1&ru=http://search-completed.ebay.com:80/search/search.dll?GetResult&catref=C3&category1=1049&sr=0&query=craaft&lotr=1&completedonly=1&from=R2";
$content = get $URL;
open (TMPOUT, ">ebaytest.out");
print TMPOUT $content;
close TMPOUT;
------------------------------
Date: Tue, 23 Dec 2003 15:36:22 -0500
From: "RB" <wi@rjsolutions.org>
Subject: monitor windows processes
Message-Id: <bsa908$2o0g$1@msunews.cl.msu.edu>
Does anyone know how to monitor the existance of a process running in
windows task manager.
Basically I want to start another process only if the previous has finished
and the script runs in windows.
Thanks
------------------------------
Date: Tue, 23 Dec 2003 15:30:29 -0600
From: Sharif Islam <mislam@spamless.uiuc.edu>
Subject: Multiple SELECT queries
Message-Id: <bsac5m$jr1$1@news.ks.uiuc.edu>
Is this is an efficient way to do multiple select queries? It does what
I need to accomplish. I am just curious if there are any better way to
do this sort of queries.
_______BEGIN CODE_______
#!/usr/bin/perl
use strict;
use DBI ;
my ($dbh, $sth, $row);
my $Count = 0;
my $SQL_server = "server";
my $SQL_username = "user" ;
my $SQL_password = "passwd" ;
my $dbh = DBI->connect("dbi:Sybase:server=$SQL_server",
$SQL_username,
$SQL_password,
{PrintError => 1});
die "Unable for connect to server $DBI::errstr" unless $dbh;
$dbh->do("use Resources"); # Set the database to use.
my @Query = ("SELECT COUNT(*) AS CT from table where URL is NULL",
"SELECT COUNT(*) AS Cov from table where coverage is NULL","SELECT
COUNT(*) AS Agg from table where ID is NULL") ;
#query 1
$sth = $dbh->prepare($Query[$Count]);
$sth->execute();
$row = $sth->fetchrow_arrayref;
$sth->finish() ;
#query 2
$sth = $dbh->prepare($Query[$Count+1]);
$sth->execute();
$row1 = $sth->fetchrow_arrayref;
$sth->finish;
#query 3
$sth = $dbh->prepare($Query[$Count+2]);
$sth->execute();
$row2 = $sth->fetchrow_arrayref;
print "NULL Record Count\n---------------------\n";
print "url\t $$row[0]\n";
print "coverage\t $$row1[0]\n";
print "id \t $$row2[0]\n";
$sth->finish() ;
$dbh->disconnect ;
______END CODE__________
--
)
((
|""|-.
| :|/'
-`--'-
------------------------------
Date: Wed, 24 Dec 2003 08:19:03 +1000
From: Gregory Toomey <nospam@bigpond.com>
Subject: Re: Multiple SELECT queries
Message-Id: <1234857.qLv1sVo2m1@gregs-web-hosting-and-pickle-farming>
It was a dark and stormy night, and Sharif Islam managed to scribble:
> Is this is an efficient way to do multiple select queries? It does what
> I need to accomplish. I am just curious if there are any better way to
> do this sort of queries.
>
>
> _______BEGIN CODE_______
> #!/usr/bin/perl
> use strict;
> use DBI ;
>
> my ($dbh, $sth, $row);
> my $Count = 0;
>
> my $SQL_server = "server";
> my $SQL_username = "user" ;
> my $SQL_password = "passwd" ;
>
> my $dbh = DBI->connect("dbi:Sybase:server=$SQL_server",
> $SQL_username,
> $SQL_password,
> {PrintError => 1});
>
> die "Unable for connect to server $DBI::errstr" unless $dbh;
>
> $dbh->do("use Resources"); # Set the database to use.
>
> my @Query = ("SELECT COUNT(*) AS CT from table where URL is NULL",
> "SELECT COUNT(*) AS Cov from table where coverage is NULL","SELECT
> COUNT(*) AS Agg from table where ID is NULL") ;
>
> #query 1
> $sth = $dbh->prepare($Query[$Count]);
> $sth->execute();
> $row = $sth->fetchrow_arrayref;
> $sth->finish() ;
> #query 2
> $sth = $dbh->prepare($Query[$Count+1]);
> $sth->execute();
> $row1 = $sth->fetchrow_arrayref;
> $sth->finish;
> #query 3
> $sth = $dbh->prepare($Query[$Count+2]);
> $sth->execute();
> $row2 = $sth->fetchrow_arrayref;
>
> print "NULL Record Count\n---------------------\n";
> print "url\t $$row[0]\n";
> print "coverage\t $$row1[0]\n";
> print "id \t $$row2[0]\n";
>
> $sth->finish() ;
> $dbh->disconnect ;
> ______END CODE__________
>
You could use a union statement:
select count(*) kount from table1 where ...
union
select count(*) kount from table2 where ...
or better still you could use derived tables if you SQL supports it
select kount1, kount2
from (select kount(*) kount1 from table1 where ...), (select count(*) kount2 from table2 where ...)
A bigger issue is 'count(*)' and 'where field is null' will genertall do full table scans, which you don't want to do.
And of course this has nothing to do with Perl.
gtoomey
------------------------------
Date: Tue, 23 Dec 2003 14:36:45 -0800
From: "Paul Hovnanian P.E." <Paul@Hovnanian.com>
Subject: Re: Multiple SELECT queries
Message-Id: <3FE8C37D.42368B43@Hovnanian.com>
Sharif Islam wrote:
>
> Is this is an efficient way to do multiple select queries? It does what
> I need to accomplish. I am just curious if there are any better way to
> do this sort of queries.
>
> _______BEGIN CODE_______
> #!/usr/bin/perl
> use strict;
> use DBI ;
>
> my ($dbh, $sth, $row);
> my $Count = 0;
>
> my $SQL_server = "server";
> my $SQL_username = "user" ;
> my $SQL_password = "passwd" ;
>
> my $dbh = DBI->connect("dbi:Sybase:server=$SQL_server",
> $SQL_username,
> $SQL_password,
> {PrintError => 1});
>
> die "Unable for connect to server $DBI::errstr" unless $dbh;
>
> $dbh->do("use Resources"); # Set the database to use.
>
> my @Query = ("SELECT COUNT(*) AS CT from table where URL is NULL",
> "SELECT COUNT(*) AS Cov from table where coverage is NULL","SELECT
> COUNT(*) AS Agg from table where ID is NULL") ;
>
> #query 1
> $sth = $dbh->prepare($Query[$Count]);
> $sth->execute();
> $row = $sth->fetchrow_arrayref;
> $sth->finish() ;
> #query 2
> $sth = $dbh->prepare($Query[$Count+1]);
> $sth->execute();
> $row1 = $sth->fetchrow_arrayref;
> $sth->finish;
> #query 3
> $sth = $dbh->prepare($Query[$Count+2]);
> $sth->execute();
> $row2 = $sth->fetchrow_arrayref;
>
> print "NULL Record Count\n---------------------\n";
> print "url\t $$row[0]\n";
> print "coverage\t $$row1[0]\n";
> print "id \t $$row2[0]\n";
>
> $sth->finish() ;
> $dbh->disconnect ;
> ______END CODE__________
Its hard to read and in the grand scheme of things, the @Query array
doesn't buy you anything.
The ->prepare method is still working with one string at a time. But its
all just a matter of style.
I'd put the ->prepare(), ->execute() and ->fetchrow_arrayref into a
little subroutine.
--
Paul Hovnanian mailto:Paul@Hovnanian.com
note to spammers: a Washington State resident
------------------------------------------------------------------
"If Bill gates had a dime for every windows machine that crashed...
Wait a minute, he does!"
------------------------------
Date: Wed, 24 Dec 2003 08:09:52 +1300
From: "Tintin" <me@privacy.net>
Subject: Re: newbie: Terminating while($line=<stdin>) on Windows
Message-Id: <bsa3vo$b2pi7$1@ID-172104.news.uni-berlin.de>
"Madhur" <a_madhur@vsnl.net> wrote in message
news:bsa11g$b8aej$1@ID-81175.news.uni-berlin.de...
> Hello
> while($line==<stdin>)
> {
>
> }
> How would this be terminated. It is terminated by ^D on Unix system I
> think.
> But on windows It doesnt work.
Unix is not Windows, Windows is not Unix, and neither Windows nor Unix are
Perl.
^Z
------------------------------
Date: Tue, 23 Dec 2003 17:33:01 -0500
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: newbie: Terminating while($line=<stdin>) on Windows
Message-Id: <mo3Gb.7504$d%1.1647093@news20.bellglobal.com>
"Madhur" <a_madhur@vsnl.net> wrote in message
news:bsa11g$b8aej$1@ID-81175.news.uni-berlin.de...
> Hello
> while($line==<stdin>)
> {
>
> }
> How would this be terminated. It is terminated by ^D on Unix system I
> think.
> But on windows It doesnt work.
>
Ctrl+C, Ctrl+Break, that little x in the corner of your dos window, via the
task manager, tmtowtdi...
Did you have a Perl question, though?
Matt
------------------------------
Date: Tue, 23 Dec 2003 14:37:57 -0700
From: James Szinger <szinger@lanl.gov>
Subject: Re: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.4 $)
Message-Id: <giu13rnrka.fsf@cytotoxic.lanl.gov>
"Alan J. Flavell" <flavell@ph.gla.ac.uk> writes:
>
> http://www.google.com/search?q=jargon+tofu
> Use the web, Luke...
Acronyms which are not generally known should be spelled out the first
time they appear. Since "TOFU" is jargon and the guidelines are meant
to help, I'm in favor of spelling it out as a courtesy to the reader.
I find the present wording unclear.
--
James J. Szinger Los Alamos National Laboratory
szinger@lanl.gov Theoretical Biology & Biophysics (T-10)
------------------------------
Date: 23 Dec 2003 21:48:01 GMT
From: ctcgag@hotmail.com
Subject: Re: Return all points with x km of y
Message-Id: <20031223164801.799$B7@newsreader.com>
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
> <ctcgag@hotmail.com> wrote in comp.lang.perl.misc:
> >
> > I'd start by doing something like:
> >
> > my @data;
> > while (my($key,$dist)=next_point()) {
> > push @data, [$key,$dist];
> > };
> > @data = (sort {$a->[1]<=>$b->[1]})[0..$x-1];
>
> Hmm... Sorting is not an efficient way to find the top n of a population
> if n is considerably smaller than the population. Sorting is slow and
> (usually) needs all the data in memory. Both can be avoided using a
> heap.
Sorting is actually quite fast, and doesn't need all the data in memory
if you make the simple modification I gave for doing intermediate sorts.
(Now that I've implemented and tested it, I'd recommed 3 or 4 rather than 8
as a good multiplier for the itermediate sorts. If you are space paranoid,
even 1.5 gives good perfomance.)
It is only "not an efficient way" in the same sense that Perl is not
an efficient way to program much of anything. Now if heaps were a native
Perl data structure the way hashes are, I would have definitely gone with
that over sorts.
> For this particular problem, we'd want a maximum heap, that is, one that
> allows the extraction of the largest element at all times. Assuming we
> have a Heap class with methods new, add, size and extract_maximum, this
> finds the $x closest points:
>
> my $heap = Heap->new( sub { $a->[1] <=> $b->[1] });
> while ( my( $key, $dist) = next_point() ) {
> $heap->add( [ $key, $dist]);
> # discard the most distant point if no longer needed
> $heap->extract_maximum if $heap->size > $x;
> }
> my @data = reverse map $heap->extract_maximum, 1 .. $heap->size;
>
> If $x is small compared to the overall number n of locations, this runs
> essentially in time proportional to n and space proportional to $x. If
> $x approaches the total, it seamlessly mutates into a heap sort.
The multiple-sorting method has the same properties, only the constants of
proportionality and the overheads are different. And I think they favor
the sorting method. In the limiting case, one method becomes a single
huge sort implemented in C with all data in memory, while the other becomes
a single huge sort implemented in Perl (OO-Perl, at that) with all data in
memory.
> There are two or three heap modules on CPAN, though none of them may
> have the interface I assumed.
Another advantage of using sort. It has the interface I assumed :), and I
didn't have to install (I couldn't even get Heap::Simple to install with
CPAN) and learn a new module.
(Not that learning a new module is a bad thing, but when it decreases my
programming speed by a factor of 2 or so, decreases the program speed by a
factor of 4 or so, descreases portability by requiring yet another non-core
module to be installed, and makes the program longer to boot, it isn't such
a good thing, except perhaps as an intellectual challenge.)
> > And if that took too much memory I'd do:
>
> [snip]
>
> The heap method does a better job of keeping intermediate storage small.
Not necessarily. The memory overhead of the heap data structure, as
implemented in Heap::Binomial for example, is substantial. Unless the key
size is quite large (>0.5kb, in which case they should probably be
decomposed into a true key and a lazy-loaded data portion), or x is quite
large, the multiple sorting method is likely to be both faster (3 to 6
times, in the tests I did) and smaller.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service New Rate! $9.95/Month 50GB
------------------------------
Date: Tue, 23 Dec 2003 20:50:15 +0000
From: Big and Blue <No_4@dsl.pipex.com>
Subject: Re: Running a New Process
Message-Id: <3fe8aa71$0$9391$cc9e4d1f@news-text.dial.pipex.com>
Fabio wrote:
>
> This VPNClient behaves a little bit strange. If
> you run it from the prompt is starts and prints some text on the screen.
> But after printing the messages it doesn't return to the prompt.
What is strange about that? If you run perl (awk, sed, etc...) then
you don't get back to a prompt until they finish, unless the program
backgrounds itself. vpnclinet does not. It sits there (allowing you, if
you can't think of anythign better, to ctl-C (or rather SIGINT) to stop the
program and let it release control of the ethernet interface.
> It's
> possible to use it from another console window but I don't want to do
> that. I want to run it from the Perl script and allow the other commands
> to be executed after the VPN is initialized.
vpnclient doesn't just initialize - it keeps it running.
So you need to run it in another process....
--
-*- Just because I've written it here doesn't -*-
-*- mean that you should, or I do, believe it. -*-
------------------------------
Date: Tue, 23 Dec 2003 17:43:25 -0500
From: Ying Hu <yhu@mail.nih.gov>
Subject: search interval
Message-Id: <3FE8C50D.BE70D081@mail.nih.gov>
Hi,
There are two data sets,
data set 1:
A1 123 125
A2 129 200
A3 400 420
...
...
data set 2:
B1 126
B2 130
B3 202
...
...
My question is how to get B2 and A2 { 130 (B2) is in [from]129 [to]200
(A2)} .
thanks
Ying
------------------------------
Date: Wed, 24 Dec 2003 08:08:22 +1300
From: "Tintin" <me@privacy.net>
Subject: Re: Simple telnet-like access
Message-Id: <bsa3su$b5tr8$1@ID-172104.news.uni-berlin.de>
"John Deuf" <google@spongers.com> wrote in message
news:9760255.0312230955.5e6d4fd6@posting.google.com...
> Hi,
> I'd like to use a module in Perl to connect to a remote server through
> a Telnet like connection.
>
> Something allowing to
> - wait for "login" word, then send the "login" name
> - then wait for "password" then entering my password
> - then doing a few I/O with the server (commands / answers)
>
> I never used a tcp-ip module in Perl
>
> - what is the best module to use
> - how to simply implement the telnet connectivity
Either use Net::Telnet or one of the Expect modules.
------------------------------
Date: Tue, 23 Dec 2003 19:21:37 +0000
From: pkent <pkent77tea@yahoo.com.tea>
Subject: Re: Simple telnet-like access
Message-Id: <pkent77tea-D8C66B.19213723122003@ptb-nnrpp01.plus.net>
In article <9760255.0312230955.5e6d4fd6@posting.google.com>,
google@spongers.com (John Deuf) wrote:
> I'd like to use a module in Perl to connect to a remote server through
> a Telnet like connection.
>
> Something allowing to
> - wait for "login" word, then send the "login" name
> - then wait for "password" then entering my password
> - then doing a few I/O with the server (commands / answers)
>
> I never used a tcp-ip module in Perl
>
> - what is the best module to use
I'd use Net::Telnet - the first example does exactly what you've said
you want to do:
http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm
P
--
pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
Remove the tea to reply
------------------------------
Date: 23 Dec 2003 13:50:55 -0800
From: macapone@yahoo.com (Michael Capone)
Subject: Time out SSL request?
Message-Id: <96996c7b.0312231350.4b960612@posting.google.com>
Hi folks,
I'm using perl (actually, mod_perl environment on RH 8.0) to connect
to an external SSL server. I feed it an XML request and get a
response back. The problem is, the server is flaky, and I'd like to
be able to timeout / break if I haven't gotten a response in X
seconds. (Note that the server may connect, but simply not send a
response, i.e., it's too busy.)
I don't really understand filehandles and setting timeouts in unix /
perl as much as I should. The code below was lifted from the
Net::SSLeay readme and worked, and I never questioned it further.
Could someone show me how to modify the code below to add my timeout?
Note that I'm also completely open to other methods of connecting to
an SSL server; the Net::SSLeay method seemed to be the quickest to
implement, and I have no idea if there's a better way out there.
Thanks!
Michael
#!/usr/local/bin/perl -w
use strict;
use Net::SSLeay::Handle qw/shutdown/;
my ($request, $response) ;
local *F;
my $FH = *F;
$request = <<ENDXML;
<?xml version="1.0"?>
<Request>
<UserId>user1</UserId>
<Password>pass1</Password>
</Request>
ENDXML
my ($host, $port) = ("www.some.server", 443);
tie(*SSL, "Net::SSLeay::Handle", $host, $port);
print SSL "POST /xmlapps/blah HTTP/1.0\n";
print SSL "Content-type: application/x-www-form-urlencoded\n";
print SSL "Content-length: " . length($request) . "\n\n";
print SSL $request;
shutdown(\*SSL, 1);
$response .= $_ while (<SSL>);
close SSL;
print $response;
------------------------------
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 5975
***************************************