[15592] in Perl-Users-Digest
Perl-Users Digest, Issue: 3005 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 10 14:10:47 2000
Date: Wed, 10 May 2000 11:10:21 -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: <957982220-v9-i3005@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 10 May 2000 Volume: 9 Number: 3005
Today's topics:
Re: Perl code to check for broken links <aqumsieh@hyperchip.com>
Re: Please check my 'random' code <lr@hpl.hp.com>
Re: please help me to display REMOTE_HOST variable by g (Jim)
Precedence question <talexb@tabsoft.on.ca>
Re: Precedence question (Rafael Garcia-Suarez)
Re: Precedence question <uri@sysarch.com>
Re: Precedence question <talexb@tabsoft.on.ca>
Re: Precedence question <uri@sysarch.com>
Re: Precedence question (Tad McClellan)
Re: Precedence question nobull@mail.com
Re: Precedence question <lr@hpl.hp.com>
Re: Printing Arrays <nospam@devnull.com>
Re: Problem processing multiple files (Rafael Garcia-Suarez)
Re: Problem processing multiple files (Tad McClellan)
Re: Problem processing multiple files nobull@mail.com
Re: Pulling my hair out! <lr@hpl.hp.com>
Re: re-initializing through each loop (Abigail)
Re: Retrieving the name of a variable (Abigail)
Returning an IP Address <aran@mapcruzin.com>
Re: Returning an IP Address <nomail@nomail.com>
saving a html document wdysc@my-deja.com
saving a html document wdysc@my-deja.com
Re: saving a html document <tony_curtis32@yahoo.com>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 10 May 2000 17:59:49 GMT
From: Ala Qumsieh <aqumsieh@hyperchip.com>
Subject: Re: Perl code to check for broken links
Message-Id: <7ak8h2qoa1.fsf@Merlin.i-did-not-set--mail-host-address--so-shoot-me>
"Erick Jensen" <erick.jensen@unisys.com> writes:
> By the way, I may be wrong but the following smells like a goto (jump)
> statement:
>
> MAINLOOP:
> ...
> next MAINLOOP;
> ...
> next MAINLOOP;
> ...
> next MAINLOOP if $response->is_success; # ok
'Smells like' and 'actually is' are two different things. Please get
your notions straight. 'perldoc -f next' for more info.
> Perl is an ugly language to begin with, which is why I prefer to use
> ASP/VBScript as do most large companies (have you noticed the asp extension
> on a lot of your bookmarks?). I hope this little bit of truth doesn't
> offend anyone.
What is your definition of 'ugly'? Is it the excessive use of punctuation
characters? Is it the fact that there is more than one way to do things?
Something else? Please clarify.
Most large companies use ASP/VBScript for one reason: Microsoft. Perl,
as you know, is open source. Unfortunately, most managers don't know
better and simply go with the big names. I used to know someone who
loved Delphi, and always bragged about how superior it was to VB. But
when it came for production code, he always used VB! His reason: most
clients feel comfortable knowing that their product was written with a
Microsoft tool.
Moreover, you seem to assume that Perl is only used for web
applications. You can't be more wrong.
If you believe that your statements are true, then please back them up
with solid facts if you want anyone to take you seriously. Also, if you
are such a big fan of ASP/VBScript, why are you using Perl?
Just curious,
--Ala
------------------------------
Date: Wed, 10 May 2000 09:39:51 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Please check my 'random' code
Message-Id: <MPG.138330b45a65019a98aa48@nntp.hpl.hp.com>
In article <8fbddb$kcg$1@nnrp1.deja.com> on Wed, 10 May 2000 10:24:54
GMT, ra jones <ra_jones@my-deja.com> says...
It's best to leave attribution in here, rather than anonymous quotes.
Them's my words.
> > It will tell you not to call srand:
> >
> > In fact, it's usually not necessary to call srand at all, because if
> > it is not called explicitly, it is called implicitly at the first
> > use of the rand operator. However, this was not the case in version
> > of Perl before 5.004, so if your script will run under older Perl
> > versions, it should call srand.
> >
> > To simulate a coin toss, the rand() function is quite adequate, and
> > your approach is OK. It can be compressed thus:
> >
> > (qw(Test Control))[rand 2]
> >
>
> OK, thanks for that. Is there any harm in calling srand, even though the
> script is running on 5.005?
No harm, provided you don't supply an argument. The one you had in your
original post, time ^ $$, is explicitly singled out as inappropriate in
the srand documentation.
> And are there any advantages (apart from
> cosmetic) in compressing the code as you suggest?
I find the one line above easier to comprehend than the four lines in
your original post. But this is a matter of experience and taste, about
wich there can be no arguing.
There may be minor performance improvements in doing as few perl ops as
needed in any computation. But that can hardly matter here.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Wed, 10 May 2000 15:15:05 GMT
From: jconner@enterit.com (Jim)
Subject: Re: please help me to display REMOTE_HOST variable by gethostbyaddr
Message-Id: <39197ee0.160133289@news.earthlink.net>
Im not a guru at this but it seemed you were going about this in a
very difficult manner. Here is what I did to resolve your problem.
Remember that this is absolutely dependent on your name server
whichever name server you are using...if its not configured properly
this script won't work. I compensated for that a little bit. Try it
out.
#!/usr/bin/perl -wT
use Socket;
#Start printing the HTML Output format.
print <<"SeqONE";
Content-type: text/html
<html>
<head>
<title>Environment Variable </title>
</head>
<body text=#000000 bgcolor=#FFFFFF link=#990033 aLink=#cc0000
vLink=#996666>
<h2><u><center> Environmental System Variables</center></u></h2>
<table Col=3 border=2>
<strong> <tr>
<td><center> <h3>Variable Name</h3></center></td>
<td><center> <h3> Variable Value </h3></center></td>
<tr>
SeqONE
while(($key,$val) = each(%ENV)) {
print "<tr><td><B>$key</B>:</td><td> $val</td></tr> \n";
print "\n";
}
$iaddr = inet_aton($ENV{'REMOTE_ADDR'});
$nam_addr = gethostbyaddr($iaddr,AF_INET);
if (!$name_addr) {
$nam_addr = "did not resolve";
}
print "<tr><td><b>REMOTE_ADDR<b></td><td>$nam_addr</td></tr>";
print << "END_HTML";
</table><br><br>
<div align=right> © Srimanta Roy, 18th February 2K.</div>
</body>
</html>
END_HTML
On Tue, 09 May 2000 17:00:13 GMT, Joydip Chakladar
<joydip_chaklader@my-deja.com> wrote:
> I am just trying to display the REMOTE_HOST variable for my hit
> statistics programme.Though the other enviroment variables are
> displayed REMOTE_HOST is showing blank. By the suggestion of this
>newsgroup I got a
> function
>
> gethostbyaddr()
>
> I applied it to turn the IP Addresses in domain name of Remote Host .
>
> @bytes= split(/\./, $ENV{'REMOTE_ADDR'});
> $packAddress=pack("C4",@bytes);
> ($temp1,$temp2,$temp3,$temp4) =gethostbyaddr($packAddress,2);
> print"<tr><td>REMOTE_ADDR</td><td><B> $temp1,$temp2,$temp3,$temp4.
> $ENV{'REMOTE_ADDR'}.</B></td></tr>";
>
> this programme in the proxy is working fine and turning the IP address
>127.0.0.1 into localhost
> but it was not working in my server .It is giving me blank values .
>
> My online programme is in
>
> http://www.hitostat.com/cgi-bin/hit.pl
>
> and the programme content is in
>
> http://www.hitostat.com/hit.html
>
> Can anybody help me in this regard?
>
> Thanks in advance
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>
>
>
>--
>Joydip Chakladar
>http://www.allindialive.com
>From India to world
>A meeting place of surfers and webmasters
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.
------------------------------
Date: Wed, 10 May 2000 16:06:24 GMT
From: T. Alex Beamish <talexb@tabsoft.on.ca>
Subject: Precedence question
Message-Id: <1o1jhs8tbdat5mj2plpmb0giuf1l01nsml@4ax.com>
Greetings folks,
I have a question about precendence: I have an array of hashes as
follows:
my @PageMap =
(
{
'script' => 'query_results',
'symarg' => 'search_input',
'links' => $QueryResultsLinks
},
...
}
QueryResults is defined as a reference to an array of hash refernces:
my $QueryResultsLinks = [ \%ProfileTab, \%HistoricalFinancialsTab,
\%AnalystRatingsTab, \%InsiderTradesTab, \%ShortPositionsTab,
\%ResearchReportsTab, \%AdviceArticlesTab, \%FrancaisTab ];
When I run through the PageMap array using $Page as a foreach loop
variable, I want to run through the list of links, but I'm not able to
add the right punctuation to get it to work. The following workaround
does work:
my $RArray = $Page->{'links'};
foreach my $Link ( @$RArray )
I was trying to avoid using a temporary variable to get around the
correct syntax which I thought would be
foreach my $Link ( @$Page->{'links'} )
but I get "Not an ARRAY reference .. " from perl when I try that. If I
add braces
foreach my $Link ( @${Page->{'links'}} )
I don't get an error but I don't get any array elements either. Does
anyone have suggestions?
Thanks.
------------------------------
Date: Wed, 10 May 2000 16:22:09 GMT
From: garcia_suarez@hotmail.com (Rafael Garcia-Suarez)
Subject: Re: Precedence question
Message-Id: <slrn8hj3ao.muk.garcia_suarez@r-garcia.kazibao.net>
T. Alex Beamish wrote in comp.lang.perl.misc:
>Greetings folks,
>
>I have a question about precendence: I have an array of hashes as
>follows:
>
>my @PageMap =
>(
> {
> 'script' => 'query_results',
> 'symarg' => 'search_input',
> 'links' => $QueryResultsLinks
> },
> ...
>}
>
>QueryResults is defined as a reference to an array of hash refernces:
>
>my $QueryResultsLinks = [ \%ProfileTab, \%HistoricalFinancialsTab,
> \%AnalystRatingsTab, \%InsiderTradesTab, \%ShortPositionsTab,
> \%ResearchReportsTab, \%AdviceArticlesTab, \%FrancaisTab ];
>
>
>When I run through the PageMap array using $Page as a foreach loop
>variable, I want to run through the list of links, but I'm not able to
>add the right punctuation to get it to work. The following workaround
>does work:
>
> my $RArray = $Page->{'links'};
>
> foreach my $Link ( @$RArray )
>
>I was trying to avoid using a temporary variable to get around the
>correct syntax which I thought would be
>
> foreach my $Link ( @$Page->{'links'} )
Use @{ $Page->{'links'} }
Your construct was understood as (@$Page)->{'links'}, and $Page is not
an array reference.
>but I get "Not an ARRAY reference .. " from perl when I try that. If I
>add braces
>
> foreach my $Link ( @${Page->{'links'}} )
Oddities of perl grammar... This syntax means that you're accessing a static
field of a class Page, and that this static field is expected to be a
reference on an array reference.
>I don't get an error but I don't get any array elements either. Does
>anyone have suggestions?
>
>Thanks.
>
--
Rafael Garcia-Suarez
------------------------------
Date: Wed, 10 May 2000 16:23:53 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Precedence question
Message-Id: <x7snvqtluv.fsf@home.sysarch.com>
>>>>> "TAB" == T Alex Beamish <talexb@tabsoft.on.ca> writes:
TAB> my @PageMap =
TAB> {
TAB> 'links' => $QueryResultsLinks
TAB> },
TAB> foreach my $Link ( @$Page->{'links'} )
TAB> but I get "Not an ARRAY reference .. " from perl when I try that. If I
TAB> add braces
the @ is binding to $Page which is not an array ref.
TAB> foreach my $Link ( @${Page->{'links'}} )
TAB> I don't get an error but I don't get any array elements either. Does
TAB> anyone have suggestions?
you have the $ and braces in the wrong place. use
@{ $Page->{'links'} }
what you are doing is dereferencing the $Page hash to get the links
array. then that (in braces) is derefed to get the list of links.
now a good question is how is perl parsing this:
@${Page->{'links'}}
Page is a bareword being symbolically derefed as a hash ref. this is
evil and would have been detected if you used strict. also -w would
have given you more warnings about undefined values. you ARE y]using
strict and -w aren't you?
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page ----------- http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net ---------- http://www.northernlight.com
------------------------------
Date: Wed, 10 May 2000 16:34:36 GMT
From: T. Alex Beamish <talexb@tabsoft.on.ca>
Subject: Re: Precedence question
Message-Id: <1r3jhsch3kj98al3e5defi2eebcipkggb7@4ax.com>
On Wed, 10 May 2000 16:23:53 GMT, Uri Guttman <uri@sysarch.com> wrote:
>>>>>> "TAB" == T Alex Beamish <talexb@tabsoft.on.ca> writes:
>
> TAB> my @PageMap =
> TAB> {
> TAB> 'links' => $QueryResultsLinks
> TAB> },
>
> TAB> foreach my $Link ( @$Page->{'links'} )
>
> TAB> but I get "Not an ARRAY reference .. " from perl when I try that. If I
> TAB> add braces
>
>the @ is binding to $Page which is not an array ref.
>
>
> TAB> foreach my $Link ( @${Page->{'links'}} )
>
> TAB> I don't get an error but I don't get any array elements either. Does
> TAB> anyone have suggestions?
>
>you have the $ and braces in the wrong place. use
>
> @{ $Page->{'links'} }
>
>what you are doing is dereferencing the $Page hash to get the links
>array. then that (in braces) is derefed to get the list of links.
Uri,
Thanks so much for your answer. I was trying to dereference using ()
and not {} .. I'm not clear about why .. are braces always used to
dereference? Right at the beginning of Advanced Perl Programming '$'
is used for dereferencing. Oh well. Got it now.
>now a good question is how is perl parsing this:
>
> @${Page->{'links'}}
>
>Page is a bareword being symbolically derefed as a hash ref. this is
>evil and would have been detected if you used strict. also -w would
>have given you more warnings about undefined values. you ARE y]using
>strict and -w aren't you?
I know this group well enough not to even post without having tried
that. I have -w in the file and on the command line, and I have use
strict in the code.
Alex
------------------------------
Date: Wed, 10 May 2000 16:48:37 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Precedence question
Message-Id: <x7og6etkpo.fsf@home.sysarch.com>
>>>>> "TAB" == T Alex Beamish <talexb@tabsoft.on.ca> writes:
TAB> Thanks so much for your answer. I was trying to dereference using ()
TAB> and not {} .. I'm not clear about why .. are braces always used to
TAB> dereference? Right at the beginning of Advanced Perl Programming '$'
TAB> is used for dereferencing. Oh well. Got it now.
a simple ref in a scalar is derefed by putting the appropriate symbols
in front of it. so $foo = [ 1 , 2] is derefed as @$foo. but if the ref
is the result of an expression, you need to put braces around it to
control the precedence and the you deref what is in the braces.
$foo->{'bar'} = [1,2] ;
print @{$foo->{'bar'}} ;
so in the world of refs, braces do act like parens elsewhere.
perl quiz: name as many syntactical uses for {} as you can.
TAB> I know this group well enough not to even post without having tried
TAB> that. I have -w in the file and on the command line, and I have use
TAB> strict in the code.
but then you should have gotten lots of fun messages and you didn't
report any. that is why i made that point.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page ----------- http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net ---------- http://www.northernlight.com
------------------------------
Date: Wed, 10 May 2000 11:44:34 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Precedence question
Message-Id: <slrn8hj0v2.ii9.tadmc@magna.metronet.com>
On Wed, 10 May 2000 16:06:24 GMT, T. Alex Beamish <talexb@tabsoft.on.ca> wrote:
>The following workaround
>does work:
>
> my $RArray = $Page->{'links'};
>
> foreach my $Link ( @$RArray )
>
>I was trying to avoid using a temporary variable to get around the
>correct syntax which I thought would be
>
> foreach my $Link ( @$Page->{'links'} )
>
>but I get "Not an ARRAY reference .. " from perl when I try that. If I
>add braces
>
> foreach my $Link ( @${Page->{'links'}} )
^
^
That brace is in the wrong place
Page->{'links'} (the stuff in the curlies) does not
look like a Perl variable.
foreach my $Link ( @{$Page->{'links'}} )
>I don't get an error but I don't get any array elements either. Does
>anyone have suggestions?
Move the curly over one character :-)
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: 10 May 2000 17:56:04 +0100
From: nobull@mail.com
Subject: Re: Precedence question
Message-Id: <u9snvqe44b.fsf@wcl-l.bham.ac.uk>
T. Alex Beamish <talexb@tabsoft.on.ca> writes:
> Thanks so much for your answer. I was trying to dereference using ()
> and not {} .. I'm not clear about why .. are braces always used to
> dereference?
No - braces are used to override precedence when dereferencing.
Confusingly:
Braces are also used to enclose hash keys when seleting an element or
slice from a hash.
Braces are the anonymous hash constructor.
Braces enclose BLOCKS of code.
Braces are used in interpolation to resolve ambiguity as to where a
symbol ends. (Actually this is kinda the same as the dereferencing
thing).
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 10 May 2000 10:43:24 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Precedence question
Message-Id: <MPG.13833f9b990be15098aa4d@nntp.hpl.hp.com>
In article <x7og6etkpo.fsf@home.sysarch.com> on Wed, 10 May 2000
16:48:37 GMT, Uri Guttman <uri@sysarch.com> says...
...
> perl quiz: name as many syntactical uses for {} as you can.
Risking all:
Hash-key designator
Anonymous hash constructor
Paired delimiters (quotes, regexes, substitutions)
Block delimiters
Dereference delimiters
In ${$foo->bar}, the contents of {...} are a BLOCK.
In "${foo}bar", similarly.
So this may not count as a separate syntactical use.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 10 May 2000 17:46:00 GMT
From: The WebDragon <nospam@devnull.com>
Subject: Re: Printing Arrays
Message-Id: <8fc78o$dd4$1@216.155.32.13>
In article <8fbcfo$jlt$1@nnrp1.deja.com>, Ilja <billy@arnis-bsl.com>
wrote:
| In article <MPG.1381e7a7b5fe36fe98aa2a@nntp.hpl.hp.com>,
| Larry Rosler <lr@hpl.hp.com> wrote:
| > In article <8f8lag$i4a$1@nnrp1.deja.com> on Tue, 09 May 2000 09:21:30
| > GMT, Ilja <billy@arnis-bsl.com> says...
| >
| > ...
| >
| > > Hm-m, why you need loop to print an array ?
| > >
| > > $, = "\n";
| > > print @block;
| >
| > To produce the same output as the other attempts, that should be:
| >
| > print @block, "\n";
|
| IMHO you'll get an extra new-line, I guess you mean:
...
my @dataList;
for (@filesList) {
my $grabFile = $inputDir . $_;
open(GRAB, "<$grabFile") or die ("Cannot open file $grabFile : $!");
# print "Successful open of $grabFile\n";
push @dataList, "-- $_\n";
while(<GRAB>) {
tr/\r//d;
next unless /^\Qmaps[i++] = new Map(\E([^)]+)/;
push @dataList, "$1\n" ;
# print "$1\n";
}
close (GRAB);
};
...
how do I remove that last \n once @dataList is done being created?
--
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: Wed, 10 May 2000 15:21:33 GMT
From: garcia_suarez@hotmail.com (Rafael Garcia-Suarez)
Subject: Re: Problem processing multiple files
Message-Id: <slrn8hivp4.mrl.garcia_suarez@r-garcia.kazibao.net>
mfelps@my-deja.com wrote in comp.lang.perl.misc:
>First the script:
>
>$dirname="test";
>opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
>while (defined($file = readdir(DIR))) {
> open (FILE, $file); #open original html file
Check the return value of your system calls!
You're trying to open 'test1.html' but you want to open
"$dirname/test1.html".
And don't try to open a $file that is not a file (e.g. the '.' and '..'
directories). Test $file for fileness:
if (-f $file) { open FILE,$file or die "Can't open file:$!\n"; }
> $file2="new".$file; #prepend new to file name
> open (FILE2, ">$file2"); #open new file for writing
> while ($line=<FILE>){ #read in the file
> #replace </BODY>with (link) <BODY>
>$line=~
>s/<\/[Bb][Oo][Dd][Yy]>/<BR><AHREF=http:\/\/www.foobar.com>Link\n<\/BODY>
>/;
> print FILE2 $line; #write line to new file
>}
>close (FILE); #close files
>close (FILE2);
>}
>closedir(DIR); #close directory
>
>
> Okay, basically this is supposed to open each HTML file in the
>"test" directory and then print out a new page
>(from test1.html to newtest1.html) and the new page should have a link
>at the end of the page.
> It works if I use it against a single file (ie: leaving off the 1st
>three lines [and the last]). It sort of works this way,
>in that it opens the files for write, but it never writes anything to
>them. I get a bunch of blank newtest.html files. I did the middle
>part, and swiped the multiple file processing code from Perl Cookbook.
>Any ideas why this only half works?
>
>Mark Felps
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.
--
Rafael Garcia-Suarez
------------------------------
Date: Wed, 10 May 2000 11:27:23 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Problem processing multiple files
Message-Id: <slrn8hivur.ids.tadmc@magna.metronet.com>
On Wed, 10 May 2000 13:54:31 GMT, mfelps@my-deja.com <mfelps@my-deja.com> wrote:
>First the script:
>
>$dirname="test";
>opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
You checked the return value. Very Good Idea.
>while (defined($file = readdir(DIR))) {
^^^^^^^
perldoc -f readdir
(my underlining)
------------------------------
opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
@dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
^^^^ ^^^^^^^^^^^^
closedir DIR;
------------------------------
If you had read and understood what the author of the readdir()
function has to say about how his function works, you might
have noticed that directory name pasted onto the front of
what readdir() returned.
Then you would have pasted _your_ dirname in.
You might also have noticed the use of grep() to filter the
results of readdir().
Then you would have filtered to ensure you process _only_
HTML files.
> open (FILE, $file); #open original html file
^^^^
perldoc -f open
If you had read and understood what the author of the open()
function has to say about how his function works, you might
have checked the return value there, which would have helped
you debug the other problems in your program.
Always, yes *always*, check the return value from open()
open (FILE, $file) || die "could not open '$file' $!";
^^
^^
That would have said "No such file" or some such, which
would have been a clue as to what you were doing wrong.
> $file2="new".$file; #prepend new to file name
my $file2 = "new$file";
>$line=~
>s/<\/[Bb][Oo][Dd][Yy]>/<BR><AHREF=http:\/\/www.foobar.com>Link\n<\/BODY>
>/;
Yikes!
That is much uglier (and harder to understand) than it needs to be.
It might be _you_ that has to understand it later, so you should
help yourself out by writing things as clearly as you can.
Perl has some features that can help you do that, such as the
s///i option and alternate delimiters for the substitution
operator.
My code does not force the </BODY> tag to all upper case like
your code did.
I also changed the replacement string.
s#(</BODY>)#<BR><AHREF="http://www.foobar.com">Link</A>\n$1#i;
> Okay, basically this is supposed to open each HTML file
^^^^^^^^^^^^^^
It will try to open() HTML files, and any _other_ files, and
directories and symlinks and ...
perldoc -f grep
foreach my $file ( grep /\.html$/, readdir(DIR) ) {
>in the
>"test" directory
Your open() calls try to open in in the _current directory_,
not in the "test/" subdirectory.
open (FILE, "$dirname/$file") || die "could not open '$dirname/$file' $!";
>and then print out a new page
>(from test1.html to newtest1.html) and the new page should have a link
>at the end of the page.
You have some crufty looking HTML there.
>Any ideas why this only half works?
Yes.
And you would too if you could be bothered to read the
description of the functions that you use!
You have done the programming equivalent of signing a contract
without reading it!
You are asking for trouble.
Looks like you got what you asked for.
I recommend not asking for trouble :-)
Use the docs that come with perl.
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: 10 May 2000 17:40:01 +0100
From: nobull@mail.com
Subject: Re: Problem processing multiple files
Message-Id: <u9u2g6e4v2.fsf@wcl-l.bham.ac.uk>
mfelps@my-deja.com writes:
> First the script:
Please get into the habbit of using strict, enableling all warnings
and checking for errors on all system calls. You may not have been
bitten yet but sooner or later you will be. The longer you've been
programming in Perl without these safety measures the more bad habits
you'll have to break if you ever start using Perl seriously.
> $dirname="test";
> opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
Good you checked the return from opendir()
> while (defined($file = readdir(DIR))) {
> open (FILE, $file); #open original html file
You forgot to check the return from open().
You forgot to prepend $dirname.
You also forgot to check the $file was a file not a directory.
Comments that simply re-state what is written in the programming
language are of no use.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 10 May 2000 10:16:33 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Pulling my hair out!
Message-Id: <MPG.13833949b1cba0c698aa4b@nntp.hpl.hp.com>
[Removed alt.perl]
In article <8fbmpd$ueu$1@nnrp1.deja.com> on Wed, 10 May 2000 13:04:56
GMT, vlad55@my-deja.com <vlad55@my-deja.com> says...
> In article <957176011.20224.0.nnrp-13.d4e441b4@news.demon.co.uk>,
> "Adam T Walton" <waltonic@earbuzz.demon.co.uk> wrote:
...
> > @chopped=/"([^"]+)"/g;
>
> try this:
> @chopped=/"([^"]+?)"/gcs;
>
> not sure but may help
How would it help? It is identical, except for the addition of three
superfluous characters, none of which affects its function in any way.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 10 May 2000 17:07:07 GMT
From: abigail@foad.org (Abigail)
Subject: Re: re-initializing through each loop
Message-Id: <slrn8hj5pr.tf8.abigail@ucan.foad.org>
On Wed, 10 May 2000 08:10:26 GMT, Bart Lateur <bart.lateur@skynet.be> wrote:
++ Abigail wrote:
++
++ >On Sat, 06 May 2000 21:04:30 GMT, Bart Lateur <bart.lateur@skynet.be> wrote:
++
++ >++ BTW I don't think that the speed penalty for always declaring the
++ >++ lexical variables inside the loop isn't any reason for not doing it all
++ >++ the time.
++ >
++ >
++ >What speed penalty? Hopefully you know that my declares the variable
++ >at _compile_ time; not runtime.
++
++ I thought about that later, too. I'm still in some doubt.
++
++ for (1 .. 10) {
++ my $i;
++ $i = 'foo';
++ }
++
++ Now, I expect that the runtime effect of my is to undef the lexical
++ variable, every time in a loop. Now *that* was the speed penalty I was
++ thinking of, basically.
++
++ I'm not sure if
++
++ my $i = 'foo';
++ and
++ my $i; $i = 'foo';
++
++ do the same thing: in the first case, does the declaration clear the
++ lexical before assigning the value to it, or not? It does in the second
++ case.
Again, you are confusing declarion and run time effects. Declaration
happens at COMPILE time. Assignment happens at RUNTIME.
Your two things are, at runtime, equivalent to:
$i = 'foo';
and
undef $i; $i = 'foo';
Unless the optimizer optimizes it away, I think you can see the second one
would be slower.
However, there's no need to write:
my $i; $i = 'foo';
as
my $i = 'foo';
works as well.
Abigail
------------------------------
Date: 10 May 2000 18:01:49 GMT
From: abigail@foad.org (Abigail)
Subject: Re: Retrieving the name of a variable
Message-Id: <slrn8hj90d.tf8.abigail@ucan.foad.org>
On Mon, 01 May 2000 03:08:05 GMT, Makau Divangamene <makau@multimania.com> wrote
++
++ > I know the answer to your question but it is too
++ > complicated to explain to you.
++
++ I really doubt it.
++
++ Some people on this newsgroup will either never say that something
++ can't be done with Perl, or will prefer to give such an incredibly
++ helpful reply expressed above.
++
++ Talk about context. I may explain you *in depth* why I want to do that
++ and you will likely find a workaround to my problem. But what if
++ someone else has the same problem as mine, with a completely different
++ context? Just giving the solution to my problem problem would help
++ *everybody*. Giving a workaround to my specific case will virtually
++ profit noone else but me.
Then let me give a general answer: you do it basically in the same way
as in C, Java, Pascal and most other languages.
Abigail
------------------------------
Date: Wed, 10 May 2000 17:35:33 GMT
From: "Aran Meuser" <aran@mapcruzin.com>
Subject: Returning an IP Address
Message-Id: <F5hS4.96986$cZ.260318@typhoon.southeast.rr.com>
I'm trying to write a script that will display my IP address as seen by the
rest of the internet. I can't seem to find anything in any of the Net,
Http, DNS, etc, modules that *might* have this capability. If anyone could
help, I'd sure appreciate it, I bet its quite simple.
Aran
------------------------------
Date: Wed, 10 May 2000 12:10:55 -0400
From: "Jonah" <nomail@nomail.com>
Subject: Re: Returning an IP Address
Message-Id: <8fc7un$3430$1@onlink3.onlink.net>
Aran Meuser <aran@mapcruzin.com> wrote in message
news:F5hS4.96986$cZ.260318@typhoon.southeast.rr.com...
> I'm trying to write a script that will display my IP address as seen by
the
> rest of the internet. I can't seem to find anything in any of the Net,
> Http, DNS, etc, modules that *might* have this capability. If anyone
could
> help, I'd sure appreciate it, I bet its quite simple.
>
> Aran
>
>
Not sure exactly you mean, but try
$ENV{'REMOTE_ADDR'};
is that what you want?
------------------------------
Date: Wed, 10 May 2000 15:31:30 GMT
From: wdysc@my-deja.com
Subject: saving a html document
Message-Id: <8fbvci$808$1@nnrp1.deja.com>
I am trying to write a script which should save to a file a html
document, I have to call several times with diffrent parameters, I
figured out how to do it but the problem is i need a password to that
site I tried using http://username:password@www.file.com but it gives me
an error.
does anyone have a solution?
here is my code
#!/usr/bin/perl5
use LWP::UserAgent;
print "Content-type: text/html\n\n";
$user_agent = new LWP::UserAgent;
$request = new HTTP::Request('GET', 'http://www.file.com');
$response = $user_agent->request($request);
open FILEHANDLE, ">cha.txt";
print FILEHANDLE $response->{_content};
print FILEHANDLE $a;
close FILEHANDLE;
print $response->{_content};
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Wed, 10 May 2000 15:32:18 GMT
From: wdysc@my-deja.com
Subject: saving a html document
Message-Id: <8fbve3$80b$1@nnrp1.deja.com>
I am trying to write a script which should save to a file a html
document, I have to call several times with diffrent parameters, I
figured out how to do it but the problem is i need a password to that
site I tried using http://username:password@www.file.com but it gives me
an error.
does anyone have a solution?
here is my code
#!/usr/bin/perl5
use LWP::UserAgent;
print "Content-type: text/html\n\n";
$user_agent = new LWP::UserAgent;
$request = new HTTP::Request('GET', 'http://www.file.com');
$response = $user_agent->request($request);
open FILEHANDLE, ">cha.txt";
print FILEHANDLE $response->{_content};
print FILEHANDLE $a;
close FILEHANDLE;
print $response->{_content};
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 10 May 2000 10:44:59 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: saving a html document
Message-Id: <878zxiwgsk.fsf@shleppie.uh.edu>
>> On Wed, 10 May 2000 15:32:18 GMT,
>> wdysc@my-deja.com said:
> I am trying to write a script which should save to a
> file a html document, I have to call several times with
> diffrent parameters, I figured out how to do it but the
> problem is i need a password to that site I tried using
> http://username:password@www.file.com but it gives me an
> error. does anyone have a solution? here is my code
I presume this is using just the Basic authentication?
If so, you'll probably want to take a look in
LWP::UserAgent for
$ua->credentials($netloc, $realm, $uname, $pass)
Set the user name and password to be used for a realm.
It is often more useful to specialize the
get_basic_credentials() method instead.
$ua->get_basic_credentials($realm, $uri, [$proxy])
...
hth
t
------------------------------
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 3005
**************************************