[19414] in Perl-Users-Digest
Perl-Users Digest, Issue: 1609 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Aug 25 06:05:31 2001
Date: Sat, 25 Aug 2001 03:05:09 -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: <998733908-v10-i1609@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Sat, 25 Aug 2001 Volume: 10 Number: 1609
Today's topics:
Re: Accessing Microsoft SQL Server (version 6+) from Ac <goldbb2@earthlink.net>
Re: Accessing Microsoft SQL Server (version 6+) from Ac <kstep@pepsdesign.com>
Re: Accessing Microsoft SQL Server (version 6+) from Ac <kurt.stephens@verizon.net>
Re: Accessing Microsoft SQL Server (version 6+) from Ac <pne-news-20010825@newton.digitalspace.net>
Re: Can someone critique my code and point to better wa <goldbb2@earthlink.net>
Re: CGI redirect <mbudash@sonic.net>
Re: Check subroutine data from other process <cave@pertus.com.pl>
Re: Check subroutine data from other process <ilya@martynov.org>
Re: confused about 4 argument select <goldbb2@earthlink.net>
Re: Extract the relative sorting of items from multiple (David Combs)
Re: From UNIX to Windows <Dave.Stafford@globis.net>
Re: How should perl exit when sent TERM? <goldbb2@earthlink.net>
Re: localtime() - returns the wrong year! <rob_13@excite.com>
Re: one character at a time <goldbb2@earthlink.net>
Re: pl or not pl, that is the question <ilya@martynov.org>
Re: pl or not pl, that is the question (Martien Verbruggen)
Re: pl or not pl, that is the question <nowhere@dot.com>
Re: quick(?) programming question for perl newbie <krahnj@acm.org>
Re: Read/write specific line of file <goldbb2@earthlink.net>
Re: Read/write specific line of file <uri@sysarch.com>
Re: Read/write specific line of file <pne-news-20010825@newton.digitalspace.net>
Re: round off operator on Perl? <radiotito@yahoo.com>
Re: Statement modifiers?? (David Combs)
Re: String Substitution across two files <goldbb2@earthlink.net>
This has got to be a bug <arthur@ablair.physics.wisc.edu>
Re: This has got to be a bug (Logan Shaw)
Re: This has got to be a bug <mbudash@sonic.net>
Re: Webmail <callgirl@la.znet.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 25 Aug 2001 02:21:20 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Accessing Microsoft SQL Server (version 6+) from ActiveState Perl?
Message-Id: <3B8743E0.3B458DE0@earthlink.net>
Brad wrote:
>
> Is this possible? If so, can someone point me to the modules?
Yes. Get the DBI bundle, and the bundle containing DBD::mSQL [at least,
I assume that "Microsoft SQL Server" is mSQL]. Find them by going to
search.cpan.org, and first typing in "DBI" into the search field, [to
get DBI], then typing in "DBD::" into the search field, and looking for
the one that looks most appropriate.
--
I'm not a programmer but I play one on TV...
------------------------------
Date: Sat, 25 Aug 2001 06:27:16 GMT
From: Kurt Stephens <kstep@pepsdesign.com>
Subject: Re: Accessing Microsoft SQL Server (version 6+) from ActiveState Perl?
Message-Id: <3B874544.5020507@pepsdesign.com>
Brad wrote:
> Is this possible? If so, can someone point me to the modules?
>
> Thanks
> Brad
>
You can access MS-SQL using the DBI module with the DBD::ODBC or
DBD::ADO drivers. These should come preinstalled with ActiveState Perl,
however if you need a specific DBD driver you can install these via PPM.
DBI should work for most CRUD and data definition statements. I have
not tried using DBI with stored procedures, although this should work
for procedures taking parameterized inputs and returning rowsets. From
what I can tell reading the documentation, output and bidirectional
parameters do not seem to be supported.
HTH,
Kurt Stephens
------------------------------
Date: Sat, 25 Aug 2001 06:36:52 GMT
From: Kurt Stephens <kurt.stephens@verizon.net>
Subject: Re: Accessing Microsoft SQL Server (version 6+) from ActiveState Perl?
Message-Id: <3B874784.4070409@verizon.net>
Benjamin Goldberg wrote:
> Yes. Get the DBI bundle, and the bundle containing DBD::mSQL [at least, I assume that "Microsoft SQL Server" is mSQL].
DBD::mSQL is the driver for MiniSQL, a shareware, cross-platform
database engine by David Hughes. Definitely a different beast. The OP
should be lookong for DBD::ODBC or DBD::ADO.
Actually, I believe that you can use DBD::ODBC to connect to MiniSQL
using the appropriate ODBC driver, but definitely not the other way around.
------------------------------
Date: Sat, 25 Aug 2001 10:03:48 +0200
From: Philip Newton <pne-news-20010825@newton.digitalspace.net>
Subject: Re: Accessing Microsoft SQL Server (version 6+) from ActiveState Perl?
Message-Id: <4pjeots1n1d1jmnqfd44abnocda3tnshf4@4ax.com>
On Fri, 24 Aug 2001 16:52:32 -0700, "Brad" <Bradl@AAAcostbook.com>
wrote:
> Is this possible?
I would say yes.
> If so, can someone point me to the modules?
I would say DBI together with DBD::ODBC or maybe DBD::Sybase. There's
also Win32::ODBC, and possibly other methods.
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Sat, 25 Aug 2001 03:46:13 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Can someone critique my code and point to better way??
Message-Id: <3B8757C5.3ADFDCBE@earthlink.net>
Carlos C. Gonzalez wrote:
>
> Hi everyone,
>
> I was wondering if someone could critique my code by letting me know
> if there is a better, more efficient way to write or do what it does.
# call this as: print_contents( \*STDOUT, \%h );
use constant TEMPLATE =>
join( " ", map "%-${_}.${_}s", (20,10,15,13,12,3) ) . "\n";
sub print_contents {
my ($FH, $file_hash) = @_;
while( my ($key, $val) = each %$file_hash ) {
printf $FH TEMPLATE, $key, split /,/, $val;
}
}
There's no reason to join $key with $val, just to split it off.
Nor is there any need for a subroutine for something so simple as a
split on commas. Passing a reference to %h instead of turning it into a
list should help quite a bit, especially if it's large. Doing use
constant makes the code easier to read... I suppose you could have put
the printf format in there literally, rather than join/map, but I like
it this way :P. Declaring $key and $val so they are scoped to the while
loop gives them the smallest possible scope... note that they are *not*
realloced each time you go into the loop -- perl tries to keep memory
around that it expects to reuse. Moving them out of the loop will give
you little if any improvement.
--
I'm not a programmer but I play one on TV...
------------------------------
Date: Sat, 25 Aug 2001 07:42:38 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: CGI redirect
Message-Id: <mbudash-D8C354.00423925082001@news.sonic.net>
In article <9m6oo5$alt$1@news.netvision.net.il>, "Guy"
<guymal@__NOSPAM_hotmail.com> wrote:
> "Michael Budash" <mbudash@sonic.net> wrote in message
> news:mbudash-C673C4.23485221082001@news.sonic.net...
> > In article <3b832ff4@news.barak.net.il>, "Guy"
> > <guymal@__NOSPAM__hotmail.com> wrote:
> >
> > > How can I redirect to a url, using POST, and pass it some parameters?
> > >
> > > I'm using CGI.pm to open a saved query:
> > > open(OLD_Q,"$saved_query_path")|| die "Error: $! in file:
> > > ".__FILE__."
> > > line:
> > > ".__LINE__."Cannot open saved query $saved_query_path \n";
> > > my $old_query=new CGI(OLD_Q);
> > > close(OLD_Q);
> > >
> > > and I would like to redirect to a url as if the user submitted a form
> > > (the
> > > parameters from the saved query) to the new url:
> > >
> > > print
> > > $old_query->redirect("http://my-server.com/cgi-bin/a-script.pl");
> > > #but
> > > I need to pass all saved parameters using POST
> > >
> > > What I basically need is how to do something like this:
> > > my $one=$old_query->param('param_1');
> > > my $two=$old_query->param('param_2');
> > > print
> > >
> $old_query->redirect("http://my-server.com/cgi-bin/a-script.pl?param_1=$on
> > > e&
> > > param_2=$two....");
> > >
> > > but using POST.
> >
> > use the LWP perl module (which can be found at
> > http://www.perl.com/CPAN-local/modules/by-module/LWP/ as libwww-perl)
> >
>
> LWP would be used to open a connection from the web server to another
> url.
> What I need is to redirect the USER to another url (just like the CGI.pm
> redirect function).
trust me, you want LWP in a POST scenario. i do it all the time... read
the LWP docs (type: perldoc lwpcook)... or not, it's your choice...
hth-
--
Michael Budash ~~~~~~~~~~ mbudash@sonic.net
------------------------------
Date: Sat, 25 Aug 2001 10:09:49 +0200
From: Mirek Rewak <cave@pertus.com.pl>
Subject: Re: Check subroutine data from other process
Message-Id: <utmeotgjmdi6psdil568okpop1rd7bv3je@4ax.com>
On 24 Aug 2001 22:09:17 +0400, Ilya Martynov <ilya@martynov.org>
wrote:
>So you need InterProcess Communication (IPC). Read 'perldoc perlipc'.
Yes, I know that I must use it, but I don't know how? I know that I
must connect to process that will run the loop but while connect will
go through accept, the process will block and loop will stop.
Pozdrowienia
Mirek Rewak
cave@pertus.com.pl
------------------------------
Date: 25 Aug 2001 12:24:46 +0400
From: Ilya Martynov <ilya@martynov.org>
Subject: Re: Check subroutine data from other process
Message-Id: <87itfcv969.fsf@abra.ru>
>>>>> On Sat, 25 Aug 2001 10:09:49 +0200, Mirek Rewak <cave@pertus.com.pl> said:
MR> On 24 Aug 2001 22:09:17 +0400, Ilya Martynov <ilya@martynov.org>
MR> wrote:
>> So you need InterProcess Communication (IPC). Read 'perldoc perlipc'.
MR> Yes, I know that I must use it, but I don't know how? I know that I
MR> must connect to process that will run the loop but while connect will
MR> go through accept, the process will block and loop will stop.
If you want to use named pipes or sockets for communication and do not
want processes to be blocked use select (see 'perldoc -tf select';
note that Perl have two functions named select; you need select which
accepts four arguments). There was an example of select usage in
perlipc.
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Ilya Martynov (http://martynov.org/) |
| GnuPG 1024D/323BDEE6 D7F7 561E 4C1D 8A15 8E80 E4AE BE1A 53EB 323B DEE6 |
| AGAVA Software Company (http://www.agava.com/) |
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
------------------------------
Date: Sat, 25 Aug 2001 03:17:17 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: confused about 4 argument select
Message-Id: <3B8750FD.9F8B8FA@earthlink.net>
Alex Hart wrote:
>
> I have read the doc on this, but there are things about the select(4
> argument) that I don't get. I see in the doc to set it up like this:
>
> $rin = $win = $ein = '';
> vec($rin,fileno(STDIN),1) = 1;
> vec($win,fileno(STDOUT),1) = 1;
> $ein = $rin | $win;
>
> or this:
> sub fhbits {
> my(@fhlist) = split(' ',$_[0]);
> my($bits);
> for (@fhlist) {
> vec($bits,fileno($_),1) = 1;
> }
> $bits;
> }
> $rin = fhbits('STDIN TTY SOCK');
> then this:
> ($nfound,$timeleft) =
> select($rout=$rin, $wout=$win, $eout=$ein, $timeout);
>
> Can someone please explain the significance of the $rout=$rin part?
$rin is a bitvector with some flags set, one flag per filedescriptor.
$rout [after the assignment $rout=$in] has the same bunch of flags set.
After select() returns, it causes each bitvector to *only* have bits set
if the corresponding filehandle is readable, writable, or exceptional.
If you just did select($rin, ...), then $rin would be changed. By doing
it as select($rout=$rin, ...), then you can re-use $rin, without having
to set all the flags in it again. Here's two examples, one showin each:
sub answerQuestions {
print "You have 2 minutes to answer all of the\n";
print "questions in the quiz. Press enter to begin.\n";
my ($timeout, $rin, $ok) = (120, "", 1);
while( my $ques = next_question() ) {
print $ques;
vec($rin, fileno STDIN, 1) = 1;
($ok, $timeout) =
select( $rin, undef, undef, $timeout )
or die "select: $!";
last if !$ok;
got_answer( scalar <STDIN> );
}
print $ok ?
"You finished them all in time!\n" :
"Sorry, you ran out of time.\n";
show_score();
}
In the above code, after select() returns, you could look to see the
value of vec($rin, fileno STDOUT, 1) to see if STDOUT is ready for
reading. Of course, since there's only one fd we're looking at, we only
need to consider *how many* are available for reading or writing, and if
it's nonzero, we're ok, so I don't bother to look at $rin after select()
returns.
If you were using $rout = $rin, it would be changed:
my ($timeout, $rin, $ok, $rout) = (120, "", 1);
vec($rin, fileno STDIN, 1) = 1; # moved up here.
while( my $ques = next_question() ) {
print $ques;
($ok, $timeout) =
select( $rout = $rin, undef, undef, $timeout )
With this code, select() ends up changing $rout, rather than $rin, so I
can reuse $rin repeatedly, and not have to set it inside the loop.
> What does $rin look like . Can you tell me in english what is going on
> here.
It's a bitvector, a packed string of bits. If you have bit 0 set to
true, then the first byte will be chr(0b0000001). If you have bit 1 set
to true, then the first byte will be chr(0b0000010), etc. Of course,
you can have more than 1 bit set to true. Except in select(),
bitvectors aren't often important.
> Also, is this the only method of timing out a read? Will <> ever time
> out, or is it forever?
<> will wait forever, and not timeout. That's why you need select.
> It's all very appreciated. Thanks.
>
> - Alex
--
I'm not a programmer but I play one on TV...
------------------------------
Date: 25 Aug 2001 08:42:40 GMT
From: dkcombs@panix.com (David Combs)
Subject: Re: Extract the relative sorting of items from multiple lists
Message-Id: <9m7oe0$h0v$1@news.panix.com>
In article <3B76147C.1FBD0B8B@acm.org>, John W. Krahn <krahnj@acm.org> wrote:
>
>Get the first section of volume four here:
>
>http://Sunburn.Stanford.EDU/~knuth/fasc2a.ps.gz
>
THANK YOU!
How did you know this even existed -- unless
you are *at* Stanford?
Was it by an email-list? If so, I gotta
get on it!
David
------------------------------
Date: Sat, 25 Aug 2001 04:56:05 GMT
From: "Dave Stafford" <Dave.Stafford@globis.net>
Subject: Re: From UNIX to Windows
Message-Id: <FdGh7.455840$XL1.7838966@nlnews00.chello.com>
"Martin Vorlaender" <martin@radiogaga.harz.de> wrote in message
> Kev The Condenser (mkc@aon.at) wrote:
> > Benjamin Goldberg wrote:
> > > try this code:
> > > #!/usr/local/bin/perl -wT
> > > use strict;
> > > use CGI;
> > >
> > > print header, begin_html;
> > > print "<H1>Hellow World</H1>";
> > > print end_html;
> > > __END__
> >
> > did not thought that the CGI module would be _that_ powerful:)
> > used it only for getting GETted parameters...
>
> You could even replace the "<H1>...</H1>" stuff by h1(...). CGI,pm has
> functions for (almost?) all HTML tags.
No no no no!
You could, but don't. This is pointless abstraction of HTML, and IMO poor
practice. If you have anything more that a few lines of HTML use a template
module.
The "power" comes with a price. CGI.pm is huge, and very slow to load. If
you only want to get parameters use the much simpler (also faster and more
stable) cgi-lib.pl
If you use mod_perl the load time is less of an issue, but still, avoid
mixing HTML and perl for anything more than hello world.
Dave
------------------------------
Date: Sat, 25 Aug 2001 02:56:19 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: How should perl exit when sent TERM?
Message-Id: <3B874C13.8442FABB@earthlink.net>
Bruce McArdle wrote:
>
> I have a strange problem. This code sometimes will seemingly keep
> executing reaper forever if the perl script receives a kill -term:
> It's almost always consistent with some people and other people it
> behaves correct. Originaly I had die""; instead of exit(), and I
> thought that die was recursively calling the reaper, but exit() does
> the same thing.
>
use warnings;
use strict;
> use POSIX ":sys_wait_h";
> $SIG{TERM} = sub{reaper()};
> $SIG{INT} = sub{reaper()};
> $SIG{QUIT} = sub{reaper()};
You shouldn't name your subroutine "reaper" when it's not "reaping" a
child pid. Call it "cleanup" or somesuch.
Second, why make three anonymous subs here, when it would be faster and
use less memory to do:
@SIG{qw(TERM INT QUIT)} = (\&reaper) x 3;
> fork_process ("sleep 60");
>
> sub fork_process {
> my $exec_line = $_[0];
> # fork the sub process
> if (!defined($kidpid = fork())) {
> die "ERROR: cannot fork: $!";
> } elsif ($kidpid == 0) {
> # fork returned 0, so this branch is the child
> $debug && print "starting $exec_line\n";
> exec("$exec_line");
Do you have a reason for putting "" around $exec_line ?
> # if the exec fails, fall through to the next statement
> die "ERROR: can't exec: $!";
> } else {
> # fork returned neither 0 nor undef, so this branch is the parent
> waitpid $kidpid,0;
> $kidpid = 0;
> return $?;
Are you certain that you want to return $? I would think, that under
most circumstances, you would want to just check if it's zero, and die
with an appropriate error message if not. Also, you don't [but should]
check the return value of waitpid.
waitpid $kidpid, 0
or die "waitpid($kidpid) failed: $!";
my ($sig, $ret) = ($?/256, $?&255);
die qq{"$exec_line" died from signal $sig\n} if $sig;
die qq{"$exec_line" exited with code $ret\n} if $ret;
return 0;
> }
> } # sub fork_process
By the way, your indentation style is rather poor, and it's hard to tell
which braces matches which. Read perldoc perlstyle for some ideas.
>
> sub reaper{
> # if our legitament child really is running
> if ($kidpid) {kill 'TERM', $kidpid;}
> print "received TERM signal\n";
> exit();
> } # sub reaper
D'oh! I only noticed just here that $kidpid is global.
Blech. Try and avoid using globals when possible. This can easily be
done by making $kidpid a lexical private to fork_process, and your
reaper sub into an anonymous sub which refers to it.
sub fork_process {
my ($exec_line) = @_;
my ($kidpid, $killer);
$killer = sub {
my $gotsig = shift;
print "got a $gotsig signal\n";
kill 'TERM', $kidpid;
print "sent TERM signal to $kidpid\n";
exit;
};
# local is why we don't have to set $kidpid to 0 later.
local @SIG{qw(TERM INT QUIT)} = ($killer) x 3;
This way, when fork_process returns, the signal handlers it installed
all go back to how they were before.
Of course, this might not necessarily solve your problem, but better
diagnostics can certainly help.
--
I'm not a programmer but I play one on TV...
------------------------------
Date: Sat, 25 Aug 2001 06:09:13 GMT
From: "Rob - Rock13.com" <rob_13@excite.com>
Subject: Re: localtime() - returns the wrong year!
Message-Id: <Xns910815E5374CErock13com@64.8.1.227>
Peter Mann>
> Dear All,
> I have a small problem I can't work out! I need to retrieve
> the current
> date and display it in 'short date' format. This is my code I
> have to try and achieve this! The problem is that is displays
> '24/07/101'
> The day and month are correct, but 101 for the year isn't! Why
> wont is give me back the correct year?
perldoc -f localtime
The year is given as an offset from 1900. So add 1900. The months
start with January as 0 so add 1.
> sub tasksBehind
> {
> my (undef, undef, undef, $day, $month, $year) = localtime();
> print "$day/$month/$year";
> }
I would do this:
my @date = (localtime)[5,4,3];
$date[0] += 1900;
$date[1] += 1;
print join('-', @date);
And it would print yyyy-mm-dd. i.e. a better format for the date
although this method won't pad the month or day with a zero.
--
Rob - http://rock13.com/
Web Stuff: http://rock13.com/webhelp/
------------------------------
Date: Sat, 25 Aug 2001 01:16:43 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: one character at a time
Message-Id: <3B8734BB.3DDA1D93@earthlink.net>
Ken wrote:
>
> I have some strings of text that I need to take one character out of
> at a time. I've looked into getc(), but that apparently only works on
> filehandles (and according to the Camel book, it's also very slow).
>
> An example of what I need:
>
> I have the string "my little string". I need to loop through and pull
> out first the 'm', then the 'y', then the ' ', then the 'l', etc.
my $string = "my little string";
while( /(.)/gs ) {
print $1, "\n";
}
--
I'm not a programmer but I play one on TV...
------------------------------
Date: 25 Aug 2001 12:54:24 +0400
From: Ilya Martynov <ilya@martynov.org>
Subject: Re: pl or not pl, that is the question
Message-Id: <87elq0v7sv.fsf@abra.ru>
>>>>> On 24 Aug 2001 17:50:39 -0700, merlyn@stonehenge.com (Randal L. Schwartz) said:
>>>>> "Ilya" == Ilya Martynov <ilya@martynov.org> writes:
Ilya> .pl as 'perl library' was valid in Perl 4 days. Nowdays people
Ilya> (including me) often use .pl for scripts. I think it is ok.
RLS> Do you put .sh on the end of all your shell scripts, and .py on the
RLS> end of all your python programs? And do you rename /bin/cat to
RLS> /bin/cat.o? It's an object file after all! And what happens
RLS> if you change the implementation from Perl to shell? Do you have
RLS> to readjust all the invokers of your commands?
RLS> I'm of the firm belief that a *command* (aka "program") should *not*
RLS> have the implementation language embedded in the path. And I'm not
RLS> the only one out there. Why should I care when I invoke your program
RLS> that it was shell or Perl or C originally? I just care about the
RLS> interface!
Ok. On Unix if script is a command (i.e. something executable
installed in $PATH) I also think that it should not have any postfix
because user usually doesn't care how command is implemented. But in
other cases it can be useful to know if something is Perl, Python,
Ruby or another language script. At least when I write scripts for
myself which don't leave my $HOME I always add postfix to them. Also
it can be convenient to use postfixes when dealing with web server
configuration.
RLS> Also, if it's in the path, and that path is used for CGI, then it
RLS> gives away a potential exploit. For example, if I'm a bad guy, and I
RLS> discover that many Python scripts can be broken by typing "FOO" into a
RLS> form box, then I'll look for "_____.py" in paths using a search
RLS> engine. Instant hole, because you've given away more than you need to
RLS> give away.
I don't agree. Most webservers are configured in such way that it is
possible to find easily if some url is bound to scripts. It will be
either 'cgi', 'cgi-bin', some postfix or something simular in URL. And
usually all programs on one webserver are written in one language. So
if I were this hacker who found such breakage I would find easily what
is common in URL's of scripts on webserver and I would try to break
them all.
RLS> [..skip..]
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Ilya Martynov (http://martynov.org/) |
| GnuPG 1024D/323BDEE6 D7F7 561E 4C1D 8A15 8E80 E4AE BE1A 53EB 323B DEE6 |
| AGAVA Software Company (http://www.agava.com/) |
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
------------------------------
Date: Sat, 25 Aug 2001 18:43:29 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: pl or not pl, that is the question
Message-Id: <slrn9oep9h.3ga.mgjv@martien.heliotrope.home>
On 24 Aug 2001 17:50:39 -0700,
Randal L. Schwartz <merlyn@stonehenge.com> wrote:
>>>>>> "Ilya" == Ilya Martynov <ilya@martynov.org> writes:
>
>Ilya> .pl as 'perl library' was valid in Perl 4 days. Nowdays people
>Ilya> (including me) often use .pl for scripts. I think it is ok.
>
> And do you rename /bin/cat to
> /bin/cat.o? It's an object file after all!
Of course not. It's an ELF executable, not _just_ an object file.
Everyone knows it's /bin/cat.exe.. or rather, /bin/CAT.EXE... Or was it
C:\BIN\CAT.EXE, or is it CAT.COM?
anyway...
$ ls -1 /bin | grep -i cat
CAT
CAT.COM
CAT.EXE
cat
cat.com
cat.elf
cat.elf.stripped
cat.elf.stripped.80386
cat.elf.stripped.80386.1
cat.exe
Don't think anyone is going to be missing it. Except... for you I now
have to add cat.o as a link. *sigh*
Martien
--
Martien Verbruggen |
Interactive Media Division | This matter is best disposed of from
Commercial Dynamics Pty. Ltd. | a great height, over water.
NSW, Australia |
------------------------------
Date: Sat, 25 Aug 2001 20:04:05 +1000
From: "Gregory Toomey" <nowhere@dot.com>
Subject: Re: pl or not pl, that is the question
Message-Id: <%GKh7.2157$Nd2.46926@newsfeeds.bigpond.com>
"Randal L. Schwartz" <merlyn@stonehenge.com> wrote in message
news:m13d6hx8rk.fsf@halfdome.holdit.com...
> >>>>> "Ilya" == Ilya Martynov <ilya@martynov.org> writes:
>
> Ilya> .pl as 'perl library' was valid in Perl 4 days. Nowdays people
> Ilya> (including me) often use .pl for scripts. I think it is ok.
>
> Do you put .sh on the end of all your shell scripts, and .py on the
> end of all your python programs? And do you rename /bin/cat to
> /bin/cat.o? It's an object file after all! And what happens
> if you change the implementation from Perl to shell? Do you have
> to readjust all the invokers of your commands?
I call my Perl scripts anything from stock.cgi, daily.pl, runload etc.
Windows may enforce file extensions but you can make names in *nix far more
readable.
gtoomey
------------------------------
Date: Sat, 25 Aug 2001 04:19:08 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: quick(?) programming question for perl newbie
Message-Id: <3B8727AE.AACE2095@acm.org>
David Hilsee wrote:
>
> "John W. Krahn" <krahnj@acm.org> wrote in message
> news:3B871FF3.870298A2@acm.org...
> >
> > Thanks David!
> >
> > Nathan, change the subroutine to this:
> >
> >
> > sub getheaderval {
> > my $filename = shift || return;
> > open IN, $filename or die "Cannot open $filename: $!";
> > binmode IN;
> > seek IN, 2194, 0 or die "Cannot seek on $filename: $!";
> > my $shortval;
> > read( IN, $shortval, 2 ) == 2 or die "Cannot read from $filename:
> > $!";
> > close IN;
> > return unpack 'n', pack 'S', $shortval;
> > }
>
> I think that last statement should be:
>
> return unpack 'n', $shortval;
>
> After all, it is a short in network byte order, right?
Yes, I just copied and pasted from my original (erroneous) post.
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sat, 25 Aug 2001 02:17:29 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Read/write specific line of file
Message-Id: <3B8742F9.2C77FABF@earthlink.net>
GOGAR wrote:
>
> Hello there,
>
> i'm fairly new to perl and ..umm i was just wondering if it was
> possible to read or write a specific line of a file instead of having
> to go through the whole file..
>
> i'm working on some cgi scripts so it's important that it's as fast as
> possible. Reading in the whole file and picking out the line i want
> just doesn't feel right..
> my guess is that it's something with sysread and syswrite but i'm
> not sure how to approach this, i hope anybody can help me here.
Unless your lines are of fixed length, there's no really fast way to go
to a particular line without first reading in all the preceding lines.
sub get_Nth_line($$) {
my ($filename, $line) = @_;
croak "Use seek() and read() for fixed length records"
if ref($/);
confess "Paragraph mode not implemented" if $/ eq "";
return undef if $line < 0;
local (*FILE, $_);
open( FILE, "<", $filename )
or croak "Could not open $filename for read: $!";
return scalar <FILE> if $line == 0;
my $blocksize = (stat(FILE))[11] || 1024;
while( sysread( FILE, $_, $blocksize, length $_ ) ) {
my $count = (() = m[\Q$/]g);
if( $count < $line ) {
next if length($_) < length($/); #unlikely?
$line -= $count;
substr( $_, 0, length($_)-length($/), "" );
next;
}
if( $count == $line ) {
next;
}
# the nth line starts and ends within $_
# I do a linear search, but a binary search might
# be faster.
while( --$count > $line ) {
s[^[\x00-\xFF]*?\Q$/][];
}
s[^\Q$/\E[\x00-\xFF]*][];
return $_;
}
die "\$/ never occured within $filename!\n";
}
This code is untested. It's entirely possible that the logic of the
comparisons of $count and $line is wrong.
--
I'm not a programmer but I play one on TV...
------------------------------
Date: Sat, 25 Aug 2001 06:37:24 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Read/write specific line of file
Message-Id: <x78zg8hcgl.fsf@home.sysarch.com>
>>>>> "BG" == Benjamin Goldberg <goldbb2@earthlink.net> writes:
BG> sub get_Nth_line($$) {
BG> my ($filename, $line) = @_;
BG> croak "Use seek() and read() for fixed length records"
BG> if ref($/);
BG> confess "Paragraph mode not implemented" if $/ eq "";
BG> return undef if $line < 0;
return undef will break if the sub is called and the result is saved in
an array. the array will have a single value of undef and will be true.
a plain return is what is needed, it returns undef in scalar context and
an empty list in a list context.
BG> local (*FILE, $_);
why $_?
BG> open( FILE, "<", $filename )
BG> or croak "Could not open $filename for read: $!";
BG> return scalar <FILE> if $line == 0;
$line_num would be better.
BG> my $blocksize = (stat(FILE))[11] || 1024;
BG> while( sysread( FILE, $_, $blocksize, length $_ ) ) {
why are you doing block i/o? let perl do it for you.
why $_? you use it explicitly below so you might as well give it a
variable name
BG> my $count = (() = m[\Q$/]g);
ewww. that ugly idiom strikes again.
and why scan for $/ twice? here and below?
BG> if( $count < $line ) {
BG> next if length($_) < length($/); #unlikely?
length defaults to $_.
BG> $line -= $count;
BG> substr( $_, 0, length($_)-length($/), "" );
BG> next;
BG> }
BG> if( $count == $line ) {
BG> next;
BG> }
a waste of braces. they are precious. you use if modifiers so use it
here.
next if $count == $line ;
BG> # the nth line starts and ends within $_
BG> # I do a linear search, but a binary search might
BG> # be faster.
BG> while( --$count > $line ) {
BG> s[^[\x00-\xFF]*?\Q$/][];
BG> }
BG> s[^\Q$/\E[\x00-\xFF]*][];
BG> return $_;
BG> }
BG> die "\$/ never occured within $filename!\n";
BG> }
what is wrong with something simple like:
do{ $line = <FH> } while $. < $line_number_wanted ;
return $line;
that needs some work to handle EOF and such but it is all the OP really
wants.
for a similar (and working) way to read blocks of data and get
lines/records from it, look at the code in File::Readbackwards on CPAN.
it could be modified to work forwards and take a line number as
an option. i leave that as an exercise to the reader.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Search or Offer Perl Jobs -------------------------- http://jobs.perl.org
------------------------------
Date: Sat, 25 Aug 2001 10:03:48 +0200
From: Philip Newton <pne-news-20010825@newton.digitalspace.net>
Subject: Re: Read/write specific line of file
Message-Id: <6rjeotsfomm78m7rhpmru9rmdgnj4u1417@4ax.com>
On Sat, 25 Aug 2001 06:03:56 +0200, "GOGAR" <angenent@kabelfoon.nl>
wrote:
> i'm fairly new to perl and ..umm i was just wondering if it was possible to
> read or write a specific line of a file instead of having to go through the
> whole file..
Depends. One way is to use DB_File with DB_RECNO which allows tied
access to the file as if it were an array of lines.
> my guess is that it's something with sysread and syswrite but i'm not sure
> how to approach this,
This will work if all your lines are exactly the same length. Otherwise
I don't know of a better stand-alone approach than reading in the file
(a line at a time or a chunk at a time) until you've got the line you
want.
If you can make extra files, you could create an index file which maps
line numbers to file offsets; then you could seek() to the desired place
in your file quickly (and since the index file entries would have
constant length, finding the right one also only requires a seek() and a
read()).
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Sat, 25 Aug 2001 10:08:32 +0200
From: Valentin 30IR976 <radiotito@yahoo.com>
Subject: Re: round off operator on Perl?
Message-Id: <3B875D00.37D0B96D@yahoo.com>
OK, many thanks to all. I will try
$x=87.9;
my $x=int($x);
It works fine...
Thank you...
Ilya Martynov wrote:
> >>>>> On Fri, 24 Aug 2001 19:57:36 +0200, Valentin 30IR976 <radiotito@yahoo.com> said:
>
> V3> Hello. I have results on my Perl program like 2.5, 87.9, and I want to
> V3> have only the "integer" part of this number. I know I can do something
> V3> like:
>
> V3> printf("%d",87.9);
>
> V3> and it prints 87 (what I want), but my question is if Perl has any
> V3> operator/function that make this thing automatic. I know in C exists,
> V3> but...in Perl?
>
> int(). See 'perldoc -tf int'
>
> --
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> | Ilya Martynov (http://martynov.org/) |
> | GnuPG 1024D/323BDEE6 D7F7 561E 4C1D 8A15 8E80 E4AE BE1A 53EB 323B DEE6 |
> | AGAVA Software Company (http://www.agava.com/) |
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
--
73 de Valentin 30IR976 (161 DXCC) ICQ # 86220598 Firetalk # 1626864
web 30IR976: http://www.geocities.com/titoradio
IR Members: Promote your IR-related-Website at:
http://www.topsitelists.com/bestsites/topir976list/topsites.html
web personal: http://get.to/zamora
SEMANA SANTA ZAMORA http://www.geocities.com/semanasantazamora
------------------------------
Date: 25 Aug 2001 09:49:21 GMT
From: dkcombs@panix.com (David Combs)
Subject: Re: Statement modifiers??
Message-Id: <9m7sb1$kt3$1@news.panix.com>
In article <9l9lfp$n9e$1@bob.news.rcn.net>,
Eric Bohlman <ebohlman@omsdev.com> wrote:
>ctcgag@hotmail.com wrote:
>> What's more, always add "or die" to the ends of things that can fail in
>> general, even if they can't conceivably fail in the local logic.
>
>> m/Foo(.*)Bar/ or die;
>> do_something($1);
>
>> Yes, even if you already made sure that Foo and Bar are there and in
>> order, still add the or die.
>
>That's a variant on the technique known as "assertions." Some languages
>have built-in mechanisms for making assertions and verifying them (often
>allowing you to turn on assertion-checking during development and testing
>and turn it off in production, without having to remove the assertion
>code.
>
The language I use mostly, MainSail (has gc, dynamic memory,
cross-compiles all over the place, has coroutines,
etc, etc -- and no one's even heard of it!) also has
macros (in fact, they're built-in to the compiler,
so that when in the debugger, you see the source,
of course, but you can execute macros; nice!).
Anyway, about assertions.
You really need macros to do them "right".
You define assert *as a macro*.
So that Assert(i > j) generates:
if not (i > j) {
either-die-or-warn-or-goIntoDebugger("Assertion Failed: " . "i > j")
}
(not exact syntax, but at least readable)
, the really neat thing being that you pass that "i > j"
ONLY ONCE!, and inside the macro's code you can
add your quotes and stringify it, etc.
---
Oh, that language has lots of things perl should have
(and vice versa!!!), but in the debugger, you
can go up and down the stack frames, and look
at the variables, etc at that level.
You can go both up and down.
I think you can do *something* like that in perl's debugger,
but it's not at all as easy or transparent.
----
Anyway, macros are WONDERFUL! You can sure do some clever
things with them!
(But of course perl 5 or 6 will NEVER EVER have macros;
on those tablets Larry brought down from that mountain,
item one is: THOU SHALT HAVE NO MACROS,
and, well, that's just the way it is. :-(
David
------------------------------
Date: Sat, 25 Aug 2001 02:19:04 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: String Substitution across two files
Message-Id: <3B874358.6C3C4838@earthlink.net>
Pularis wrote:
>
> I have two very long multi column text files. Is it possible to
> somehow look at one of the columns in one file and then substitue an
> entry from that file to the other file if a match is found. Both
> files have a common column, I want to replace other entries in one of
> the columns if a match is found. I would greatly appreciate any help.
Give some example data, and what you expect the result to be, and
perhaps we'll be able to help.
--
I'm not a programmer but I play one on TV...
------------------------------
Date: 25 Aug 2001 07:14:20 GMT
From: art blair <arthur@ablair.physics.wisc.edu>
Subject: This has got to be a bug
Message-Id: <9m7j8c$8q2$1@news.doit.wisc.edu>
If this isn't a bug, it should be.
It appears that 2 strings more than 100 chars in length will
sometimes match when they shouldn't.
I'm running perl 5.6.0 on a mandrake 8.0 linux box. Consider the
following bit of dialog in the debugger.
DB<201> $qq="FDRY |WF Van Kasper |Strong Buy |13.85 |8/24/2001:1056 |1.6 |1.4 |0.3 |"
DB<202> $ww="AAI |UBS Warburg |Hold |5.96 |8/23/2001:1512 |0.0 |0.0 |0.0 |"
DB<203> p ($ww =~ /$qq/)
1
DB<204> $ww="AAI |UBS Warburg |Hold |5.96 |8/23/2001:1512 |0.0 |0.0 |0.0 "
DB<205> $qq="FDRY |WF Van Kasper |Strong Buy |13.85 |8/24/2001:1056 |1.6 |1.4 |0.3 "
DB<206> p ($ww =~ /$qq/)
DB<207>
Note there are no newlines so all that multiline stuff shouldn't be an
issue.
The only change is that in the first case the strings are 101 chars in
length. They should not have matched in either case (right!?!) but in
the first case they do.
Any ideas why?
Art.
------------------------------
Date: 25 Aug 2001 02:34:21 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: This has got to be a bug
Message-Id: <9m7kdt$qco$1@charity.cs.utexas.edu>
In article <9m7j8c$8q2$1@news.doit.wisc.edu>,
art blair <arthur@ablair.physics.wisc.edu> wrote:
>It appears that 2 strings more than 100 chars in length will
>sometimes match when they shouldn't.
>I'm running perl 5.6.0 on a mandrake 8.0 linux box. Consider the
>following bit of dialog in the debugger.
>
>DB<201> $qq="FDRY |WF Van Kasper |Strong Buy |13.85 |8/24/2001:1056 |1.6 |1.4 |0.3 |"
>
>DB<202> $ww="AAI |UBS Warburg |Hold |5.96 |8/23/2001:1512 |0.0 |0.0 |0.0 |"
>
>DB<203> p ($ww =~ /$qq/)
>1
>DB<204> $ww="AAI |UBS Warburg |Hold |5.96 |8/23/2001:1512 |0.0 |0.0 |0.0 "
>
>DB<205> $qq="FDRY |WF Van Kasper |Strong Buy |13.85 |8/24/2001:1056 |1.6 |1.4 |0.3 "
>
>DB<206> p ($ww =~ /$qq/)
>
>DB<207>
>
>Note there are no newlines so all that multiline stuff shouldn't be an
>issue.
>The only change is that in the first case the strings are 101 chars in
>length. They should not have matched in either case (right!?!) but in
>the first case they do.
What does this regular expression match?
/abc/
That's right -- it matches any string that has "abc" in it. What about
this one?
//
It matches any string that contains the empty string, i.e. any string
at all. Now what about this one?
/abc|123/
It matches any string that either has "abc" or has "123" in it.
So, what do you think this one matches?
/abc|/
It matches any string that either has "abc" or has the empty string in
it. That means it matches any string regardless of whether "abc"
occurs, because the empty string always occurs in any string.
Your first regular expression (line 201) looks a lot like that regular
expression, because it ends with the character "|". That means this
regular expression will match any string whatsoever. And that's
why it matches the string you try to match it against.
Hope that helps.
- Logan
--
"Our grandkids love that we get Roadrunner and digital cable."
(Advertisement for Time Warner cable TV and internet access, July 2001)
------------------------------
Date: Sat, 25 Aug 2001 07:37:33 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: This has got to be a bug
Message-Id: <mbudash-B106A5.00373525082001@news.sonic.net>
In article <9m7j8c$8q2$1@news.doit.wisc.edu>, art blair
<arthur@ablair.physics.wisc.edu> wrote:
> If this isn't a bug, it should be.
> It appears that 2 strings more than 100 chars in length will
> sometimes match when they shouldn't.
> I'm running perl 5.6.0 on a mandrake 8.0 linux box. Consider the
> following bit of dialog in the debugger.
>
> DB<201> $qq="FDRY |WF Van Kasper |Strong Buy |13.85
> |8/24/2001:1056 |1.6 |1.4 |0.3 |"
>
> DB<202> $ww="AAI |UBS Warburg |Hold |5.96
> |8/23/2001:1512 |0.0 |0.0 |0.0 |"
>
> DB<203> p ($ww =~ /$qq/)
> 1
> DB<204> $ww="AAI |UBS Warburg |Hold |5.96
> |8/23/2001:1512 |0.0 |0.0 |0.0 "
>
> DB<205> $qq="FDRY |WF Van Kasper |Strong Buy |13.85
> |8/24/2001:1056 |1.6 |1.4 |0.3 "
>
> DB<206> p ($ww =~ /$qq/)
>
> DB<207>
>
> Note there are no newlines so all that multiline stuff shouldn't be an
> issue.
> The only change is that in the first case the strings are 101 chars in
> length. They should not have matched in either case (right!?!) but in
> the first case they do.
>
> Any ideas why?
yup. the *pertinent* diff between the first and the second cases is the
final pipe on $qq, which, when used as a match expression, has special
meaning. that's as much as i'm gonna tell ya... oh, what the heck: check
out the meaning of \Q in a match expression...
hth-
--
Michael Budash ~~~~~~~~~~ mbudash@sonic.net
------------------------------
Date: Fri, 24 Aug 2001 22:30:47 -0700
From: Kira <callgirl@la.znet.com>
Subject: Re: Webmail
Message-Id: <3B873807.5A5659E4@la.znet.com>
fanzila@yahoo.com wrote:
> I want to set up free webmail service for my site, where the user sign
> up automatly does anyone know free script ?
Took me less than a minute to find hundreds of email scripts.
Are your fingers broken?
http://www.google.com/search?q="email+script"
Godzilla!
------------------------------
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 1609
***************************************