[29744] in Perl-Users-Digest
Perl-Users Digest, Issue: 988 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 29 21:09:43 2007
Date: Mon, 29 Oct 2007 18:09:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Mon, 29 Oct 2007 Volume: 11 Number: 988
Today's topics:
emacs lisp as text processing language... <xah@xahlee.org>
Re: emacs lisp as text processing language... <xah@xahlee.org>
Re: From "The Camel Book", Ch. 16 (IPC) <ced@blv-sam-01.ca.boeing.com>
Re: how to close a stalled file descriptor? <ben@morrow.me.uk>
Re: packing a C structure <jl_post@hotmail.com>
Re: packing a C structure <bgeer@xmission.com>
Problem getting command output <kingskippus@gmail.com>
Problem getting command output <kingskippus@gmail.com>
Re: Problem getting command output <krahnj@telus.net>
Re: problems building perl modules, path question <ben@morrow.me.uk>
Query returns -1 if row present (DBI, SQL Server 2000) <xcdx80@gmail.com>
Re: Query returns -1 if row present (DBI, SQL Server 20 <glex_no-spam@qwest-spam-no.invalid>
Re: Query returns -1 if row present (DBI, SQL Server 20 <xcdx80@gmail.com>
Re: reading a directory, first files the newest ones <hjp-usenet2@hjp.at>
Regex Help <inderpaul_s@yahoo.com>
Re: Regex Help <smallpond@juno.com>
Re: Regex Help <peter@makholm.net>
Re: Regex Help <kingskippus@gmail.com>
Re: Regex Help <rvtol+news@isolution.nl>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 29 Oct 2007 15:30:24 -0700
From: Xah Lee <xah@xahlee.org>
Subject: emacs lisp as text processing language...
Message-Id: <1193697024.171112.28490@t8g2000prg.googlegroups.com>
Text Processing with Emacs Lisp
Xah Lee, 2007-10-29
This page gives a outline of how to use emacs lisp to do text
processing, using a specific real-world problem as example. If you
don't know elisp, first take a gander at Emacs Lisp Basics.
HTML version with links and colors is at:
http://xahlee.org/emacs/elisp_text_processing.html
Following this post as a separate post, is some relevant (i hope)
remark about Perl and Python.
---------------------------------
THE PROBLEM
----------------
Summary
I want to write a elisp program, that process a list of given files.
Each file is a HTML file. For each file, i want to remove the link to
itself, in its page navigation bar. More specifically, each file have
a page navigation bar in this format:
<div class=3D"pages">Goto Page: <a href=3D"1.html">1</a>, <a
href=3D"2.html">2</a>, <a href=3D"3.html">3</a>, <a href=3D"4.html">3</
a>, ...</div>.
where the file names and link texts are all arbitrary. (not as 1, 2, 3
shown here.) The link to itself needs to be removed.
----------------
Detail
My website has over 3 thousand files; many of the pages is a series.
For example, i have a article on Algorithmic Mathematical Art, which
is broken into 3 HTML pages. So, at the bottom of each page, i have a
page navigation bar with code like this:
<div class=3D"pages">Goto Page: <a href=3D"20040113_cmaci_larcu.html">1</
a>, <a href=3D"cmaci_larcu2.html">2</a>, <a href=3D"cmaci_larcu3.html">3</
a></div>
In a browser, it would look like this:
i/page tag
Note that the link to the page itself really shouldn't be a link.
There are a total of 134 pages scattered about in various directories
that has this page navigation bar. I need some automated way to
process these files and remove the self-link.
I've been programing in perl professionally from 1998 to 2002 full
time. Typically, for this task in perl (or Python), i'd open each
file, read in the file, then use regex to do the replacement, then
write out the file. For replacement that span over several lines, the
regex needs to act on the whole file (as opposed to one line at a
time). The regex can become quite complex or reaching its limit. For a
more robust solution, a XML/HTML parser package can be used to read in
the file into a structured representation, then process that. Using a
HTML parser is a bit involved. Then, as usual, one may need to create
backups of the original files, and also deal with maintaining the
file's meta info such as keeping the same permission bits. In summary,
if the particular text-processing required is not simple, then the
coding gets fairly complex quickly, even if job is trivial in
principle.
With emacs lisp, the task is vastly simplified, because emacs reads in
a file into its buffer representation. With buffers, one can move a
pointer back and forth, search and delete or insert text arbitrarily,
with the entire emacs lisp's suite of functions designed for
processing text, as well the entire emacs environment that
automatically deals with maintaining file. (symbolic links, hard
links, auto-backup system, file meta-info maintaince, file locking,
remote files... etc).
We proceed to write a elisp code to solve this problem.
---------------------------------
SOLUTION
Here's are the steps we need to do for each file:
* open the file in a buffer
* move cursor to the page navigation text.
* move cursor to file name
* run sgml-delete-tag (removes the link)
* save file
* close buffer
We begin by writing a test code to process a single file.
(defun xx ()
"temp. experimental code"
(interactive)
(let (fpath fname mybuffer)
(setq fpath "/Users/xah/test1.html")
(setq fname (file-name-nondirectory fpath))
(setq mybuffer (find-file fpath))
(search-forward "<div class=3D\"pages\">Goto Page:")
(search-forward fname)
(sgml-delete-tag 1)
(save-buffer)
(kill-buffer mybuffer)))
First of all, create files test1.html, test2.html, test3.html in a
temp directory for testing this code. Each file will contain this page
navigation line:
<div class=3D"pages">Goto Page: <a href=3D"test1.html">some1</a>, <a
href=3D"test2.html">another</a>, <a href=3D"test3.html">xyz3</a></div>
Note that in actual files, the page-nav string may not be in a single
line.
The elisp code above is fairly simple and self-explanatory. The file
opening function find-file is found from elisp doc section =E2=80=9CFiles=
=E2=80=9D.
The cursor moving function search-forward is in =E2=80=9CSearching and
Matching=E2=80=9D, the save or close buffer fuctions are in section =E2=80=
=9CBuffer=E2=80=9D.
Reference: Elisp Manual: Files.
Reference: Elisp Manual: Buffers.
Reference: Elisp Manual: Searching-and-Matching.
The interesting part is calling the function sgml-delete-tag. It is a
function loaded by html-mode (which is automatically loaded when a
html file is opened). What sgml-delete-tag does is to delete the tag
that encloses the cursor (both the opening and closing tags will de
deleted). The cursor can be anywhere in the beginning angle bracket of
the opening to the ending angle bracket of the closing tag. This sgml-
delete-tag function helps us tremendously.
Now, with the above code, our job is essentially done. All we need to
do now is to feed it a bunch of file paths. First we clean the code up
by writing it to take a path as argument.
(defun my-modfile-page-tag (fpath)
"Modify the HTML file at fpath."
(let (fname mybuffer)
(setq fname (file-name-nondirectory fpath))
(setq mybuffer (find-file fpath))
(search-forward "<div class=3D\"pages\">Goto Page:")
(search-forward fname)
(sgml-delete-tag 1)
(save-buffer)
(kill-buffer mybuffer)))
Then, we test this modified code by evaluating the following code:
(my-modfile-page-tag "/Users/xah/test1.html")
To complete our task, all we have to do now is get the list of files
that contains the page-nav tag and feed them to my-modfile-page-tag.
To generate a list of files, we can simply use unix's =E2=80=9Cfind=E2=80=
=9D and
=E2=80=9Cgrep=E2=80=9D, like this:
find . -name "*\.html" -exec grep -l '<div class=3D"pages">' {} \;
For each line in the output, we just wrap a double quote around it to
make it a lisp string. Possibly also insert the full path by using
string-rectangle, to construct the following code:
(mapcar (lambda (x) (my-modfile-page-tag x))
(list
"/Users/xah/web/3d/viz.html"
"/Users/xah/web/3d/viz2.html"
"/Users/xah/web/dinju/Khajuraho.html"
"/Users/xah/web/dinju/Khajuraho2.html"
"/Users/xah/web/dinju/Khajuraho3.html"
;=2E.. 100+ lines
)
)
The mapcar and lambda is a lisp idiom of looping thru a list. We
evaluate the code and we are all done!
Emacs is beautiful!
(a separate post follows on the relevance of Perl and Python)
Xah
xah@xahlee.org
=E2=88=91 http://xahlee.org/
------------------------------
Date: Mon, 29 Oct 2007 15:33:54 -0700
From: Xah Lee <xah@xahlee.org>
Subject: Re: emacs lisp as text processing language...
Message-Id: <1193697234.759637.20120@q3g2000prf.googlegroups.com>
.=2E. continued from previous post.
PS I'm cross-posting this post to perl and python groups because i
find that it being a little know fact that emacs lisp's power in the
area of text processing, are far beyond Perl (or Python).
.=2E. i worked as a professional perl programer since 1998. I started to
study elisp as a hobby since 2005. (i started to use emacs daily since
1998) It is only today, while i was studying elisp's file and buffer
related functions, that i realized how elisp can be used as a general
text processing language, and in fact is a dedicated language for this
task, with powers quite beyond Perl (or Python, PHP (Ruby, java, c
etc) etc).
This realization surprised me, because it is well-known that Perl is
the de facto language for text processing, and emacs lisp for this is
almost unknown (outside of elisp developers). The surprise was
exasperated by the fact that Emacs Lisp existed before perl by almost
a decade. (Albeit Emacs Lisp is not suitable for writing general
applications.)
My study about lisp as a text processing tool today, remind me of a
article i read in 2000: =E2=80=9CIlya Regularly Expresses=E2=80=9D, of a in=
terview
with Dr Ilya Zakharevich (author of cperl-mode.el and a major
contributor to the Perl language). In the article, he mentioned
something about Perl's lack of text processing primitives that are in
emacs, which i did not fully understand at the time. (i don't know
elisp at the time)
The article is at:
http://www.perl.com/lpt/a/2000/09/ilya.html
Here's the relevant excerpt:
=C2=AB
Let me also mention that classifying the text handling facilities of
Perl as "extremely agile" gives me the willies. Perl's regular
expressions are indeed more convenient than in other languages.
However, the lack of a lot of key text-processing ingredients makes
Perl solutions for many averagely complicated tasks either extremely
slow, or not easier to maintain than solutions in other languages (and
in some cases both).
I wrote a (heuristic-driven) Perlish syntax parser and transformer in
Emacs Lisp, and though Perl as a language is incomparably friendlier
than Lisps, I would not be even able of thinking about rewriting this
tool in Perl: there are just not enough text-handling primitives
hardwired into Perl. I will need to code all these primitives first.
And having these primitives coded in Perl, the solution would turn out
to be (possibly) hundreds times slower than the built-in Emacs
operations.
My current conjecture on why people classify Perl as an agile text-
handler (in addition to obvious traits of false advertisements) is
that most of the problems to handle are more or less trivial ("system
maintenance"-type problems). For such problems Perl indeed shines. But
between having simple solutions for simple problems and having it
possible to solve complicated problems, there is a principle of having
moderately complicated solutions for moderately complicated problems.
There is no reason for Perl to be not capable of satisfying this
requirement, but currently Perl needs improvement in this regard.
=C2=BB
Xah
xah@xahlee.org
=E2=88=91 http://xahlee.org/
------------------------------
Date: Mon, 29 Oct 2007 16:44:46 -0700
From: "comp.llang.perl.moderated" <ced@blv-sam-01.ca.boeing.com>
Subject: Re: From "The Camel Book", Ch. 16 (IPC)
Message-Id: <1193701486.168977.159000@e9g2000prf.googlegroups.com>
On Oct 24, 6:18 pm, xhos...@gmail.com wrote:
> Jim Gibson <jgib...@mail.arc.nasa.gov> wrote:
> > In article <20071024182536.288...@newsreader.com>, <xhos...@gmail.com>
> > wrote:
>
> > > rihad <ri...@mail.ru> wrote:
> > > > Chapter 16 "Interprocess Communication" of "Programming Perl" has
> > > > this part:
>
> > > What edition of the book?
>
> > > > use Fcntl ':flock';
> > > > eval {
> > > > local $SIG{ALRM} = sub { die "alarm clock restart" };
> > > > alarm 10; # schedule alarm in 10 seconds
> > > > eval {
> > > > flock(FH, LOCK_EX) # a blocking, exclusive lock
> > > > or die "can't flock: $!";
> > > > };
> > > > alarm 0; # cancel the alarm
> > > > };
> > > > alarm 0; # race condition protection
> > > > die if $@ && $@ !~ /alarm clock restart/; # reraise
>
> > > This just doesn't make sense to me in the first place. What is the
> > > purpose if the inner eval? The "can't flock: $!" message is useless,
> > > as the inner eval is never tested against failure directly and the
> > > outer eval will stomp on the $@ set by the inner eval. Similarly, the
> > > error raised by $SIG{ALRM} is overwhelmingly likely to occur inside the
> > > inner eval, so the test of $@ outside both evals is not going to detect
> > > the time out, which seems to be its sole purpose.
>
Hm, I have to wonder if the following line or something akin wasn't
omitted after the inner
'alarm 0':
die $@ if $@;
Otherwise, any flock fatality will never be
propagated or seen as you noted.
> > 3rd Edition, p. 417:
>
> > "The nested exception trap is included because calling flock would
> > raise an exception if flock is not implemented on your platform,
>
> Isn't that something you would want to know? Why go to the trouble
> of constructing a decent die string if it will never be seen?
>
> And why test for "alarm clock restart" in such a way that vast majority
> of such restarts won't be detected by the test?
>
> > and
> > you need to make sure to clear the alarm anyway. The second alarm 0 is
> > provided in case the signal comes in after running the flock but before
> > getting to the first alarm 0."
>
> Presumably that means "after exiting the inner eval but before
> getting to the first alarm 0". But I don't see that that makes much
> sense, either. Once the alarm has fired, there is no need to
> turn it off--it is already off. ...
The final die in the quoted Camel snip:
die if $@ && $@ !~ /alarm clock restart/;
suggests the intent was propagate the error as if
therewere another phantom nesting eval level.
Even so, this wouldn't explain the spurious
'alarm 0' at the outer level when the signal's
already fired off.
--
Charles DeRykus
------------------------------
Date: Mon, 29 Oct 2007 23:03:26 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: how to close a stalled file descriptor?
Message-Id: <ue5iv4-nd1.ln1@osiris.mauzo.dyndns.org>
Quoth Glenn <eponymousalias@yahoo.com>:
> I'm having trouble closing a file descriptor on a stalled named pipe.
> To unblock myself if the write takes too long because the pipe is full
> and there is no reader on the other end of the pipe, I put in place
> a signal handler.
Which OS are you using? Under most Unices, a writing to a full pipe with
no readers will first send SIGPIPE, and if that is handled or ignored,
fail with EPIPE.
> But if the signal handler is invoked and I regain
> control, on any subsequent attempt to close the file descriptor,
> it stalls again!
When you say 'file descriptor' do you mean 'Perl file handle'? Which
perl version are you using? Are you using PerlIO? It's possible that you
are closing a Perl FH, which is attempting to flush its buffers and
handling the error badly. If this is the case, then you may be able to
ignore (and lose) the buffer by pushing a :unix layer before you close.
Ben
------------------------------
Date: Mon, 29 Oct 2007 14:40:09 -0700
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: packing a C structure
Message-Id: <1193694009.132106.322060@k79g2000hse.googlegroups.com>
On Oct 29, 5:04 am, rams <haai...@gmail.com> wrote:
>
> Thanks for ur help....when i used the format specified by you i
> observed a strange behaviour..i have given $a value as 271 ans i
> expect that the data while sending through socket would be 00 00 01 0f
> that is hex value(271) 0x010F but what i saw as a output of my socket
> is data as 32 37 31...since i tried with L option as well both are
> giving the same result.how to send that value as a hex itself..
Okay, if you gave $a a value of 271, then the data you want to send
through the socket would be 00 00 01 0f (hexadecimal) BUT ONLY IF the
data is in big-endian order. If the platforms are little-endian, then
217 would be sent as 0f 01 00 00 (hexadecimal). Just keep this in
mind. (Most platforms I work on are little-endian, so it's good to be
aware of the ordering.)
You said that when you tried sending 271 through the socket, the
socket received the bytes 32 37 31 (hexadecimal) instead of what you
expected of 00 00 01 0f (hexadecimal). Well, the values 32, 37, 31
(hexadecimal) correspond to the values 50, 55, 49 (decimal) which
correspond to the ASCII characters '2', '7', '1'.
It looks like when you packed the value 271 you mistakenly packed
it as a string (with something like "Z*") and not as an integer (with
"I"). Go back into your perl code and make sure that the 271 value is
the first value given after the pack-string in the pack() function,
and that the first character in your pack-string is "I" (and not "Z",
"A", or "a").
> for a variable $b i need to send 16 characters iam wondering during
> packing what will happen to the Null Character of the string.
Are you trying to pack a null-terminated string in the "char b[16]"
field? If so, the "Z16" specifier will be sure to terminate whatever
perl string you give it with a null-byte. For example, if you write:
my $string = pack("Z5", "abcdefgh");
then $string will be set to "abcd\0". (Notice that it doesn't use any
more characters that you told it to (in this case, 5) and that the
resulting string is always null-terminated, even if it means
truncating the original string.)
Whether or not you need that string to be null-terminated when you
send it through the socket depends on the receiving program. If it
needs to be null-terminated, use "Z16". If it doesn't need to be null-
terminated, you might want to use "a16". If it's not a string at all
but rather sixteen different integer values, use "c16". This is just
a guess, but most likely you'll want to use "Z16" (but I'll never be
sure unless I can read how the receiving program handles the data it
gets).
Is is possible to post the hex output of a sample structure that
you are trying to match? (Just one would be good; any more would
probably be too much.) That way a lot of questions can get answered
and I can help you better.
By the way, if you're confused about how to use the pack()
function, you may consider reading the Perl tutorial on "pack" and
"unpack" by typing "perldoc perlpacktut" at the DOS/Unix prompt. (I
found this page after I used pack() and unpack() extensively, and it
still taught me a lot.)
I hope this helps, Rams.
-- Jean-Luc Romano
------------------------------
Date: Tue, 30 Oct 2007 01:02:07 +0000 (UTC)
From: bgeer <bgeer@xmission.com>
Subject: Re: packing a C structure
Message-Id: <fg5vqf$f33$2@news.xmission.com>
rams <haairam@gmail.com> writes:
>On Oct 26, 10:37 pm, "jl_p...@hotmail.com" <jl_p...@hotmail.com>
>wrote:
>> On Oct 25, 10:57 pm, haai...@gmail.com wrote:
>>
>> I have the impression that what you want is to pack values in Perl
>> into a binary string of data that matches how a C program would do it.
>>
>> If I'm right, this might work:
>>
>> my $stringToSend = pack('I c16 I I c6 c8',
>> <YOUR PERL VARIABLES GO HERE>);
>>
Check out "man perlfunc", then use "/" to search followed by " pack "
with the spaces & without the ".
Note especially that if you want to put the resulting binary structure
on a network, that you should consider the difference between I (32
bit int) & N (unsigned long in network order).
On a 32-bit Intel, a long & int are the same size - I don't know why
there isn't a specific template character for "unsigned int in network
order".
Cheers, Bob
--
<> Robert Geer & Donna Tomky | |||| We sure |||| <>
<> bgeer@xmission.com | == == find it == == <>
<> dtomky@xmission.com | == == enchanting == == <>
<> Albuquerque, NM USA | |||| here! |||| <>
------------------------------
Date: Mon, 29 Oct 2007 12:06:43 -0700
From: TonyV <kingskippus@gmail.com>
Subject: Problem getting command output
Message-Id: <1193684803.541555.215290@19g2000hsx.googlegroups.com>
I'm running ActiveState Perl version v5.6.1 built for MSWin32-x86-
multi-thread.
I have a script that is being run from another application (HP
Openview Operations for Windows) under the NT Authority\SYSTEM
account, and when I try to collect the output of a command using
something like this:
my $output = `defrag -a c:`;
It comes back blank. It doesn't matter which command I try to run, it
always comes back blank. I really need the output of the command. If
I redirect it to a file like so:
`defrag -a c: > c:\\test.txt`;
It puts the correct info into the file.
I'm not sure why the output is being suppressed. I checked that STDIN
and STDOUT exists using the if (-t STDIN && -t STDOUT), and they both
seem to be.
Does anyone know if there's something special I need to do when trying
to read the output of system commands when scripts are run from within
an automated service-type application? I'm trying to avoid echoing
the results to a file and reading the file in if possible.
Thanks!
------------------------------
Date: Mon, 29 Oct 2007 12:06:45 -0700
From: TonyV <kingskippus@gmail.com>
Subject: Problem getting command output
Message-Id: <1193684805.913598.108220@d55g2000hsg.googlegroups.com>
I'm running ActiveState Perl version v5.6.1 built for MSWin32-x86-
multi-thread.
I have a script that is being run from another application (HP
Openview Operations for Windows) under the NT Authority\SYSTEM
account, and when I try to collect the output of a command using
something like this:
my $output = `defrag -a c:`;
It comes back blank. It doesn't matter which command I try to run, it
always comes back blank. I really need the output of the command. If
I redirect it to a file like so:
`defrag -a c: > c:\\test.txt`;
It puts the correct info into the file.
I'm not sure why the output is being suppressed. I checked that STDIN
and STDOUT exists using the if (-t STDIN && -t STDOUT), and they both
seem to be.
Does anyone know if there's something special I need to do when trying
to read the output of system commands when scripts are run from within
an automated service-type application? I'm trying to avoid echoing
the results to a file and reading the file in if possible.
Thanks!
------------------------------
Date: Mon, 29 Oct 2007 19:50:09 GMT
From: "John W. Krahn" <krahnj@telus.net>
Subject: Re: Problem getting command output
Message-Id: <4726396F.FA48A764@telus.net>
TonyV wrote:
>
> I'm running ActiveState Perl version v5.6.1 built for MSWin32-x86-
> multi-thread.
>
> I have a script that is being run from another application (HP
> Openview Operations for Windows) under the NT Authority\SYSTEM
> account, and when I try to collect the output of a command using
> something like this:
>
> my $output = `defrag -a c:`;
>
> It comes back blank. It doesn't matter which command I try to run, it
> always comes back blank. I really need the output of the command. If
> I redirect it to a file like so:
>
> `defrag -a c: > c:\\test.txt`;
>
> It puts the correct info into the file.
>
> I'm not sure why the output is being suppressed. I checked that STDIN
> and STDOUT exists using the if (-t STDIN && -t STDOUT), and they both
> seem to be.
>
> Does anyone know if there's something special I need to do when trying
> to read the output of system commands when scripts are run from within
> an automated service-type application? I'm trying to avoid echoing
> the results to a file and reading the file in if possible.
Try running it like this:
open PIPE, "defrag -a c: |" or die "Cannot open pipe from defrag: $^E":
my $output = do { local $/; <PIPE> };
close PIPE or warn $! ? "Error closing defag pipe: $!"
: "Exit status $? from defrag";
Then you may get some indication that there may be something wrong.
(Disclamer: I don't do Windows. YMMV)
John
--
use Perl;
program
fulfillment
------------------------------
Date: Mon, 29 Oct 2007 22:53:56 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: problems building perl modules, path question
Message-Id: <4t4iv4-nd1.ln1@osiris.mauzo.dyndns.org>
Quoth Ron Bergin <rkb@i.frys.com>:
>
> Try doing:
>
> $ sudo bash
> [pass your login credentials]
>
> # perl -MCPAN -e shell
>
> Note the difference between the $ prompt and the # prompt.
It would be better to avoid performing the Makefile.PL and make steps as
root, if possible. Since CPAN (at least recent versions) has support for
using sudo for make install, it would be better to take advantage of it.
Ben
------------------------------
Date: Mon, 29 Oct 2007 19:16:33 -0000
From: int eighty <xcdx80@gmail.com>
Subject: Query returns -1 if row present (DBI, SQL Server 2000)
Message-Id: <1193685393.723084.250940@19g2000hsx.googlegroups.com>
Hello,
I am using the DBI module to interface with a SQL Server 2000 database
-- connection, INSERT, UPDATE, and SELECT (when 0 rows exist in
resultset) are fine. However, if the resultset contains a row, a
value of -1 is returned. The query is very simple:
my $sth = $dbh->prepare(q{SELECT TOP 1 id FROM host WHERE ip = ?});
$sth->execute($ip);
print "Looked up $ip: " . $sth->rows . " ($DBI::errstr)\n";
if ($sth->rows == 0) {
# do something
}
elsif ($sth->rows > 0) {
# do something else
}
else {
# error
}
I do not believe this to is a permission problem with SQL Server as
the initial SELECT runs and jumps properly when $sth->rows is 0.
However the code always ends up in the else block if a row is returned
from the initial SELECT. The SELECT query also runs fine in Query
Analyzer when entered manually. Oh, it may also be worth mentioning
that $DBI::errstr is empty after the execute call for the initial
SELECT query.
TIA.
------------------------------
Date: Mon, 29 Oct 2007 14:35:40 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Query returns -1 if row present (DBI, SQL Server 2000)
Message-Id: <4726360d$0$10305$815e3792@news.qwest.net>
int eighty wrote:
> Hello,
>
> I am using the DBI module to interface with a SQL Server 2000 database
> -- connection, INSERT, UPDATE, and SELECT (when 0 rows exist in
> resultset) are fine. However, if the resultset contains a row, a
> value of -1 is returned. The query is very simple:
>
> my $sth = $dbh->prepare(q{SELECT TOP 1 id FROM host WHERE ip = ?});
> $sth->execute($ip);
> print "Looked up $ip: " . $sth->rows . " ($DBI::errstr)\n";
>
> if ($sth->rows == 0) {
> # do something
> }
>
> elsif ($sth->rows > 0) {
> # do something else
> }
>
> else {
> # error
> }
>
> I do not believe this to is a permission problem with SQL Server as
> the initial SELECT runs and jumps properly when $sth->rows is 0.
> However the code always ends up in the else block if a row is returned
> from the initial SELECT. The SELECT query also runs fine in Query
> Analyzer when entered manually. Oh, it may also be worth mentioning
> that $DBI::errstr is empty after the execute call for the initial
> SELECT query.
Possibly, reading the documentation will help.
"Returns the number of rows affected by the last row affecting command,
or -1 if the number of rows is not known or not available. "
[ continue reading docs for more information.]
------------------------------
Date: Mon, 29 Oct 2007 19:55:12 -0000
From: int eighty <xcdx80@gmail.com>
Subject: Re: Query returns -1 if row present (DBI, SQL Server 2000)
Message-Id: <1193687712.339674.66600@50g2000hsm.googlegroups.com>
On Oct 29, 2:35 pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
wrote:
> int eighty wrote:
> > Hello,
>
> > I am using the DBI module to interface with a SQL Server 2000 database
> > -- connection, INSERT, UPDATE, and SELECT (when 0 rows exist in
> > resultset) are fine. However, if the resultset contains a row, a
> > value of -1 is returned. The query is very simple:
>
> > my $sth = $dbh->prepare(q{SELECT TOP 1 id FROM host WHERE ip = ?});
> > $sth->execute($ip);
> > print "Looked up $ip: " . $sth->rows . " ($DBI::errstr)\n";
>
> > if ($sth->rows == 0) {
> > # do something
> > }
>
> > elsif ($sth->rows > 0) {
> > # do something else
> > }
>
> > else {
> > # error
> > }
>
> > I do not believe this to is a permission problem with SQL Server as
> > the initial SELECT runs and jumps properly when $sth->rows is 0.
> > However the code always ends up in the else block if a row is returned
> > from the initial SELECT. The SELECT query also runs fine in Query
> > Analyzer when entered manually. Oh, it may also be worth mentioning
> > that $DBI::errstr is empty after the execute call for the initial
> > SELECT query.
>
> Possibly, reading the documentation will help.
>
> "Returns the number of rows affected by the last row affecting command,
> or -1 if the number of rows is not known or not available. "
>
> [ continue reading docs for more information.]
What a surprise that something I often advocate actually works. That
is an unexpected aspect of the rows method, as I anticipated the
functionality would be similar to http://us.php.net/manual/en/function.mysqli-num-rows.php
The documentation doesn't lie, though. Many thanks.
------------------------------
Date: Tue, 30 Oct 2007 00:07:09 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: reading a directory, first files the newest ones
Message-Id: <slrnficpst.vpr.hjp-usenet2@zeno.hjp.at>
On 2007-10-29 05:31, Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
> Peter J. Holzer wrote:
>> On 2007-10-28 04:29, Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
>>> xhoster@gmail.com wrote:
>>>> There is no reason to think that ls is going to be meaningfully
>>>> faster about this than perl will.
>>> ... my benchmark (see below) indicates otherwise. The difference seems
>>> to increase when the directory size increases.
[...]
>>> $ perl sortdir.pl
>>> Rate Perl Linux
>>> Perl 174/s -- -75%
>>> Linux 693/s 297% --
>>
>> Your benchmark isn't valid: You are processing the complete directory
>> several hundred times per second, which indicates that it fits
>> completely into the buffer cache. After the first time you are measuring
>> mostly the processing time of ls and perl, not disk accesses.
>
> And that's what we were discussing,
If you have been discussing this, you totally missed the OPs problem.
The OP has to read a directory which - and I repeat this - takes more
than ten *minutes* to read. Your benchmar reads the directory in 5.7
(perl) or 1.4 (ls) *milliseconds*. That's a difference of more than four
orders of magnitude!
That tells us that the OP reads from a cold cache, while you read from a
hot cache: In you case CPU time is the dominant factor, so ls (being
written in C) will be faster. In the OPs case disk access time will be
the dominant factor, and any CPU usage advantage from ls will be
completely insignificant (and indeed ls may take more time because it
has to sort the files *after* having stat'ed them).
The only way to speed up this program is to reduce the number of disk
accesses. Xho already suggested way with the most potential: Change the
directory structure - having directories with hundredthousands or
millions of files is not a good idea, even of filesystems with tree- or
hash-structured directories. I asked if another one is feasible:
Estimating the age from the filename so you don't have to stat each one.
There is a third one, but I don't think this works in pure perl, because
you need access to information readdir doesn't deliver: Read the
directory, sort by inode number, then stat the files in order. This
doesn't reduce the number of stat calls, but it reduces the number of
disk seeks which may provide a major speedup (there's a patch for mutt
which uses this technique for maildirs, and it's really a lot faster for
large mailboxes).
hp
--
_ | Peter J. Holzer | It took a genius to create [TeX],
|_|_) | Sysadmin WSR | and it takes a genius to maintain it.
| | | hjp@hjp.at | That's not engineering, that's art.
__/ | http://www.hjp.at/ | -- David Kastrup in comp.text.tex
------------------------------
Date: Mon, 29 Oct 2007 11:14:44 -0700
From: "inderpaul_s@yahoo.com" <inderpaul_s@yahoo.com>
Subject: Regex Help
Message-Id: <1193681684.512456.251350@q5g2000prf.googlegroups.com>
I'm a newbie to perl expression more or less. What I want to do is to
use a regex pattern to find the presence of an IP address within a
file or string.
I found this example on the net for a pattern.... \b\d{1,3}\.\d{1,3}\.
\d{1,3}\.\d{1,3}\b
but reading on the \b means a word boundary but does it matter that I
want to find the pattern matching an IP address since the IP address
is made up of characters which are numeric and separated by a
decimal ?
Any further explanation of the above solution I have found would be
appreciated. Any other alternative solution would be appreciated.
Many thanks
Victor
------------------------------
Date: Mon, 29 Oct 2007 11:58:49 -0700
From: smallpond <smallpond@juno.com>
Subject: Re: Regex Help
Message-Id: <1193684329.260616.172930@57g2000hsv.googlegroups.com>
On Oct 29, 2:14 pm, "inderpau...@yahoo.com" <inderpau...@yahoo.com>
wrote:
> I'm a newbie to perl expression more or less. What I want to do is to
> use a regex pattern to find the presence of an IP address within a
> file or string.
>
> I found this example on the net for a pattern.... \b\d{1,3}\.\d{1,3}\.
> \d{1,3}\.\d{1,3}\b
>
> but reading on the \b means a word boundary but does it matter that I
> want to find the pattern matching an IP address since the IP address
> is made up of characters which are numeric and separated by a
> decimal ?
>
> Any further explanation of the above solution I have found would be
> appreciated. Any other alternative solution would be appreciated.
>
> Many thanks
>
> Victor
The pattern above will match "255.255.255.255"
but not match "foo255.255.255.255" or "255.255.255.255foo"
because those don't have word boundaries at both ends.
--S
------------------------------
Date: Mon, 29 Oct 2007 19:09:26 +0000
From: Peter Makholm <peter@makholm.net>
Subject: Re: Regex Help
Message-Id: <87ejfdivq1.fsf@hacking.dk>
"inderpaul_s@yahoo.com" <inderpaul_s@yahoo.com> writes:
> but reading on the \b means a word boundary but does it matter that I
> want to find the pattern matching an IP address since the IP address
> is made up of characters which are numeric and separated by a
> decimal ?
\b is the zero width point between something which is a word character
(\w) and something which isn't (\W). Word characters is defined as
alphabetic characters (a-z, A-Z), decimal digits (0-9) and the
underscore character (_) (and depending on locale something more).
So the four numeric parts of an IP-address counts as words in relation
to \b.
//Makholm
------------------------------
Date: Mon, 29 Oct 2007 12:12:38 -0700
From: TonyV <kingskippus@gmail.com>
Subject: Re: Regex Help
Message-Id: <1193685158.516700.123860@o38g2000hse.googlegroups.com>
On Oct 29, 2:14 pm, "inderpau...@yahoo.com" <inderpau...@yahoo.com>
wrote:
> I'm a newbie to perl expression more or less. What I want to do is to
> use a regex pattern to find the presence of an IP address within a
> file or string.
>
> I found this example on the net for a pattern.... \b\d{1,3}\.\d{1,3}\.
> \d{1,3}\.\d{1,3}\b
>
> but reading on the \b means a word boundary but does it matter that I
> want to find the pattern matching an IP address since the IP address
> is made up of characters which are numeric and separated by a
> decimal ?
>
> Any further explanation of the above solution I have found would be
> appreciated. Any other alternative solution would be appreciated.
>
> Many thanks
>
> Victor
This might be more than you're looking for, but from Regular
Expression Examples at this site: http://www.regular-expressions.info/examples.html
I found the following, which also captures the octets into groups for
use with $1, $2, $3, and $4:
\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?
[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4]
[0-9]|[01]?[0-9][0-9]?)\b
If you don't need to capture the octets, you can use this:
\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4]
[0-9]|[01]?[0-9][0-9]?)\b
These not only check for the right number of digits and dots, but
validates that the addresses are at least somewhat valid. (For
example, it won't match an illegal address like 64.128.249.712.)
Hope this helps,
--TV
------------------------------
Date: Mon, 29 Oct 2007 21:51:19 +0100
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Regex Help
Message-Id: <fg5kkv.1nc.1@news.isolution.nl>
inderpaul_s@yahoo.com schreef:
> I'm a newbie to perl expression more or less. What I want to do is to
> use a regex pattern to find the presence of an IP address within a
> file or string.
Consider Regexp::Common, specifically $RE{net}{ipv4}.
http://search.cpan.org/~abigail/Regexp-Common/lib/Regexp/Common/net.pm
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 V11 Issue 988
**************************************