[19022] in Perl-Users-Digest
Perl-Users Digest, Issue: 1217 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jun 29 09:05:32 2001
Date: Fri, 29 Jun 2001 06:05:10 -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: <993819910-v10-i1217@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Fri, 29 Jun 2001 Volume: 10 Number: 1217
Today's topics:
anonymous subroutines - why? <m.grimshaw@salford.ac.uk>
Re: Basic Questions about Locking a DBM <amittai@amittai.com>
Re: Code Review Needed! (Dave Hoover)
getting a defined value from a html page <nospam@peng.nl>
Re: Help: How to make a hash global <goldbb2@earthlink.net>
Re: how can i get the output of a forked child? <goldbb2@earthlink.net>
How to install Math-Round on windows <keng@spinalfluid.com>
Re: passing variables the 'right' way <bart.lateur@skynet.be>
Re: Perl from VXML <howard@brazee.net>
Re: Perl from VXML <howard@brazee.net>
Re: reading the first line off the file <goldbb2@earthlink.net>
Re: releasing array memory <goldbb2@earthlink.net>
Re: replacement -- peculiar interpolation (Anno Siegel)
Re: Scanning a file in CGI <wyzelli@yahoo.com>
Re: Scanning a file in CGI (Anno Siegel)
Re: Scanning a file in CGI <wyzelli@yahoo.com>
Re: Search script: passing information between programs <gnarinn@hotmail.com>
starting with perl <mr.thanquol@gmx.de>
Re: starting with perl <krahnj@acm.org>
Re: starting with perl <mr.thanquol@gmx.de>
Re: starting with perl <goldbb2@earthlink.net>
Textarea to file <oeser@enternet.com.au>
Re: Textarea to file (TuNNe|ing)
Re: Textarea to file <goldbb2@earthlink.net>
Re: Textarea to file <flavell@mail.cern.ch>
Re: What kind of object is an old-fashioned DBM? <amittai@amittai.com>
Re: What kind of object is an old-fashioned DBM? (Anno Siegel)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 29 Jun 2001 13:05:41 +0100
From: Mark Grimshaw <m.grimshaw@salford.ac.uk>
Subject: anonymous subroutines - why?
Message-Id: <3B3C6F15.C5028408@salford.ac.uk>
Hi,
I've been looking at implementations of Sockets in 2nd Ed. Camel pp
350-351 and was puzzled by the usage of:
spawn sub{...};
there.
I've got a simplified version just for test purposes followed by a
'standard' example. Despite the fact that Ex 2 requires less typing, is
there an advantage to using Ex 1 or am I just being perverse for such a
simple script? Do any advantages only come when I start using anonymous
subroutines for closure?
Ex 1:
#!/usr/local/bin/perl -w
use strict;
sub my_print;
my_print sub
{
$_[0] eq 1 ? print "$_[0]\n" : print "not 1\n";
};
sub my_print
{
my $subref = shift;
exit &$subref('1');
}
#############
Ex 2:
#!/usr/local/bin/perl -w
use strict;
sub my_print
{
$_[0] eq 1 ? print "$_[0]\n" : print "not 1\n";
}
exit &my_print('1');
############
------------------------------
Date: Fri, 29 Jun 2001 12:06:30 +0200
From: "Amittai Aviram" <amittai@amittai.com>
Subject: Re: Basic Questions about Locking a DBM
Message-Id: <9hhk2h$o2e$1@rznews2.rrze.uni-erlangen.de>
Thanks to Dave for the kind and helpful explanation!
Please note followup questions below. -- Amittai Aviram
"Mark Grimshaw" <m.grimshaw@salford.ac.uk> wrote in message
news:3B3C4E12.9CED9334@salford.ac.uk...
>
>
> dave wrote:
> >
> > open (DBLOCK, "$db_lock") or bail("Cannot open lock file $db_lock
> > $!");
> > flock(DBLOCK, LOCK_EX);
> >
> > #dbmopen, tie, save the world here, but most likely:
> > #open something
> > #write to it
> > #close it
> >
> > close (DBLOCK) or bail("Can't close size file $db_lock $!");
> > flock(DBLOCK, LOCK_UN);
>
> doesn't closing the filehandle DBLOCK implicitly unlock it?
I have been wondering the very same thing. To my surprise, none of my basic
sources -- the "camel book" and _Introduction to Perl_ -- actually says
whether closing the file unlocks it automatically or not.
Other question. Dave's example opens any old file using "open," but the
camel book specifies that you should use sysopen and not just plain open.
Why? Does it make a difference?
Finally ... I did really notice -- or anyway thought I noticed -- a
substantial slowdown in my CGI scripts when run from the server through the
Web. I was suprised -- nobody else is using them right now, and the lock
only opens one more file and later closes it. Am I just imagining this?
Was it an accident of how slowly the server was running that day in general?
Thanks again for your help!
Amittai Aviram
------------------------------
Date: 29 Jun 2001 03:38:33 -0700
From: redsquirreldesign@yahoo.com (Dave Hoover)
Subject: Re: Code Review Needed!
Message-Id: <812589bb.0106290238.d2485e4@posting.google.com>
Tad McClellan wrote ...
[snip]
> If the file is named 'soapbox.tar.gz', folks expect it to
> create a directory named 'soapbox' to put its files in.
>
> Your packaging would have wiped out my INSTALL file if one
> should have happened to be in my current directory...
These are the newbie mistakes I was hoping this review would catch. It
has been corrected. Thanks.
> > #!/usr/bin/perl -w
>
>
> Gak! If you are taking form input, you really ought to have
> taint checking turned on and think when you untaint the data.
> (perldoc perlsec):
>
>
> #!/usr/bin/perl -wT
Ok, I've added this, but now when I try to run the script it tells me:
Too late for "-T" option at main.cgi line 1.
> > # Load the keywords used
> > my ($terms, @terms);
> > if ($s->{terms}) {
> > @terms = @{$s->{terms}};
> > for (@terms) { $terms .= "$_ " }
>
>
> You do not need the @terms temporary variable, and Perl has
> a function for doing what I think you are trying to do:
>
> if ($s->{terms}) {
> $terms = join ' ', @{$s->{terms}};
Wow, yeah, I wonder why join didn't come to mind. Seems so obvious
now. Thanks.
> > chomp($terms); # for some reason chomp wasn't working here
> ^^^^^^^^^^^^^^^^^^^
>
> Yes it was. chomp() removes a newline if the string ends with one,
> else it does nothing. There cannot possibly be a newline at the
> end of any of your strings, because you tack a space onto the
> end each time. chomp() does nothing, like it is supposed to.
[snip]
I think I temporarily lost touch with reality and believed that chomp
removed any white space, not just newlines. Like you said, with join,
it's a moot point.
> Other than that, the code in main.cgi looked pretty good.
Thanks for your time Tad. I can only hope that if anyone else looks
at my code they will be as helpful as you have been.
--
Dave Hoover
"Twice blessed is help unlooked for." --Tolkien
http://www.redsquirreldesign.com/dave
------------------------------
Date: Fri, 29 Jun 2001 12:51:21 GMT
From: "Lex" <nospam@peng.nl>
Subject: getting a defined value from a html page
Message-Id: <dR__6.151242$u5.2973281@zwoll1.home.nl>
Hi,
I've got a html page with the following written it: <object1>19.1</object>
(for example)
How do I insert in a perl script that it picks out what is in between
<object> and </object> so I can process the value?
Thanks for pointing me in the right direction!
Lex Thoonen
Pêng Smart Web Design
http://www.peng.nl
------------------------------
Date: Fri, 29 Jun 2001 07:01:57 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Help: How to make a hash global
Message-Id: <3B3C6025.83810A1@earthlink.net>
Jenda Krynicky wrote:
[snip]
> Assuming you have a statement
> package printsort;
> in the packagesort.pm add this
> *db = \%main::db;
> somwehere into the module.
>
> After executing this statement $printsort::db is exaclty the same
> thing as $main::db
>
> And remember that %db cannot be "our".
I think that you mean that %db cannot be "my".
>
> You have to use
>
> use vars qw(%db);
> instead of
> our %db;
These two statements have the same effect.
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: Fri, 29 Jun 2001 06:53:05 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: how can i get the output of a forked child?
Message-Id: <3B3C5E11.8CE1789E@earthlink.net>
Thomas Schulze-Velmede wrote:
[snip]
> I now want the father to collect the output of the childs. (In reality
> the forked childs are shellscripts which will get executed by
> Net::Telnet->cmd() on many different hosts, preferable in parallel.)
use IO::Select;
my $s = IO::Select->new;
while( <> ) {
my $pid = open( my $fh, "-|" );
if( $pid ) { $s->add( $fh ) }
elsif( defined $pid ) {
my ($host, $cmd) = /(\S*)\s(.*)/;
my $t = Net::Telnet->new;
$t->open( $host );
print $t->cmd( $cmd );
} else {
die "Couldn't fork: $!\n";
}
}
while( my @ready = $s->can_read ) {
my ($fh) = @ready;
print "From filehandle $fh, I got output:\n";
print while <$fh>;
close $fh or warn "Child died: $?\n";
$s->remove($fh);
}
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: Fri, 29 Jun 2001 18:07:34 +0800
From: "keng" <keng@spinalfluid.com>
Subject: How to install Math-Round on windows
Message-Id: <9hhk0j$pdf$1@coco.singnet.com.sg>
dear all,
i need to use Nearest in the Math-Round module but cannot/ don't know how to
install using ppm on win98. Can anyone help me?
Else pls tell me how i can round decimal numbers up or down.
eg
change 3.2456 to 3.25
change 3.2446 to 3.25 or 3.24
chnage 3.2432 to 3.24
thanks
--
regards
isaac
------------------------------
Date: Fri, 29 Jun 2001 10:33:25 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: passing variables the 'right' way
Message-Id: <72mojtcgrbdr79mqcthp5ao0ok3sqo6bv2@4ax.com>
Jakob Schmidt wrote:
>"Aman Patel" <patelnavin@icenet.net> writes:
>
>> my $var = slurp_a_file; #loads a big file,
>> pass_subroutine( \$var );
>
>Yes, that will pass a reference into the sub (in $_[ 0 ]), which is much
>like passing a pointer in C(\+\+)?
You don't ever need to pass a reference to a scalar for "efficiency
reasons". Using
pass_subroutine( $var );
will make $_[0] an ALIAS to $var. Modify $_[0], and you modify $var.
It's only at the line:
sub pass_subroutine {
my($param) = @_; # or: my $param = shift;
...
}
that an actual COPY is made. So don't do that.
There's a neat little trick for aliasing one scalar: using for() or its
synonym foreach().
sub pass_subroutine {
foreach my $param ($_[0]) {
...
}
}
Passing references to arrays, and especially to hashes, *is* useful, as
passing the parameters flattens the list, and passes an alias to every
array item to the sub. It's worst with hashes, because you flatten the
hash to a list, and storing it into another localized hash will simply
rebuild a new hash, which is a rather slow process.
--
Bart.
------------------------------
Date: Fri, 29 Jun 2001 06:07:41 -0600
From: Howard Brazee <howard@brazee.net>
Subject: Re: Perl from VXML
Message-Id: <3B3C6F8D.4BD93110@brazee.net>
Tad McClellan wrote:
> Howard Brazee <howard@brazee.net> wrote:
>
> >I am trying out some VXML,
>
> What is VXML?
>
> Is it XML of some flavor or another?
Voice - XML
> >calling a Perl script written to be called by
> >HTML.
>
> CGI programs are called by www *servers* (not by browsers).
>
> Which computer your program is running on is an important
> thing to know.
The server is a Sun.
> >I am not familiar with Perl, but the VXML debugger is finding a
> >mismatched tag, leading me to infer that I am receiving some HTML code
> >back.
> >
> >I changed print "<html><body>Unknown Request Method.</html><body>","\n"
> ^^
> ^^
> >;
> >
> > To print "<html><body>Unknown Request
> >Method.<body></html>","\n" ;
> ^^
> ^^
> ^^ </body>
>
> >because XML is fussy about closing inner tags before outer tags.
>
> That can't be the reason, because you _still_ haven't closed
> any <body> sections, you just opened a second one. :-)
>
> >I am not sure how this works.
>
> That will make things hard to troubleshoot :-) :-)
>
> >I am an old CoBOL programmer and this is
> >all new to me.
>
> "this" is actually "these".
>
> Folks just starting with CGI programming are often new to
> several different domains all at once. This can easily
> lead to overload.
>
> My advice it to slow down and try and learn them only one
> or two at a time.
>
> These may not apply to you, if not then just ignore me.
>
> 1) Usenet - what newsgroups exist, netiquette for posting...
>
> 2) Unix - home system is Bill's, ISP's web server in *nix.
>
> 3) Perl - and often new to programming in general too
>
> 4) CGI - programming an application in the CGI environment
>
> Try and learn programming/Perl first. You can do this on your
> home machine without an ISP. Perl is free, just download and
> install it.
>
> First get comfortable writing programs that run from the
> command line before adding the complications of a particular
> application area (CGI).
>
> Then use the CGI module for your CGI programs, and you will be
> able to test it from the command line locally, again with no
> web server involved.
>
> After you get your CGI program working from the command line,
> _then_ is when you need to know the particulars of how your
> web servers is setup. You should ask your provider to tell
> you how they have set things up. Also ask them about access
> to the web server's error logs.
>
> You might also get a free web server to run locally.
>
> There are newsgroups for discussing servers and the CGI
> environment:
>
> comp.infosystems.www.authoring.cgi
> comp.infosystems.www.servers.mac
> comp.infosystems.www.servers.misc
> comp.infosystems.www.servers.ms-windows
> comp.infosystems.www.servers.unix
>
> --
> Tad McClellan SGML consulting
> tadmc@augustmail.com Perl programming
> Fort Worth, Texas
------------------------------
Date: Fri, 29 Jun 2001 06:08:26 -0600
From: Howard Brazee <howard@brazee.net>
Subject: Re: Perl from VXML
Message-Id: <3B3C6FBA.A74A0068@brazee.net>
Tad McClellan wrote:
> Howard Brazee <howard@brazee.net> wrote:
>
> >I am trying out some VXML,
>
> What is VXML?
>
> Is it XML of some flavor or another?
Voice - XML
> >calling a Perl script written to be called by
> >HTML.
>
> CGI programs are called by www *servers* (not by browsers).
>
> Which computer your program is running on is an important
> thing to know.
The server is a Sun.
> >I am not familiar with Perl, but the VXML debugger is finding a
> >mismatched tag, leading me to infer that I am receiving some HTML code
> >back.
> >
> >I changed print "<html><body>Unknown Request Method.</html><body>","\n"
> ^^
> ^^
> >;
> >
> > To print "<html><body>Unknown Request
> >Method.<body></html>","\n" ;
> ^^
> ^^
> ^^ </body>
>
> >because XML is fussy about closing inner tags before outer tags.
>
> That can't be the reason, because you _still_ haven't closed
> any <body> sections, you just opened a second one. :-)
>
> >I am not sure how this works.
>
> That will make things hard to troubleshoot :-) :-)
>
> >I am an old CoBOL programmer and this is
> >all new to me.
>
> "this" is actually "these".
>
> Folks just starting with CGI programming are often new to
> several different domains all at once. This can easily
> lead to overload.
>
> My advice it to slow down and try and learn them only one
> or two at a time.
>
> These may not apply to you, if not then just ignore me.
>
> 1) Usenet - what newsgroups exist, netiquette for posting...
>
> 2) Unix - home system is Bill's, ISP's web server in *nix.
>
> 3) Perl - and often new to programming in general too
>
> 4) CGI - programming an application in the CGI environment
>
> Try and learn programming/Perl first. You can do this on your
> home machine without an ISP. Perl is free, just download and
> install it.
>
> First get comfortable writing programs that run from the
> command line before adding the complications of a particular
> application area (CGI).
>
> Then use the CGI module for your CGI programs, and you will be
> able to test it from the command line locally, again with no
> web server involved.
>
> After you get your CGI program working from the command line,
> _then_ is when you need to know the particulars of how your
> web servers is setup. You should ask your provider to tell
> you how they have set things up. Also ask them about access
> to the web server's error logs.
>
> You might also get a free web server to run locally.
>
> There are newsgroups for discussing servers and the CGI
> environment:
>
> comp.infosystems.www.authoring.cgi
> comp.infosystems.www.servers.mac
> comp.infosystems.www.servers.misc
> comp.infosystems.www.servers.ms-windows
> comp.infosystems.www.servers.unix
>
> --
> Tad McClellan SGML consulting
> tadmc@augustmail.com Perl programming
> Fort Worth, Texas
------------------------------
Date: Fri, 29 Jun 2001 06:37:40 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: reading the first line off the file
Message-Id: <3B3C5A74.8121B65E@earthlink.net>
Anno Siegel wrote:
[snip]
> Yes. Open the mailx command as a pipe (see perldoc open). Then
> print the rest of FILE to the command, it will become its standard
> input:
>
> open MAIL, "| mailx $address -s '$subject'" or die "Can't fork: $!\n";
> print MAIL while <FILE>;
> close MAIL or die "Error running mailx: $?\n";
>
> Note how $subject is enclosed in single quotes in the mailx command,
> it it likely to contain blanks.
It could even contain other things, which might make the shell do rather
unsafe stuff. For instance, if the subject is ";rm -rf /" you'd be in
trouble with the above code.
This would be *much* safer:
defined( my $pid = open MAIL, "|-" ) or die "Can't fork: $!\n";
unless( $pid ) {
exec "mailx", $address, "-s", $subject;
die "Couldn't exec mailx: $!\n";
}
my $blocksize = stat(FILE)[11] || (-s _) || (2**10);
print MAIL while( sysread( FILE, $_, $blocksize ) );
close MAIL or die "Error running mailx: $?\n";
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: Fri, 29 Jun 2001 07:16:49 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: releasing array memory
Message-Id: <3B3C63A1.1B308833@earthlink.net>
nobull@mail.com wrote:
[snip]
> If Perl had proper garbage collection then giving it a defragmentable
> heap would be possible but would mean you couldn't use the pack/unpack
> trick on a refrence to get at the internals of an object.
Which trick is that?
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: 29 Jun 2001 10:09:30 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: replacement -- peculiar interpolation
Message-Id: <9hhk4q$r92$1@mamenchi.zrz.TU-Berlin.DE>
According to Seppo Enarvi <seppo.enarvi@genera.fi>:
> Hello,
>
>
> I'm trying to understand the interpolation occuring in the replacement
> operator.
>
> I have a text file which contains "variables" that need to be replaced with
> their appropriate values. The variables resemble Perl variables; that is,
> they are simply some text of the form $variable_name.
>
> This is how I naturally (*) would do the thing:
>
>
> $string = 'a string containing $variable';
> $variableName = '$variable';
> $variableValue = 123;
> $string =~ s/$variableName/$variableValue/g;
The plan is fine, but take a look at the regular expression you are
actually using: After the contents of $variableName are interpolated,
it amounts to /$variable/. That regex starts with a "$", but "$" in
a regex means "end of string", so it can never match. That is why
the replacement didn't happen.
> However, the script doesn't work; it doesn't replace the variable. I noticed
> that the script works if I change the second line to the following:
>
> $variableName = '\$variable';
Right, because now your regex is effectively /\$variable/ with an
escaped "$". That matches itself and all is well (until the string
in $variableName contains other characters that are meaningful in
a regex).
Perl has a quoting mechanism that takes care of the problem. If you
stick with
$variableName = '$variable';
but build your regex as in
$string =~ s/\Q$variableName/$variableValue/g;
\Q will effectively insert backslashes in front of all characters
that have special meaning in regular expressions (and then some).
The result is that the text in $variableName is matched literally,
and not interpreted as (part of) a regex. See "perldoc -f quotemeta"
(sic) for more.
Using a character less laden with meaning than "$" might have saved
you this particular error, but there are more problems that turn up
with replacements in text templates. There is a module, appropriately
named Text::Template, which presumably (I haven't used it) deals
with this. Take a look.
[rest snipped]
Anno
------------------------------
Date: Fri, 29 Jun 2001 20:53:14 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: Scanning a file in CGI
Message-Id: <rlZ_6.2$N02.757@vic.nntp.telstra.net>
"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:9hfqv7$k43$1@mamenchi.zrz.TU-Berlin.DE...
> [please don't top-post]
>
> According to Jim Melanson <jim@perlservices.com>:
> > Hi,
>
>
> There is absolutely *no* advantage in reading the whole file into
> memory at once. Why do you propose it?
>
I disagree with that. In many instances I have seem significant performance
improvements by doing this because you reduce the number of disk acesses.
this is not alwys the case, as it depends on on file size and system
overhead among other things. it also carries with it the risk of eating up
huge chunks of RAM if your fils sizes are large.
But try a 1-2 Mb file read and process with the line by line method compared
to the read all into an array method, and you can often see signinficant
performance improvements, at the cost of system resources. But if you have
the resources to spare, it can be an advantage to do this, as long as you
are careful of the caveats.
I do agree with all your other comments though.
I should also add, that suggesting a change to the @lines = <IN> method
should not be sugested as the fix to a potential problem without a warning
about the potential large memory consumption.
Imagine reading a 250 Mb file like that! However, for files in the range of
512k - 7 or 8 Mb I see no real problem (a bit system dependent though - if
you have 1 Gb of RAM on a single user machine, slurp the whole 250 Mb file
and watch your performance improve. Do that on a heavily loaded server with
only 128 Mb of RAM and watch your account priviliges get revoked!)
Wyzelli
------------------------------
Date: 29 Jun 2001 11:34:32 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Scanning a file in CGI
Message-Id: <9hhp48$1se$1@mamenchi.zrz.TU-Berlin.DE>
According to Wyzelli <wyzelli@yahoo.com>:
> "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
> news:9hfqv7$k43$1@mamenchi.zrz.TU-Berlin.DE...
> > [please don't top-post]
> >
> > According to Jim Melanson <jim@perlservices.com>:
> > > Hi,
> >
> >
> > There is absolutely *no* advantage in reading the whole file into
> > memory at once. Why do you propose it?
> >
>
> I disagree with that. In many instances I have seem significant performance
> improvements by doing this because you reduce the number of disk acesses.
> this is not alwys the case, as it depends on on file size and system
> overhead among other things. it also carries with it the risk of eating up
> huge chunks of RAM if your fils sizes are large.
[details on space/time tradeoff]
Performance, okay. But that would belong in a later stage of coding,
and only if this particular IO is indeed a bottleneck. What I meant
was really that there is no algorithmic advantage in having all lines
present when all you do is work on each line at a time. Even that
isn't quite true, because you can chomp all the lines in one go (as
the OP did) while I had to call chomp on each line :)
I am getting pretty allergic against the mindless file-slurping
that we get to see left and right. There are people who believe
that this is how you do it in Perl and don't know another way.
One program presented here a year ago or so went basically like this:
my @lines = <FILE>;
$document = join '', @lines;
$document =~ s/.../.../; # no /s or /m
print @document;
He must have had a pretty roomy machine; his program broke when
the file went above 1.7 GB.
Shoot file slurping on sight, I say.
Anno
------------------------------
Date: Fri, 29 Jun 2001 21:47:29 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: Scanning a file in CGI
Message-Id: <i8__6.3$N02.526@vic.nntp.telstra.net>
"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:9hhp48$1se$1@mamenchi.zrz.TU-Berlin.DE...
> According to Wyzelli <wyzelli@yahoo.com>:
> > "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
> > news:9hfqv7$k43$1@mamenchi.zrz.TU-Berlin.DE...
> > > [please don't top-post]
> > >
> > > According to Jim Melanson <jim@perlservices.com>:
> > > > Hi,
> > >
> > >
> > > There is absolutely *no* advantage in reading the whole file into
> > > memory at once. Why do you propose it?
> > >
> >
> > I disagree with that. In many instances I have seem significant
performance
> > improvements by doing this because you reduce the number of disk
acesses.
> > this is not alwys the case, as it depends on on file size and system
> > overhead among other things. it also carries with it the risk of eating
up
> > huge chunks of RAM if your fils sizes are large.
>
> [details on space/time tradeoff]
>
> Performance, okay. But that would belong in a later stage of coding,
> and only if this particular IO is indeed a bottleneck. What I meant
> was really that there is no algorithmic advantage in having all lines
> present when all you do is work on each line at a time. Even that
> isn't quite true, because you can chomp all the lines in one go (as
> the OP did) while I had to call chomp on each line :)
>
> I am getting pretty allergic against the mindless file-slurping
> that we get to see left and right. There are people who believe
> that this is how you do it in Perl and don't know another way.
Yes well I certainly concur. There are often pros and cons and that method
tends to be presented as the solution rather than an alternative.
> One program presented here a year ago or so went basically like this:
>
> my @lines = <FILE>;
> $document = join '', @lines;
> $document =~ s/.../.../; # no /s or /m
> print @document;
>
> He must have had a pretty roomy machine; his program broke when
> the file went above 1.7 GB.
Indeed. would break earlier than that on most of mine.
> Shoot file slurping on sight, I say.
Maybe not quite, but I agree pretty much with the motivation.
Wyzelli
--
#Modified from the original by Jim Menard
for(reverse(1..100)){$s=($_==1)? '':'s';print"$_ bottle$s of beer on the
wall,\n";
print"$_ bottle$s of beer,\nTake one down, pass it around,\n";
$_--;$s=($_==1)?'':'s';print"$_ bottle$s of beer on the
wall\n\n";}print'*burp*';
------------------------------
Date: Fri, 29 Jun 2001 10:31:24 +0000
From: gnari <gnarinn@hotmail.com>
Subject: Re: Search script: passing information between programs?
Message-Id: <993810684.548597217071801.gnarinn@hotmail.com>
In article <c7d9d63c.0106280922.1ea3b58@posting.google.com>,
Alex <samara_biz@hotmail.com> wrote:
>
>I am implementing a search script for a web site. I am not sure how to
>pass results returned from my search script (which is just a program
>on a Linux box) to a cgi script. I guess one way would be to write
>them in a file and then read the file from the cgi script, but in this
>case I would have to have a unique name for each file I'm writing to
>avoid a race condition on files. (Or lock the file during read/write,
>but that's more complicated). Is that the best way to do this? Can
>someone maybe advise me on a better way of doing this?
>
there are many ways to create unique temporary filenames
for example:
$tmp="/tmp/myapp_.$$.".time();
for some ideas, see perldoc -q temporary
also you can have your search script just output the results
on STDOUT and call it with backtics:
$result=`$myprog $args`;
or
@results=$myprog $args;
or you could implement the search (if it is in Perl)
in a module and 'use' it.
this should be more effective, specially if you use mod_perl
gnari
------------------------------
Date: Fri, 29 Jun 2001 12:21:30 +0200
From: "felix" <mr.thanquol@gmx.de>
Subject: starting with perl
Message-Id: <9hhkpc$13f$1@crusher.de.colt.net>
hi,
I just started with perl and need some help with regexps ....
it's the following situation
if got a file with line which look like this
[28.06.01 14:08:03] [info] root@localhost telnet: Session timed out!
and I want to change every line to look like this
<tr><td>28.06.01</td><td>14:08:03</td><td>info</td><td>root</td><td>localhos
t</td><td>telnet</td><td>Session timed out!</td>
so I do something like
while (<>) {
if ( $_ =~ *some_regexp*) {
if ( $_ =~ *some_more_regexp*) {
print $_
if ( $_ =~ *some_regexp*) {
print $_
}
}
print $_
}
else print $_
}
but I don't no what kind of regexp to use ...
thx for every piece of help
felix
------------------------------
Date: Fri, 29 Jun 2001 10:49:45 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: starting with perl
Message-Id: <3B3C5D8A.58DAF8FD@acm.org>
felix wrote:
>
> hi,
>
> I just started with perl and need some help with regexps ....
>
> it's the following situation
>
> if got a file with line which look like this
>
> [28.06.01 14:08:03] [info] root@localhost telnet: Session timed out!
>
> and I want to change every line to look like this
>
> <tr><td>28.06.01</td><td>14:08:03</td><td>info</td><td>root</td><td>localhos
> t</td><td>telnet</td><td>Session timed out!</td>
>
> so I do something like
>
> while (<>) {
>
> if ( $_ =~ *some_regexp*) {
> if ( $_ =~ *some_more_regexp*) {
> print $_
> if ( $_ =~ *some_regexp*) {
> print $_
> }
>
> }
> print $_
> }
> else print $_
> }
while ( <> ) {
s{^\[(\d+\.\d+\.\d+)\s+(\d+:\d+:\d+)]\s+\[(\S+)]\s+(\S+)\@(\S+)\s+(\S+)\s+(.*)$}
{<tr><td>$1</td><td>$2</td><td>$3</td><td>$4</td><td>$5</td><td>$6</td><td>$7</td>};
}
John
--
use Perl;
program
fulfillment
------------------------------
Date: Fri, 29 Jun 2001 13:47:46 +0200
From: "felix" <mr.thanquol@gmx.de>
Subject: Re: starting with perl
Message-Id: <9hhpr2$3gl$1@crusher.de.colt.net>
THX!!!!!
"John W. Krahn" <krahnj@acm.org> wrote in message
news:3B3C5D8A.58DAF8FD@acm.org...
> felix wrote:
> >
> > hi,
> >
> > I just started with perl and need some help with regexps ....
> >
> > it's the following situation
> >
> > if got a file with line which look like this
> >
> > [28.06.01 14:08:03] [info] root@localhost telnet: Session timed out!
> >
> > and I want to change every line to look like this
> >
> >
<tr><td>28.06.01</td><td>14:08:03</td><td>info</td><td>root</td><td>localhos
> > t</td><td>telnet</td><td>Session timed out!</td>
> >
> > so I do something like
> >
> > while (<>) {
> >
> > if ( $_ =~ *some_regexp*) {
> > if ( $_ =~ *some_more_regexp*) {
> > print $_
> > if ( $_ =~ *some_regexp*) {
> > print $_
> > }
> >
> > }
> > print $_
> > }
> > else print $_
> > }
>
> while ( <> ) {
>
>
s{^\[(\d+\.\d+\.\d+)\s+(\d+:\d+:\d+)]\s+\[(\S+)]\s+(\S+)\@(\S+)\s+(\S+)\s+(.
*)$}
>
>
{<tr><td>$1</td><td>$2</td><td>$3</td><td>$4</td><td>$5</td><td>$6</td><td>$
7</td>};
>
> }
>
>
> John
> --
> use Perl;
> program
> fulfillment
------------------------------
Date: Fri, 29 Jun 2001 08:13:06 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: starting with perl
Message-Id: <3B3C70D2.EA65C30D@earthlink.net>
felix wrote:
>
> hi,
>
> I just started with perl and need some help with regexps ....
>
> it's the following situation
>
> if got a file with line which look like this
>
> [28.06.01 14:08:03] [info] root@localhost telnet: Session timed out!
>
> and I want to change every line to look like this
>
> <tr><td>28.06.01</td><td>14:08:03</td><td>info</td><td>root</td>
> <td>localhost</td><td>telnet</td><td>Session timed out!</td>
>
while( my $line = <> ) {
chomp $line;
my @fields = $line =~ m[
\[(.{8})\s(.{8})\]\s # [date time]
\[([^]]*)\]\s # [cwd]
([^\@]*)\@(\S*)\s # id@host
([^:]*):\s # program:
(.*) # errormsg
]x or warn "Malformed line: $line\n";
print "<tr>";
print "<td>$_</td>" for (@fields);
print "</tr>";
}
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: Fri, 29 Jun 2001 20:25:21 +1000
From: Thomas <oeser@enternet.com.au>
Subject: Textarea to file
Message-Id: <3B3C5791.512A@enternet.com.au>
Hi,
I would like to send the contents of a textarea to a file (html).
The following code almost does it but it doesn't keep all of the
html tags intact.
>>>>>
#!/usr/local/bin/perl
while (<>){
if (/MyForm=/){
@url = split(/&/);
foreach (@url){
tr/+/ /;
s/=//;
s/%27/'/g;
s/%3A/:/g;
s/%2F/\/g/;
s/%0D%0A/<BR>/g;
s/%2C/,/g;
s/%2B/+/g;
s/%3D/=/g;
s/%92/’/g;
s/%3F/?/g;
s/%22/"/g;
s/myForm//;
}
open(MYOUTFILE, ">>../filename.html");
print MYOUTFILE $url[0];
close(MYOUTFILE);
}
}
>>>>>>
Is there a simpler way of doing this?
Thanks in advance,
Thomas
------------------------------
Date: Fri, 29 Jun 2001 11:34:14 GMT
From: troll@gimptroll.com (TuNNe|ing)
Subject: Re: Textarea to file
Message-Id: <3b3c8170.37427125@news>
Where is your s/// to "recreate" _>_ and _<_ ?
This is some EDITED sample code I got from Perl 5 by Example by David
Medinets.
It stores all of the form elements in a hash called %frmFlds.
So if your TEXTAREA was called "textarea" the hash you would print to
a file would be $frmFlds{textarea}.
sub getFormData {
my($hashRef) = shift;
my($buffer) = "";
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$buffer = $ENV{'QUERY_STRING'};
}
else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}
foreach (split(/&/, $buffer)) {
my($key, $value) = split(/=/, $_);
$key = decodeURL($key);
$value = decodeURL($value);
$value =~ s!\n!<br>!g;#Convert newlines into <br>
%{$hashRef}->{$key} = $value;
}
}
sub decodeURL {
$_ = shift;
tr/+/ /;
s/%(..)/pack('c', hex($1))/eg;
return($_);
}
Jason
~ "The only time I'm happy is in my memories"
------------------------------
Date: Fri, 29 Jun 2001 08:00:53 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Textarea to file
Message-Id: <3B3C6DF5.9B51E6A8@earthlink.net>
#!/usr/local/bin/perl -wT
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
open my $outfile, ">>", "/full/path/to/filename.htmlfragment"
or die "Could not open filename.txt for writing: $!";
# always, yes, always, check the return value of open.
my $htmlstuff = param("textAreaName");
my %tr = qw( < < & & );
$htmlstuff =~ s/([<&])/$tr{$1}/g;
$htmlstuff =~ s/\015\012|\012\015?/<br>\n/g;
print $outfile $htmlstuff;
close $outfile;
print header, begin_html("ok"), h1("Got it and wrote it."), end_html;
__END__
The module CGI.pm exists for a reason. Use it unless you have a d**m
good reason not to.
You should use full names for cgi programs, unless you *know* what the
cwd will be when the program is run by the web server.
Also, always use -w, always use strict, always check the return value of
open, and [if running as a cgi] always use -T.
And of course, your CGI program ought to print *something* back to the
browser.
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: Fri, 29 Jun 2001 14:38:18 +0200
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: Textarea to file
Message-Id: <Pine.LNX.4.30.0106291409070.17294-100000@lxplus003.cern.ch>
On Fri, 29 Jun 2001, Benjamin Goldberg wrote:
[...]
> use CGI;
[...]
> my %tr = qw( < < & & );
> $htmlstuff =~ s/([<&])/$tr{$1}/g;
> $htmlstuff =~ s/\015\012|\012\015?/<br>\n/g;
> print $outfile $htmlstuff;
[...]
> The module CGI.pm exists for a reason. Use it unless you have a d**m
> good reason not to.
Indeed. So why not use it to the full, instead of hand-coding the
HTML-ifying stuff?
Anyhow, it's not clear to me that you have clearly defined what it is
that you're aiming to implement. If you want to produce HTML which
when rendered would best represent the textarea input, then then it
might be a good idea to put it inside <pre>, and let the newlines
pretty-much take care of themselves. Turning multiple newlines into
multiple <br> tags is meaningless: once a line is broken in HTML it is
broken, breaking it again doesn't mean anything definite (yes, I do
know what the popular wowsers do in response to that, but I consider
it to be an error fixup rather than anything that is guaranteed by the
spec). But the details would be better addressed on a CGI or HTML
group.
By the way, your code also falls foul of a security bug in X Netscape
4 versions, which recent-ish versions of CGI.pm would have protected
you from.
> You should use full names for cgi programs, unless you *know* what the
> cwd will be when the program is run by the web server.
You don't "know" the cwd, unless you set it explicitly yourself,
because the CGI specification doesn't mandate what it's to be. It
would be foolish to put yourself at the mercy of something not
mandated by the spec. Anyway, your -T recommendation will also come
into play in this regard.
> Also, always use -w, always use strict, always check the return value of
> open, and [if running as a cgi] always use -T.
Good advice, although you might do well, after debugging is done and
you're putting it into production, to make sure that any details of
error responses don't appear in the browser display, as they could
expose points of weakness to a potential attacker.
> And of course, your CGI program ought to print *something* back to the
> browser.
Your CGI program _must_ print a valid CGI response back to the server,
which will use it as the basis for sending a valid HTTP response back
to the client.
------------------------------
Date: Fri, 29 Jun 2001 12:12:42 +0200
From: "Amittai Aviram" <amittai@amittai.com>
Subject: Re: What kind of object is an old-fashioned DBM?
Message-Id: <9hhke6$o9i$1@rznews2.rrze.uni-erlangen.de>
"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:9hfcpg$8aa$2@mamenchi.zrz.TU-Berlin.DE...
> According to Amittai Aviram <amittai@amittai.com>:
> > I was trying to change my scripts that had used dbmopen() and dbmclose()
to
> > use tie() instead. Tie requires the name of an object. I
To which Anno Siegel replied:
> What's the problem? You use whatever flavor of DB was used creating
> them. If you don't know, under Unix the file command may help.
?? file dbmopen --> No such command.
file db --> No such command.
dbmopen is not a Unix command, AFAIK, it is a Perl function. That is why I
am asking this question. My impression is that dbmopen was developed when
there was one kind of DBM format, and that later modules extended the range
of possible DBMs. So I still wanted to know how to refer to one of the
original dbmopen-type DBMs using tie(). dbmopen and dbmclose still work, of
course, so it does not matter all that much, but it's something I wanted to
know.
Amittai Aviram
------------------------------
Date: 29 Jun 2001 10:48:00 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: What kind of object is an old-fashioned DBM?
Message-Id: <9hhmd0$t5v$1@mamenchi.zrz.TU-Berlin.DE>
According to Amittai Aviram <amittai@amittai.com>:
>
> "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
> news:9hfcpg$8aa$2@mamenchi.zrz.TU-Berlin.DE...
> > According to Amittai Aviram <amittai@amittai.com>:
> > > I was trying to change my scripts that had used dbmopen() and dbmclose()
> to
> > > use tie() instead. Tie requires the name of an object. I
>
> To which Anno Siegel replied:
>
> > What's the problem? You use whatever flavor of DB was used creating
> > them. If you don't know, under Unix the file command may help.
>
> ?? file dbmopen --> No such command.
> file db --> No such command.
Hmm... apparently you don't have the file command, or it isn't on
your path. It is supposed to tell you the type of a file, for
example for a GDBM_File-created database:
[anno4000@lublin ~/clpm]$ file /tmp/somedb
/tmp/somedb: GNU dbm 1.x or ndbm database, little endian
> dbmopen is not a Unix command, AFAIK, it is a Perl function.
Correct.
> That is why I
> am asking this question. My impression is that dbmopen was developed when
> there was one kind of DBM format, and that later modules extended the range
> of possible DBMs.
I don't know about the early history of dbmopen(), but currently
that is not the case. A look at perldoc -f dbmopen shows:
This binds a dbm(3), ndbm(3), sdbm(3), gdbm(3), or
Berkeley DB file to a hash. HASH is the name of
so it handles all the common cases.
It also says:
You can control which DBM library you use by
loading that library before you call dbmopen():
use DB_File;
dbmopen(%NS_Hist, "$ENV{HOME}/.netscape/history.db")
or die "Can't open netscape history file: $!";
Quite a few questions are answered there, wouldn't you say? :)
I am not sure (and haven't read the perldoc in sufficient detail to
know) how the type of database is chosen without a prior "use"
statement.
> So I still wanted to know how to refer to one of the
> original dbmopen-type DBMs using tie(). dbmopen and dbmclose still work, of
> course, so it does not matter all that much, but it's something I wanted to
> know.
The only problem I can see is determining what kind of database
we're looking at. If none of the above helps, there is still
trial-and-error (Ilya will kill me for that).
Anno
------------------------------
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 1217
***************************************