[15563] in Perl-Users-Digest
Perl-Users Digest, Issue: 2976 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon May 8 09:31:46 2000
Date: Mon, 8 May 2000 06:10:13 -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: <957791413-v9-i2976@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Mon, 8 May 2000 Volume: 9 Number: 2976
Today's topics:
methods reading in external file bluesrift@aol.com
Re: methods reading in external file (Eric Bohlman)
Re: methods reading in external file <billy@arnis-bsl.com>
more regexp madness extracting data from files. <nospam@devnull.com>
obviously missing something simple.... <dotechin@dingoblue.net.au>
Re: obviously missing something simple.... (Eric Bohlman)
Perl and JavaScript combination please help..... <KnightSky1@yahoo.com>
Re: PerlDoc (whatever) bug in win32 <gellyfish@gellyfish.com>
Re: problems with $/ = "\r\n" (Windows text files and $ <ter@my-deja.com>
Re: problems with $/ = "\r\n" (Windows text files and $ (Bart Lateur)
Proper use of resources (was Re: more regexp madness ex (Randal L. Schwartz)
Re: Quality of perl implementations <nobody@internetrobotics.com>
renaming files <jspidel@bellsouth.net>
Re: Socket Modual (Eric Bohlman)
Re: Suggest 'use Carp' to modify the messages of -w <nospam@devnull.com>
Video comes in bursts jlucande@my-deja.com
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 08 May 2000 06:59:40 GMT
From: bluesrift@aol.com
Subject: methods reading in external file
Message-Id: <8f5oko$anp$1@nnrp1.deja.com>
Is it safe to use $_ in such a way as in the code below? Should I, can
I, need I empty $_ to "" first or declare it private to the sub?
i.e. my $_="";
or $content .= my $_ while <FILE>;
sub get_afile {
if (open FILE, "html/afile.txt") {
$content .= $_ while <FILE>;
close FILE;
}
else {$content .= "<html><body>An error has occurred opening the
page, please try to reload<br><br>";}
}
To any of you who have the tools in place and have tested various
techniques, would the above perform more quickly or produce less burden
on a server than the code below? It seems the above would require at
least less memory, but there may be other factors to consider?
sub get_afile {
if (open FILE, "html/afile.txt") {
my @lines = <FILE>;
close FILE;
my ($line);
foreach $line (@lines) {
$content .= $line;
}
}
else {$content .= "<html><body>An error has occurred opening the
page, please try to reload<br><br>";}
}
Thanks!
Rob Bell
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 8 May 2000 09:43:20 GMT
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: methods reading in external file
Message-Id: <8f627o$6bv$1@slb1.atl.mindspring.net>
bluesrift@aol.com wrote:
: Is it safe to use $_ in such a way as in the code below? Should I, can
: I, need I empty $_ to "" first or declare it private to the sub?
: i.e. my $_="";
: or $content .= my $_ while <FILE>;
:
: sub get_afile {
: if (open FILE, "html/afile.txt") {
: $content .= $_ while <FILE>;
: close FILE;
: }
: else {$content .= "<html><body>An error has occurred opening the
: page, please try to reload<br><br>";}
: }
There's no need to initialize $_ to anything, as whatever it held before
gets completely overwritten by reading a line. Localizing it
*is* a good idea, since it is rather rude behavior for a subroutine to
stomp on $_ without heavily advertising that fact. However, Perl
built-in variables like $_ are globals, not lexicals, and thus you need
to localize them with local rather than my (in fact, unless you're doing
quite advanced stuff, built-ins are the *only* variables you should ever
use local on).
:
: To any of you who have the tools in place and have tested various
: techniques, would the above perform more quickly or produce less burden
: on a server than the code below? It seems the above would require at
: least less memory, but there may be other factors to consider?
:
: sub get_afile {
: if (open FILE, "html/afile.txt") {
: my @lines = <FILE>;
: close FILE;
: my ($line);
: foreach $line (@lines) {
: $content .= $line;
: }
: }
: else {$content .= "<html><body>An error has occurred opening the
: page, please try to reload<br><br>";}
: }
The former code is less wasteful than the latter, since it doesn't have
to keep two separate copies of everything around. However, there are
more efficient ways to read an entire file in the string than trying to
do it line-by-line; there were a couple threads on such ways in the last
month; try a Deja search.
------------------------------
Date: Mon, 08 May 2000 10:16:37 GMT
From: Ilja <billy@arnis-bsl.com>
Subject: Re: methods reading in external file
Message-Id: <8f645t$n8i$1@nnrp1.deja.com>
In article <8f5oko$anp$1@nnrp1.deja.com>,
bluesrift@aol.com wrote:
> Is it safe to use $_ in such a way as in the code below? Should I, can
> I, need I empty $_ to "" first or declare it private to the sub?
> i.e. my $_="";
> or $content .= my $_ while <FILE>;
>
> sub get_afile {
> if (open FILE, "html/afile.txt") {
> $content .= $_ while <FILE>;
> close FILE;
> }
> else {$content .= "<html><body>An error has occurred opening the
> page, please try to reload<br><br>";}
> }
>
> To any of you who have the tools in place and have tested various
> techniques, would the above perform more quickly or produce less burden
> on a server than the code below? It seems the above would require at
> least less memory, but there may be other factors to consider?
>
> sub get_afile {
> if (open FILE, "html/afile.txt") {
> my @lines = <FILE>;
> close FILE;
> my ($line);
> foreach $line (@lines) {
> $content .= $line;
> }
> }
> else {$content .= "<html><body>An error has occurred opening the
> page, please try to reload<br><br>";}
> }
>
> Thanks!
> Rob Bell
I guess you simply need to read the whole content of file into $content.
IMHO the simpliest (and most efficient way) is:
undef $/;
my $content = <FILE>;
Please consult 'perldoc perlvar', section about $/ variable.
Ilja.
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 8 May 2000 10:00:45 GMT
From: The WebDragon <nospam@devnull.com>
Subject: more regexp madness extracting data from files.
Message-Id: <8f638d$elj$0@216.155.32.56>
given the following
- - -
I'll be using File::Spec to make sure the paths to/from files stay
intact cross-platform.
6 files (text/html) that all contain a large block of data of the form:
the list of files to be checked will be stored in @inputFiles
maps[i++] = new Map("albundyvp","AlBundyVP",1350,"-1",-1);
maps[i++] = new Map("alienspt1vp","AlienSPT1VP",3257,"-1",-1);
maps[i++] = new Map("alornmappack","AlornMappack",1956,"-1",-1);
maps[i++] = new Map("amwaf","AMWAF",22,"-1",-1);
maps[i++] = new Map("austinpowersvp","AustinPowersVP",1033,"-1",-1);
maps[i++] = new Map("b2chosenvp","Blood2-ChosenVP",6219,"-1",-1);
maps[i++] = new Map("camocow","Camo Cow Skin",534,"-1",-1);
I want to open each file in succession, extract ONLY the information
between the () 's on any line starting with 'maps[i++]' and append it to
an output file created with today's date and time as the filename, of
the form YYYY.MM.DD.HH.mm.txt (HH.mm being 24-hour time)
each line will be it's own 'record', comma delimited, terminated by \n
the output file will be called maps_database.txt
each block of records belonging to one of the original 6 files, will be
preceded by
name_of_the_file.html\n
(probably surrounded by some sort of delimiter one can easily test for
when one wishes to separate the data into it's component original files
again after performing some manipulation, editing, sorting, output,
whatever, of the data.)
maybe a BEGIN DATA and END DATA block wrapping the data, but I'm not
sure how to best write this..
- - -
what would be the most elegant and efficient means to accomplish this in
perl?
I'm reasonably certain I can figure out the rest, but it's getting the
input/extraction all straightened out that I need help with, still being
somewhat of a perl newbie. :)
--
send mail to mactech (at) webdragon (dot) net instead of the above address.
this is to prevent spamming. e-mail reply-to's have been altered
to prevent scan software from extracting my address for the purpose
of spamming me, which I hate with a passion bordering on obsession.
------------------------------
Date: Mon, 08 May 2000 19:48:31 +1000
From: Martin Phillips <dotechin@dingoblue.net.au>
Subject: obviously missing something simple....
Message-Id: <B53CCA8E.3200%dotechin@dingoblue.net.au>
Can anyone tell me why this bit of code just doesn't work. It goes through
and does everything exactly as it should, except that the selected image is
not deleted. I have looked it and it seems to be right as far as I can
tell.......
Thanks,
Martin
sub delete_image_1
{
print <<__HTML__;
<FORM method="POST" action="$CGIURL/$PROGNAME">
<INPUT type="HIDDEN" name="image" value="$fields{'image'}">
<P align=center>Are you sure you want to delete this image?</P>
<P align=center><a
href="$BASEURL/images/$fields{'image'}">$fields{'image'}</a></P>
<center><INPUT type="submit" name="action" value="Delete This
Image"></center>
</FORM>
__HTML__
}
sub delete_image_2
{
$imgfile = "$BASEDIR/images/$fields{'image'}";
unlink $imgfile;
print "<P align=center>The image <B>$fields{'image'}</B> has been
successfully deleted.";
}
------------------------------
Date: 8 May 2000 10:01:32 GMT
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: obviously missing something simple....
Message-Id: <8f639s$6bv$3@slb1.atl.mindspring.net>
Martin Phillips (dotechin@dingoblue.net.au) wrote:
: Can anyone tell me why this bit of code just doesn't work. It goes through
: and does everything exactly as it should, except that the selected image is
: not deleted. I have looked it and it seems to be right as far as I can
: tell.......
: sub delete_image_2
: {
: $imgfile = "$BASEDIR/images/$fields{'image'}";
: unlink $imgfile;
: print "<P align=center>The image <B>$fields{'image'}</B> has been
: successfully deleted.";
: }
unlink() will tell you if it failed, and will tell you *why* it failed,
if only you ask it to. Why not do so?
I hope that somewhere else in your code you're doing some sanity checking
on $fields{'image'} before passing it to unlink(); remember that anyone
can hack your form or telnet to your script and supply whatever value
they want for that field and that the value can contain "../" sequences;
you wouldn't want to give outsiders the ability to delete any file in
your directory tree, now would you?
------------------------------
Date: Mon, 8 May 2000 12:02:13 +0100
From: "Mark" <KnightSky1@yahoo.com>
Subject: Perl and JavaScript combination please help.....
Message-Id: <39169eff.0@nnrp1.news.uk.psi.net>
I'm trying to get some perl to work on a Javascript If
statement, i need to get one of the JavaScript variables into
the perl code..any ideas??
heres the code anyways:
<script language="JavaScript">
<!--
var bob;
var bob2 = 4;
if (bob2 = 5) {
document.write(bob2);
document.write("<br>if test succesful");
}
else {
document.write("else test succesful<br>");
document.write("#BEGIN SaveTLE("test", +bob2+); #END");
}
-->
</script>
If you dont know, SaveTLE is being used in Intershop3 the
JavaScript variable i'm trying to call is 'bob' or +bob+ in the Perl code.
Thanks all in advance.If you wanna mail to me try: KnightSky1@yahoo.com
------------------------------
Date: 7 May 2000 20:36:34 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: PerlDoc (whatever) bug in win32
Message-Id: <8f4gk2$ikj$1@orpheus.gellyfish.com>
On Sun, 07 May 2000 12:25:04 GMT Steve A. Taylor wrote:
> What is causing this bug? All the recommendations to perldoc this or
> that on Win95b, perl 5.005_03 build 519, get an error message:
>
> C:\BATCH>perldoc Net::FTP
>
> Can't call method "getline" on unblessed reference at
> D:/perl/site/lib/Pod/Input Objects.pm line 149.
>
You have a misinstalled Perl. I would install the latest and greatest
in the first instance and then see if this works.
/J\
--
Whenever Marge turns on one of her non-violent programs, I take a walk. I
go to a bar, I pound a few, then I stumble home in the mood for looooove.
--
fortune oscar homer
------------------------------
Date: Mon, 08 May 2000 10:10:31 GMT
From: Tim Richardson <ter@my-deja.com>
Subject: Re: problems with $/ = "\r\n" (Windows text files and $INPUT_RECORD_SEPARATOR)
Message-Id: <8f63qg$n63$1@nnrp1.deja.com>
In article <8ev73q$ipt$1@nnrp1.deja.com>,
Paul S R Chisholm <psrchisholm@my-deja.com> wrote:
> Consider the following Perl program that tries to use $/ (a.k.a. $RS,
> a.k.a. $INPUT_RECORD_SEPARATOR):
>
What build of Perl are you using? ActiveState's? You should not have to
worry about $/ just because you are on Win32, it is handled magically by
Perl.
--
Tim Richardson
(search string: qweeblebeast)
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Mon, 08 May 2000 11:33:25 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: problems with $/ = "\r\n" (Windows text files and $INPUT_RECORD_SEPARATOR)
Message-Id: <3917a5ad.14160818@news.skynet.be>
Paul S R Chisholm wrote:
>Consider the following Perl program that tries to use $/ (a.k.a. $RS,
>a.k.a. $INPUT_RECORD_SEPARATOR):
>
> use strict;
> $^W = 1; # perl -w
>
> $/ = "\r\n" if $^O eq 'MSWin32';
>
> while (<>) {
> print;
> }
>
>The goal was to have <> and chomp handle end-of-line appropriately for
>Windows text files.
Don't do it. Unless you do binmode() on the input file handle, Perl will
convert CR+LF into "\n" (=LF) for you, and convert it back on output
(unless, again, you used binmode() on the output handle).
--
Bart.
------------------------------
Date: 08 May 2000 05:23:38 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Proper use of resources (was Re: more regexp madness extracting data from files.)
Message-Id: <m1vh0ptelx.fsf@halfdome.holdit.com>
>>>>> "The" == The WebDragon <nospam@devnull.com> writes:
The> what would be the most elegant and efficient means to accomplish this in
The> perl?
Well, I'd start by writing *something* that at least attempts to do
the problem. Where I got stuck, I'd stare at the manuals for a while,
and if I got really stuck, I'd turn to my buddies, and if they didn't
know, I'd pop up at a local PerlMongers meeting (find one or start one
via www.pm.org), and then if none of that worked, I pose a well-formed
question (including relevant snippets of code, not the whole program)
to comp.lang.perl.misc or comp.lang.perl.moderated, including a return
email address so that short replies or interaction can be had directly
with me in private.
After getting a working program, I'd continue to review relevant
documentation, and perhaps use benchmarking to see where it was
spending all of its time, maybe even writing parts in C to get the
most efficiency, although that's often not necessary.
Since you haven't done any of that yet, it's gonna be a long road
to travel.
CLP.M* is just a resource, but more of a "I've done my own thinking
and checked everywhere else" resource. Please don't abuse it. Post
with a valid email address (spamblocked if you must). If you had done
that, I wouldn't need to needle you in public - this could have been a
private message.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: 8 May 2000 04:10:08 GMT
From: Mike Heins <nobody@internetrobotics.com>
Subject: Re: Quality of perl implementations
Message-Id: <8f5en002fl8@news1.newsguy.com>
In comp.lang.perl.moderated Ilya Zakharevich <ilya@math.ohio-state.edu> wrote:
> Perl is absolutely fine as a scripting language. Perl is pretty unusable as
> a programming language.
I will labor along under my delusions, if you don't mind. I have been
using Perl for my non-programming for quite some time. I know that
other pronouncements are often made:
1. You shouldn't use Perl for large applications.
I have managed to produce a program that is fairly large, is in
fairly wide use, and is entirely (or almost) in Perl.
2. You can't use signals in Perl.
On BSD and derivatives, I agree. On Solaris and Linux and other
SYS5-ish OS, Perl seems to do pretty well. Failure rates
on busy systems that are not that much different than the rate
of parity errors in RAM of a few years ago. (Do parity errors
still happen? Do we have cosmic ray shields now? 8-)
3. You can't program a long-running daemon in Perl.
My daemon routinely runs for months at a time. It does
have the luxury of forking for most of its real work, but
it accumulates hours of used CPU with no memory leaks.
Had I listened to the unoffical information, I might not have
done it. And it works just fine in the real world.
Regards,
Mike Heins
--
Internet Robotics, 131 Willow Lane, Floor 2, Oxford, OH 45056
phone +1.513.523.7621 fax 7501 <mikeh@minivend.com>
Function in chaos, finish in style. -- Unknown
------------------------------
Date: Mon, 08 May 2000 09:02:35 -0400
From: "Jeffrey S. Pidel" <jspidel@bellsouth.net>
Subject: renaming files
Message-Id: <3916BAEB.CF5F28D@bellsouth.net>
I am using ActiveState's Perl 5.005_03 on a Windows NT server using
a cgi script and part of the script is to rename or move a file.
Occasionally someone has opened that file read only and the move or
rename will fail. I know I can determine this by the return value but
I was curious if there is another to determine on a Windows NT using
Perl if a file is being accessed by another program. I know that the
Window's NT System Monitor task that it will list files that are in
use. Is there a way in Perl to access the same information.
Thank you!
--
Jeffrey S. Pidel GIW Industries,Inc.
Computer System Engineer Engineering Department
mailto:jspidel@bellsouth.net 5000 Wrightsboro Road.
706 863-1011 ext. 2242 (VOICE) Grovetown, GA 30813
706 868-8025 (FAX) http://www.giwindustries.com
------------------------------
Date: 8 May 2000 04:58:47 GMT
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: Socket Modual
Message-Id: <8f5hi7$i0m$1@slb7.atl.mindspring.net>
Justin Batrouney (robynbatrouney@bigpond.com) wrote:
: Hi
: I am trying to establish a connection in Perl to my ISP's POP3 mail server.
:
: I have obtained the connection, and now wish to send my username and
: password to the server to login.
:
: My question is....How do I send this data to the server. I assumed it would
: be the same as a file handle and I could just use print SOCKET "user
: $username"; but this doesn't work.
You probably want to use Net::POP3 (part of the libnet bundle available
from CPAN or ActiveState) for this rather than trying to re-invent the
wheel. If for some reason you do have to re-invent the wheel, take a
look at the code of Net::POP3 (it's all pure Perl) to see how to do it.
------------------------------
Date: 8 May 2000 10:33:58 GMT
From: The WebDragon <nospam@devnull.com>
Subject: Re: Suggest 'use Carp' to modify the messages of -w
Message-Id: <8f656m$elj$3@216.155.32.56>
In article <8f5age$g2h@netnews.hinet.net>, "John Lin"
<johnlin@chttl.com.tw> wrote:
| Dear all,
|
| With -w active, when our programs are not mature enough,
| we often see warning messages like this:
|
| Use of uninitialized value in concatenation (.)
| at C:/Perl/lib/CGI.pm line 950.
|
| This kind of messages often give me the first impression:
| 'Oh, this module does not carefully deal with "-w"',
| although eventually I will find the bug is actually in my own code.
|
| Besides, it doesn't provide useful enough information for us to catch
| the bug at the first spot. Although later we can 'use diagnostics',
| the bug might be difficult to reproduce by then.
|
| Just like 'Carp::carp' improves the messages of 'warn',
| I suggest 'use Carp' modify the "-w" messages to be like:
you might also want to check out
use CGI::Carp qw(fatalsToBrowser);
in conjunction with
use diagnostics -verbose;
| Use of uninitialized value in concatenation (.)
| at C:/Perl/lib/CGI.pm line 950, called at main.pl line 15.
|
| (1) It suggests the bug might be from your own code, not the module.
| (2) You know where to begin with debugging (main.pl line 15).
you may also wish to be certain you are using a more up to date verison
of CGI.pm, which is currently at 2.66 (and running quite well I might
add)
for details, downloads, and change log of CGI.pm (which includes the
CGI::Carp module) see
http://stein.cshl.org/WWW/software/CGI/
--
send mail to mactech (at) webdragon (dot) net instead of the above address.
this is to prevent spamming. e-mail reply-to's have been altered
to prevent scan software from extracting my address for the purpose
of spamming me, which I hate with a passion bordering on obsession.
------------------------------
Date: Mon, 08 May 2000 10:54:53 GMT
From: jlucande@my-deja.com
Subject: Video comes in bursts
Message-Id: <8f66do$phe$1@nnrp1.deja.com>
Hi!
I am trying to view live video, which I have redirected from
the port 8641 to http-port 80.
Redirection is for circumventing possible firewall
restrictions, which could prohibit socket connections
to the ports with high numbers.
The problem is that the video comes in bursts of few kilobytes each.
The behavior is the same with Netscape 4.61 and IE4.
There is no problem when the video is viewed from the original
port 8641.
Here is the perl script which is doing the redirection
from the port 8641 to port 80:
---Script begins-------------------------------------------
#!c:/perl/bin/perl.exe -Tw
use strict;
use CGI;
use Socket;
$CGI::DISABLE_UPLOADS = 1;
my ($q,$remote,$port, $iaddr, $paddr, $proto, $data);
$q = new CGI;
print $q->header(-type=>'x-video/mvq');
$remote = shift || 'localhost';
$port = shift || 8641; # mvq port
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
die "No port" unless $port;
$iaddr = inet_aton($remote) || die "no host: $remote";
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
connect(SOCK, $paddr) || die "connect: $!";
$|=1; # Forces the buffer to be flushed.
LOOP: while (read SOCK, $data, 8) {
$q->print($data) or last LOOP;
}
close (SOCK) || die "close: $!";
$q->close();
exit;
----Script ends---------------------------------------------------
The http server I am running is Apache 1.3.12 win32-version
on a NT 4.0 platform.
I would appreciate any help in resolving this problem.
t: Jussi
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V9 Issue 2976
**************************************