[17001] in Perl-Users-Digest
Perl-Users Digest, Issue: 4413 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Sep 23 14:05:24 2000
Date: Sat, 23 Sep 2000 11:05:06 -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: <969732306-v9-i4413@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Sat, 23 Sep 2000 Volume: 9 Number: 4413
Today's topics:
Re: getting images with LWP? <bbowden@mitre.org>
Re: Gunzip at Prohosting <rpolzer@web.de>
Re: HTML to Text converter <tony_curtis32@yahoo.com>
Re: Local variable screws thing up? hiroshiishii@my-deja.com
Re: Local variable screws thing up? hiroshiishii@my-deja.com
looking for advice <arbaff@hotmail.com>
LWP and proxy tauras6097@my-deja.com
Perl script behaving misteriously stanislav_zunjic@my-deja.com
Re: Perl script behaving misteriously (Garry Williams)
Perl, Unix and printers <vivekvp@spliced.com>
The best tutorial? <giulio_agostini@my-deja.com>
Re: there must be an easier way to do this (Keith Calvert Ivey)
Re: there must be an easier way to do this (Abigail)
Re: there must be an easier way to do this <mauldin@netstorm.net>
Re: use vs. @ISA <jeffp@crusoe.net>
Using perl scripts for picture rollover effect in HTML? <marc.liegeois@ic24.net>
variable -> constant? <nconway@klamath.dyndns.org>
Re: variable -> constant? <mauldin@netstorm.net>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 23 Sep 2000 12:21:38 -0400
From: Bruce Bowden <bbowden@mitre.org>
Subject: Re: getting images with LWP?
Message-Id: <39CCD892.FA4CF132@mitre.org>
Success!!
Mark-Jason Dominus wrote:
> To narrow down the problem, add the line
>
> print "Got ", length($image), " bytes from network.\n";
>
> and see if it looks reasonable. If it reports that you got zero bytes
> from the network, you can rule out problems with the
> open-binmode-print part of the program and concentrate on why the
> 'get' is not working.
Great idea! I was getting zero bytes transferred. After a little testing
I found my (rather embarrasing) problem:
get('$var') vs get("$var")
strong quotes were preventing my variable from being interpolated. I was
sure I had used get() with strong quotes and variables before... but
maybe not. Sorry to waste all of your time with such a simple "problem",
but I appreciate your comments. This is the best newsgroup around.
Thanks to everyone.
> Hmm, this reminds me of a passage in 'Zen and the Art of Motorcycle
> Maintenance'. You might want to have a look at that.
been ten years since I read it, maybe I will break it out again.
------------------------------
Date: Sat, 23 Sep 2000 18:07:43 +0200
From: "Seiberdragan" <rpolzer@web.de>
Subject: Re: Gunzip at Prohosting
Message-Id: <8qinrc$j25$1@riker.addcom.de>
"Gwyn Judd" <tjla@guvfybir.qlaqaf.bet> schrieb im Newsbeitrag
news:slrn8sogpu.s5.tjla@thislove.dyndns.org...
> I was shocked! How could BUCK NAKED1 <dennis100@webtv.net>
> say such a terrible thing:
> >Try those modules and they didn't work.
>
> No english me
#!/usr/bin/perl
for(;;)
{
print "there are idiots here";
}
>
> --
> Gwyn Judd (print `echo 'tjla@guvfybir.qlaqaf.bet' | rot13`)
> The plural of spouse is spice.
------------------------------
Date: 23 Sep 2000 10:08:02 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: HTML to Text converter
Message-Id: <8766nndu3x.fsf@limey.hpcc.uh.edu>
>> On Sat, 23 Sep 2000 08:04:46 PDT,
>> Tom Geldner <tom@xor.cc> said:
> I'm looking for something in Perl that will convert HTML
> to straight Text (ASCII). Basically, I want to use it to
> strip HTML out of incoming e-mails on my server. I've
> searched most of the Perl archives I know about without
> success. I found one module listed but the ftp link was
> dead.
use HTML::TreeBuilder then HTML::FormatText
hth
t
--
Namaste!
And an "oogabooga" to you too!
-- Homer Simpson
------------------------------
Date: Sat, 23 Sep 2000 17:17:35 GMT
From: hiroshiishii@my-deja.com
Subject: Re: Local variable screws thing up?
Message-Id: <8qioj1$7v0$1@nnrp1.deja.com>
Thank you, Martien, for your advice. I am going to understand your
comments, by trying them out.
You guys are great and quick!
-Hiroshi
In article <slrn8sopmr.jr6.mgjv@martien.heliotrope.home>,
mgjv@tradingpost.com.au wrote:
> On Sat, 23 Sep 2000 04:42:25 GMT,
> hiroshiishii@my-deja.com <hiroshiishii@my-deja.com> wrote:
> > Hi everyone:
> >
> > I cannot nake this simple script work. Local declaration seems
wrong.
> >
> >
> > ---------------------------------
> > #!/usr/local/bin/perl5
>
> #!/usr/local/bin/perl5 -w
> use strict;
>
> > # This list all files in a directory.
> >
> > print "Which directory? \n";
> > $start_dir = <STDIN>;
>
> my $start_dir = <STDIN>;
> chomp $start_dir;
>
> > &search($start_dir);
>
> search($start_dir);
>
> The & isn't necessary, and you should only use it if you really need
its
> semantics.
>
> # perldoc perlsub
>
> > #***********
> > sub search
> > #***********
> > {
> > local $dir = @_;
>
> Don't use local if you mean to use my. Also see the FAQ:
>
> # perldoc -q 'local.*my'
> Found in /opt/perl/lib/5.6.0/pod/perlfaq7.pod
> What's the difference between dynamic and lexical (static)
> scoping? Between local() and my()?
>
> Don't assign arrays to scalars. arrays in scalar context yield the
> number of elements in the array.
>
> Either of the following will do, in order of preference:
>
> my $dir = shift;
> my $dir = $_[0];
> my ($dir) = @_;
>
> > opendir(DIR, $dir);
>
> opendir(DIR, $dir) or die "Cannot opendir $dir: $!";
>
> > foreach $file (readdir(DIR))
>
> If you are going to process the files one by one, then the following
> would be better style:
>
> while (my $file = readdir DIR)
>
> But more idiomatic perl is to just get all the files in once,
filtering
> with grep, and transforming with map.
>
> > {
> > print "$file\n";
> > }
> > closedir(DIR);
> > }
> >
> > Thank you very much for your advice.
>
> # perldoc -f chomp
> # perldoc strict
> # perldoc -f shift
> # perldoc -f readdir
> # perldoc -f grep
> # perldoc -f map
>
> Martien
> --
> Martien Verbruggen |
> Interactive Media Division | System Administration is a dirty
> Commercial Dynamics Pty. Ltd. | job, but someone said I have to do
> NSW, Australia | it.
>
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Sat, 23 Sep 2000 17:51:24 GMT
From: hiroshiishii@my-deja.com
Subject: Re: Local variable screws thing up?
Message-Id: <8qiqin$a3m$1@nnrp1.deja.com>
Thank you very much for your detailed comments.
You guys are really helpful. I'll look up all those docs you mentioned.
-Hiroshi
In article <39CC38AF.15C0887@vpservices.com>,
Jeff Zucker <jeff@vpservices.com> wrote:
> hiroshiishii@my-deja.com wrote:
> >
> > #!/usr/local/bin/perl5
>
> Should have "-w" to turn on warnings.
>
> Should have "use strict" to spot problems.
>
> > # This list all files in a directory.
> >
> > print "Which directory? \n";
> > $start_dir = <STDIN>;
>
> MUST chomp($start_dir) to remove the newline that gets inserted.
>
> > &search($start_dir);
> >
> > #***********
> > sub search
> > #***********
> > {
> > local $dir = @_;
>
> Use "my" instead of "local".
> @_ is a *list* of arguments. If you want only the first argument, use
> "shift" or $_[0].
>
> > opendir(DIR, $dir);
>
> Check to see if your open succeeds:
>
> opendir(DIR,$dir) or die "Couldn't open $dir: $!";
>
> --
> Jeff
>
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Sat, 23 Sep 2000 12:44:37 -0400
From: "Aaron Baff" <arbaff@hotmail.com>
Subject: looking for advice
Message-Id: <39ccdf64$1@cs.rit.edu>
ok, I'm a freshman in college majoring in software engineering and from a
few magazine articles, I have heard perl is an extremely useful language. I
would like to know what ppl think of as the best book/manuel for beginners
to start on and what you would use to compile it in a win98 environment(or
unix, I have access to sun workstations). thank you all in advance!
Aaron Baff
arb8320@rit.edu
------------------------------
Date: Sat, 23 Sep 2000 16:34:40 GMT
From: tauras6097@my-deja.com
Subject: LWP and proxy
Message-Id: <8qim30$52t$1@nnrp1.deja.com>
I must go through a proxy at my company.
Prior to the proxy I could get WEB pages like this:
use LWP::Simple;
$doc = get 'http://www.some_web_page.com/';
Very simple and it worked great.
How do I do this with the proxy?
I read all the UserAgent examples and still can't figure it out.
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Sat, 23 Sep 2000 16:23:17 GMT
From: stanislav_zunjic@my-deja.com
Subject: Perl script behaving misteriously
Message-Id: <8qild6$46r$1@nnrp1.deja.com>
This is my very first time I'm boring people with my Perl problems, so
please take a look at this . Here is a small part of my code which
behaves very, very misterious:
# # # #
$counter_name = "Total Mails";
@rowData=execSql_CountFromOneTable();
createHTMLTableRowJustCountTable(\@rowData, $counter_name);
sub createHTMLTableRowJustCountTable{
my $refRow=$_[0];
my $counter_name=$_[1];
print ("<tr>\n");
print ("<td><b>$counter_name</b></td>\n");
print ("<td>@{$refRow}[0]</td>\n");
print ("</tr>\n");
}
# # # #
Resulting HTML looks like this:
<tr>
<td><b></b></td>
<td>5655452</td>
</tr>
In other words, value in @rowData is correctly written, but value in
$counter_name dissapeared! It seems that inside my subroutine
$counter_name became "", undef or whatsoever... How is that possible?
Then, I tried to swap subroutine parameters, like this:
# # # #
$counter_name = "Total Mails";
@rowData=execSql_CountFromOneTable();
createHTMLTableRowJustCountTable($counter_name, \@rowData);
sub createHTMLTableRowJustCountTable{
my $counter_name=$_[0];
my $refRow=$_[1];
print ("<tr>\n");
print ("<td><b>$counter_name</b></td>\n");
print ("<td>@{$refRow}[0]</td>\n");
print ("</tr>\n");
}
# # # #
Now, HTML result is even worse:
<tr>
<td><b></b></td>
<td></td>
</tr>
I looks just like $counter_name prevents any further parameters to be
passed into subroutine! Twilight zone or my mistake?
And finnaly, I tried to omit $counter_name parameter, like this:
# # # #
$counter_name = "Total Mails";
@rowData=execSql_CountFromOneTable();
createHTMLTableRowJustCountTable(\@rowData);
sub createHTMLTableRowJustCountTable{
my $refRow=$_[0];
print ("<tr>\n");
print ("<td><b>$counter_name</b></td>\n");
print ("<td>@{$refRow}[0]</td>\n");
print ("</tr>\n");
}
# # # #
Once again HTML result is:
<tr>
<td><b></b></td>
<td>5655452</td>
</tr>
Unbelievable! In my script, $counter_name global variable, under some
circumstances, becomes unvisible inside subroutine. Anybody has any
ideas?
I'm using Perl 5.00503 on RedHat 6.0.
thank you,
Stanislav Zunjic
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Sat, 23 Sep 2000 16:58:27 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: Perl script behaving misteriously
Message-Id: <Ti5z5.88$RE.4403@eagle.america.net>
On Sat, 23 Sep 2000 16:23:17 GMT, stanislav_zunjic@my-deja.com
<stanislav_zunjic@my-deja.com> wrote:
...
># # # #
>$counter_name = "Total Mails";
>@rowData=execSql_CountFromOneTable();
>createHTMLTableRowJustCountTable(\@rowData, $counter_name);
>
>sub createHTMLTableRowJustCountTable{
>
>my $refRow=$_[0];
>my $counter_name=$_[1];
>print ("<tr>\n");
>print ("<td><b>$counter_name</b></td>\n");
>print ("<td>@{$refRow}[0]</td>\n");
>print ("</tr>\n");
>}
># # # #
>
>Resulting HTML looks like this:
>
><tr>
><td><b></b></td>
><td>5655452</td>
></tr>
>
>In other words, value in @rowData is correctly written, but value in
>$counter_name dissapeared! It seems that inside my subroutine
>$counter_name became "", undef or whatsoever... How is that possible?
That's funny. It works for me:
$ cat x
#!/usr/local/bin/perl -w
# # # # ^^ <-- this might help
$counter_name = "Total Mails";
@rowData=execSql_CountFromOneTable();
createHTMLTableRowJustCountTable(\@rowData, $counter_name);
sub createHTMLTableRowJustCountTable{
my $refRow=$_[0];
my $counter_name=$_[1];
print ("<tr>\n");
print ("<td><b>$counter_name</b></td>\n");
print ("<td>@{$refRow}[0]</td>\n");
print ("</tr>\n");
}
sub execSql_CountFromOneTable {
(5655452);
}
# # # #
$ perl x
<tr>
<td><b>Total Mails</b></td>
<td>5655452</td>
</tr>
$
Hmmm. I guess we're not seeing everything that you really are doing.
--
Garry Williams
------------------------------
Date: Sat, 23 Sep 2000 17:38:44 GMT
From: vivekvp <vivekvp@spliced.com>
Subject: Perl, Unix and printers
Message-Id: <8qipr1$9cc$1@nnrp1.deja.com>
Hello,
I want to write a script that would disable printer queues then reenable
them with a perl script.
The print queues are setup in a unix environment.
Let say printers pr1 pr3 and pr3
I would like to do a:
disable pr1
disable pr2
disable pr3
then be able to do an:
enable pr1
enable pr2
enable pr3
problem is - I have about 300 printers to do.
I guess I could load a printer list (print.txt) and put it in a loop
with perl - and uses a system call to disable and enable:
ie
$a=print.txt
while $a
system call to unix ('disable $a')
next
then do the same for the for the enable.
But how do I code this in perl?
Any help?
Thanks,
V
--
He who fights and runs away, lives to run another day!
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Sat, 23 Sep 2000 16:28:10 GMT
From: Giulio Agostini <giulio_agostini@my-deja.com>
Subject: The best tutorial?
Message-Id: <8qilm9$4oa$1@nnrp1.deja.com>
Hi, I'd like to teach myself Perl.
What's the best place to start? Maybe a free online tutorial?
Thanks a lot.
Ciao, Giulio
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Sat, 23 Sep 2000 15:39:36 GMT
From: kcivey@cpcug.org (Keith Calvert Ivey)
Subject: Re: there must be an easier way to do this
Message-Id: <39cdc081.47052725@news.newsguy.com>
"Dan and Shelly" <wedeking@msa.attmil.ne.jp> asked for comments
on his code:
>print "<html>
><head>
><title>Dan's paragraphs</title>
></head>
><body>
><pre>\n";
You should begin your program with
#!/usr/bin/perl -w
use strict;
as often recommended in this newsgroup. Both -w and strict will
help you find errors.
The print statement might be better written with here-doc syntax
(EOT is indented only to set off the example):
print <<EOT;
<html>
<head>
<title>Dan's paragraphs</title>
</head>
<body>
<pre>
EOT
>open(PARAGRAPHS,"paragraphs.txt") or
> die "can't open paragraphs.txt\n";
Good that you're checking the return value, but it would be
better to include $! in the message so that you'll know *why*
you couldn't open the file. Also, you might want to leave off
the newline at the end of the message, so that you'll know what
line the error occurred on:
open(PARAGRAPHS,"paragraphs.txt") or
die "can't open paragraphs.txt: $!";
I'm going to rewrite the loop below, but first I have a few
comments on the style in your version:
>while(<PARAGRAPHS>) {
>
> if ($_ =~ /\S+/ && # if the line is not blank
> $_ !~ /^bad line/ # and if it's not a bad line
The point of using $_ rather than some other variable is that
for many purposes (including regex matches) you don't need to
type it out. Make that
if (/\S+/ && # if the line is not blank
!/^bad line/ # and if it's not a bad line
> ) { # some lines are blank or bad and don't get added to the paragraph
> $paragraphLine = $_; # step through lines of paragraphs.txt
> } else { $paragraphLine = "" }
>
> if ("$paragraphLine" =~ /^00/) {
The quotes there are unnecessary and confusing.
> $endOfPara = 'true';
Rather than using 'true' and 'false' and testing with eq, it's
more Perlish to use 1 and undef (or '' or 0) and test the
variable itself as a boolean expression.
> $paragraphLine = "";
> } else { $endOfPara = 'false' }
>
> if ($endOfPara eq 'false') { # keep adding lines if your not at the end
>
> $paragraph = "$paragraph" . "$paragraphLine"; # until a full paragraph
>keep adding lines
Unnecessary quotes again. If the first variable on the
righthand side weren't the same as the lefthand side, then I'd
use one of these:
$paragraph = "$paragraph$paragraphLine";
$paragraph = $paragraph . $paragraphLine;
But since it is the same, use this:
$paragraph .= $paragraphLine;
> } elsif ("$endOfPara" eq 'true') { # builds up list of paragraphs in
>@paras
>
> if ("$paragraph" ne "") {
> push(@paras,$paragraph);
> $paragraph = "";
> $endOfPara = 'false';
> } # end of if paragraph ne ""
> } # end of elsif endofpara eq true
>
>} # end of while PARAGRAPHS, now
> # we're done looking in file paragraphs.txt and list
> # @paras should contain all the paragraphs from the file
Since you want to eliminate duplicates, it's better to use a
hash than an array for the paragraphs. This whole loop can be
replaced with
my($paragraph, %paras);
while(<PARAGRAPHS>) {
next if !/\S/ || /^bad line/;
if (/^00/) {
$paras{$paragraph}++ if $paragraph;
$paragraph = '';
next;
}
$paragraph .= $_;
}
close PARAGRAPHS;
Some people may disagree, but I recommend getting into the habit
of closing files when you're finished with them.
># second, sort the paragraphs and get
># rid of duplicate paragraphs
>
>@paras = sort @paras; # sorts the paragraphs in @paras alphabetically
>
>foreach $para (@paras) { # look through @paras to get rid of duplicates
> $i = $i + 1; # increment counter
> $nextPara = "$paras[$i]"; # since the first paragraphs is $paras[0]...
> if ("$nextPara" ne "$para") {
> push(@uniqParas,$para);
> }
>} # now @uniqParas should not have any duplicates
This whole loop can now be eliminated, since the hash keys won't
have duplicates.
># third, check each paragraph for errors, print out each paragraph
># and each paragraphs errors
># keep a count of paragraphs and corrected paragraphs
>
>foreach $para (@uniqParas) { # check unique paragraphs for errors
>
> if ("$para" =~ /(error)/) { # found an error
> $theError = "$1";
>
> $para =~ s/$theError/<u>$theError<\/u>/g; # underlines the error
> $errors++;
> $errorMsgs = "$errorMsgs\n$errors. found an error\n";
>
> }
> $paraCount++; # increments count of paragraphs checked
> print "Paragraph number $paraCount\n\n";
> print "$para\n";
> print "$errorMsgs\n";
> $errors = ""; # reset error count for next paragraph
> $errorMsgs = ""; # reset error messages for next paragraph
>} # end of checking @uniqParas for errors
This becomes
my($para_count);
for (sort keys %paras) {
my $errors;
my $error_msgs = '';
if ( s{(error)}{<u>$1</u>}g ) {
$errors++;
$error_msgs .= "\n$errors. found an error\n";
}
$para_count++;
print "Paragraph number $para_count\n\n$_\n",
"$error_msgs\n";
}
This preserves your treatment of $errors and $error_msgs, which
doesn't make much sense. As written, you can never find more
than one error per paragraph, so $errors will always be '' or 1,
while $error_msgs will always be '' or "\n1. found an error\n".
I think you need to work on that section.
># print the end of the html file:
>
>print "</pre>
></body>
></html>\n";
print <<EOT;
</pre>
</body>
</html>
EOT
I hope my comments were helpful.
--
Keith C. Ivey <kcivey@cpcug.org>
Washington, DC
------------------------------
Date: 23 Sep 2000 16:43:56 GMT
From: abigail@foad.org (Abigail)
Subject: Re: there must be an easier way to do this
Message-Id: <slrn8spnbv.lo9.abigail@alexandra.foad.org>
Dan and Shelly (wedeking@msa.attmil.ne.jp) wrote on MMDLXXX September
MCMXCIII in <URL:news:8qi8db$j2n$1@newsflood.tokyo.att.ne.jp>:
-- I wrote the below script and it seems to work as I intended it to. But
-- looking at it I wonder what I could have done to avoid using so many
-- variables. Does anyone know of an easier way to do the same thing that the
-- script below does?
--
-- # list of variables:
-- #
-- # $_ - perl's default variable in a while loop for each line in succession
-- # from the first line of text to the last.
-- # gets reset to the next line down by the while loop
-- #
-- # $paragraphLine - $_ is assigned to $paragraphLine if it is a line that
-- # should be added to the $paragraph
-- # gets reset to the next $_ down by the while loop
-- #
-- # $paragraph = collects up the lines of one paragraph
-- # gets reset to "" at end of each new paragraph
-- #
-- # $endOfPara - used to determine when to start the next paragraph
-- # gets reset to 'false' at end of each paragraph
-- #
-- # @paras - list of paragraphs - used to sort and get rid of duplicate
-- paragraphs
-- #
-- # $para - used as a temporary variable to step through @paras list and again
-- # to step through @uniqParas
-- #
-- # $i - incremented by one for each $para in @paras to check for duplicates
-- #
-- # $nextPara - compared to $para, if different, para is added to @uniqParas
-- #
-- # @uniqParas - this is the list of paragraphs that gets checked for errors
-- #
-- # $errors - keeps a count for each paragraph of the number of errors found
-- #
-- # $errorMsgs - collects up all the error messages for each paragraph
-- #
-- # $paraCount - prints out the number of the paragraph being checked
-- #
--
--
-- # first, print out html header, then collect lines into paragraphs
--
-- print "<html>
-- <head>
-- <title>Dan's paragraphs</title>
-- </head>
-- <body>
-- <pre>\n";
Use a here document:
print <<HTML;
<html>
<head>
<title>Dan's paragraphs</title>
</head>
<body>
<pre>
HTML
-- open(PARAGRAPHS,"paragraphs.txt") or
-- die "can't open paragraphs.txt\n";
It's good that you use open, but it's better to put $! in the die message
so that you know why the file could not be opened.
I'd also prefer using a scalar filehandle, and put the file name in
a variable.
-- while(<PARAGRAPHS>) {
--
-- if ($_ =~ /\S+/ && # if the line is not blank
Redundant +.
-- $_ !~ /^bad line/ # and if it's not a bad line
-- ) { # some lines are blank or bad and don't get added to the paragraph
-- $paragraphLine = $_; # step through lines of paragraphs.txt
-- } else { $paragraphLine = "" }
It's often hard to read studlyCaps. This ain't Java. Use all lowercase
variables, with underscores to separate the words. And you're using
a weird indentation style.
However, you can replace all block with:
s/^\s*\n//;
s/^bad line.*//s;
and use $_ instead of $paragraphLine.
But since you don't so anything significant on such lines, you might
as well forget the substitution and go to the next line.
next unless /\S/;
next if /^bad line/;
-- if ("$paragraphLine" =~ /^00/) {
Why quote the $paragraphLine?
-- $endOfPara = 'true';
-- $paragraphLine = "";
-- } else { $endOfPara = 'false' }
Eeek. Perl doesn't have booleans, and coding styles trying to use them
often lead to anti-Perl code. Note that 'false' itself is a *true* value.
So, you could just do:
$endOfPara = s/^00.*//;
However, since you use $endOfPara right after in a test, and then
only once more where you already know the outcome, you can write
it as:
unless (s/^00.*//s) {
# Do stuff when not the end of a paragraph.
}
else {
# End of paragraph stuff.
}
Except that you don't need to substitute $_ as you don't use it.
And you should start your loop for testing whether it's the end of
the paragraph or not, as the first things don't have to be done when
it's not.
-- if ($endOfPara eq 'false') { # keep adding lines if your not at the end
--
-- $paragraph = "$paragraph" . "$paragraphLine"; # until a full paragraph
-- keep adding lines
Redundant quotes. And you haven't initialized $paragraph.
$paragraph .= $_;
-- } elsif ("$endOfPara" eq 'true') { # builds up list of paragraphs in
-- @paras
Redundant quotes and a redundant test. You already know $endOfPara is 'true'.
-- if ("$paragraph" ne "") {
Redundant quotes. And you want
if (length $paragraph) {
which makes it clear that you make a case based on whether $paragraph is
empty or not.
Even better is
next unless length $paragraph.
-- push(@paras,$paragraph);
-- $paragraph = "";
-- $endOfPara = 'false';
Not needed as the test is done for each line anyway.
-- } # end of if paragraph ne ""
-- } # end of elsif endofpara eq true
--
-- } # end of while PARAGRAPHS, now
-- # we're done looking in file paragraphs.txt and list
-- # @paras should contain all the paragraphs from the file
--
-- # second, sort the paragraphs and get
-- # rid of duplicate paragraphs
--
-- @paras = sort @paras; # sorts the paragraphs in @paras alphabetically
--
-- foreach $para (@paras) { # look through @paras to get rid of duplicates
-- $i = $i + 1; # increment counter
Since $i is uninitialized, this should give you a warning. Just use
$i ++;
-- $nextPara = "$paras[$i]"; # since the first paragraphs is $paras[0]...
Redundant quotes and a redundant variable. Just compare $para with
$paras [$i]. However, if $para is the last element, $paras [$i] is
undefined.
-- if ("$nextPara" ne "$para") {
-- push(@uniqParas,$para);
-- }
-- } # now @uniqParas should not have any duplicates
There's an entry in the FAQ explaining how to eliminate duplicates.
It's faster to first weed out duplicates then sort, as sort is
superlinear.
my %seen;
@paras = sort grep {!$seen {$_} ++} @paras;
But you can also check for uniqueness before you push on @para, and
sort (if that's still necessary) when checking for errors.
-- # third, check each paragraph for errors, print out each paragraph
-- # and each paragraphs errors
-- # keep a count of paragraphs and corrected paragraphs
--
-- foreach $para (@uniqParas) { # check unique paragraphs for errors
--
-- if ("$para" =~ /(error)/) { # found an error
-- $theError = "$1";
--
-- $para =~ s/$theError/<u>$theError<\/u>/g; # underlines the error
-- $errors++;
-- $errorMsgs = "$errorMsgs\n$errors. found an error\n";
--
-- }
Or simple:
if ($para =~ s!(error)!<u>$1</u>!g) {
$errors ++;
$error_msgs .= "$errors. Found an error.\n\n";
}
-- $paraCount++; # increments count of paragraphs checked
-- print "Paragraph number $paraCount\n\n";
-- print "$para\n";
-- print "$errorMsgs\n";
-- $errors = ""; # reset error count for next paragraph
Eh, that doesn't make much sense. Since there can be at most one error,
why have a counter? Furthermore, since $error counts, set it to 0.
But most of all, just initialize them on top of the loop.
Also note you don't need the loop variable $para. $_ works fine, and
is convenient because of the substitution.
-- $errorMsgs = ""; # reset error messages for next paragraph
-- } # end of checking @uniqParas for errors
--
-- # print the end of the html file:
--
-- print "</pre>
-- </body>
-- </html>\n";
--
-- # end of program
--
Taking this all together, we'd get (any errors in the code below are left
as an exercise for the reader):
print <<HTML;
<html>
<head>
<title>Dan's paragraphs</title>
</head>
<body>
<pre>
HTML
my $file = 'paragraphs.txt';
open my $fh => $file or die "Failed to open $file: $!\n";
my ($paragraph, @paras, %seen);
while (<$fh>) {
unless (/^00/) {
next unless /\S/;
next if /^bad line/;
$paragraph .= $_;
}
else {
next unless length $paragraph;
push @paras => $paragraph unless $seen {$paragraph} ++;
$paragraph = "";
}
}
my $count;
foreach (sort @paras) {
my ($errors, $error_msgs) = (0, "");
$count ++;
if (s!(error)!<u>$1</u>!g) {
$errors ++;
$error_msgs .= "$errors. Found an error.\n\n";
}
print <<EOT;
Paragraph number $count
$_
$error_msgs
EOT
}
print <<HTML;
</pre>
</body>
</html>
HTML
__END__
--
perl -wle\$_=\<\<EOT\;y/\\n/\ /\;print\; -eJust -eanother -ePerl -eHacker -eEOT
# An eagle flies towards
# the south-east. Mumon sits near
# a rice field. A dove.
------------------------------
Date: Sat, 23 Sep 2000 17:15:06 GMT
From: Jim Mauldin <mauldin@netstorm.net>
Subject: Re: there must be an easier way to do this
Message-Id: <39CCE4F0.4B54E3AE@netstorm.net>
Dan and Shelly wrote:
>
> I wrote the below script and it seems to work as I intended it to. But
> looking at it I wonder what I could have done to avoid using so many
> variables. Does anyone know of an easier way to do the same thing that the
> script below does?
>
[snip]
> open(PARAGRAPHS,"paragraphs.txt") or
> die "can't open paragraphs.txt\n";
>
> while(<PARAGRAPHS>) {
>
> if ($_ =~ /\S+/ && # if the line is not blank
> $_ !~ /^bad line/ # and if it's not a bad line
> ) { # some lines are blank or bad and don't get added to the paragraph
> $paragraphLine = $_; # step through lines of paragraphs.txt
> } else { $paragraphLine = "" }
>
> if ("$paragraphLine" =~ /^00/) {
> $endOfPara = 'true';
> $paragraphLine = "";
> } else { $endOfPara = 'false' }
>
> if ($endOfPara eq 'false') { # keep adding lines if your not at the end
>
> $paragraph = "$paragraph" . "$paragraphLine"; # until a full paragraph
> keep adding lines
>
> } elsif ("$endOfPara" eq 'true') { # builds up list of paragraphs in
> @paras
>
> if ("$paragraph" ne "") {
> push(@paras,$paragraph);
> $paragraph = "";
> $endOfPara = 'false';
> } # end of if paragraph ne ""
> } # end of elsif endofpara eq true
>
> } # end of while PARAGRAPHS, now
> # we're done looking in file paragraphs.txt and list
> # @paras should contain all the paragraphs from the file
>
> # second, sort the paragraphs and get
> # rid of duplicate paragraphs
>
> @paras = sort @paras; # sorts the paragraphs in @paras alphabetically
>
> foreach $para (@paras) { # look through @paras to get rid of duplicates
> $i = $i + 1; # increment counter
> $nextPara = "$paras[$i]"; # since the first paragraphs is $paras[0]...
> if ("$nextPara" ne "$para") {
> push(@uniqParas,$para);
> }
> } # now @uniqParas should not have any duplicates
>
> # third, check each paragraph for errors, print out each paragraph
> # and each paragraphs errors
> # keep a count of paragraphs and corrected paragraphs
>
> foreach $para (@uniqParas) { # check unique paragraphs for errors
>
> if ("$para" =~ /(error)/) { # found an error
> $theError = "$1";
>
> $para =~ s/$theError/<u>$theError<\/u>/g; # underlines the error
> $errors++;
> $errorMsgs = "$errorMsgs\n$errors. found an error\n";
>
> }
> $paraCount++; # increments count of paragraphs checked
> print "Paragraph number $paraCount\n\n";
> print "$para\n";
> print "$errorMsgs\n";
> $errors = ""; # reset error count for next paragraph
> $errorMsgs = ""; # reset error messages for next paragraph
> } # end of checking @uniqParas for errors
>
With some reservations, I believe that the main procedures could be
boiled down to this:
use strict;
my $errors = my $count = 0;
my %paras;
$/= '00';
open(PARAGRAPHS,"paragraphs.txt") or die "can't open paragraphs.txt\n";
while (<PARAGRAPHS>) {
s/^bad line.*?\n|^00//mg;
$errors += s!(error)!<u>$1</u>!g;
$paras{$_} = $count++ unless $paras{$_};
}
close PARAGRAPHS;
%paras keys contain the paragraphs; %paras values contain the paragraph
numbers in the order they first appeared, with duplicates eliminated
(but rare under 'normal' circumstances to have duplicate paragraphs,
from a writer's point of view).
There are some other adjustments you could make. Hope I have properly
understood you data strucutre, and HTH,
-- Jim
------------------------------
Date: Fri, 22 Sep 2000 22:53:36 -0400
From: Jeff Pinyan <jeffp@crusoe.net>
Subject: Re: use vs. @ISA
Message-Id: <Pine.GSO.4.21.0009222250420.18425-100000@crusoe.crusoe.net>
[posted & mailed]
On Sep 22, bdesany@my-deja.com said:
>What's the difference? Is it that subroutines in packages that are
>being "used" are not accessible as object methods, and those in
>packages in @ISA are?
The 'use' compile-time directive includes a module into your program. The
@ISA array of a package (or "class") dictates the parent classes of the
package.
package Person;
sub eat { ... }
package Student;
@ISA = qw( Person );
sub new { ... }
sub learn { ... }
package main;
$jeff = Student->new;
$jeff->learn("Calculus II"); # Student::learn($jeff, 'Calculus II');
$jeff->eat("breakfast"); # Person::eat($jeff, 'breakfast');
--
Jeff "japhy" Pinyan japhy@pobox.com http://www.pobox.com/~japhy/
PerlMonth - An Online Perl Magazine http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc. http://www.perlarchive.com/
CPAN - #1 Perl Resource (my id: PINYAN) http://search.cpan.org/
------------------------------
Date: Sat, 23 Sep 2000 17:31:51 +0100
From: "Marc Liegeois" <marc.liegeois@ic24.net>
Subject: Using perl scripts for picture rollover effect in HTML?
Message-Id: <3Y4z5.43436$hP2.63740@news1-hme0>
I try to find a script to achieve a picture rollover effect using perlscript
in my HTML pages.
The only examples which I have found around are based on javascript.
Could anyone point me to a site hosting an example of such a script, or send
me a sample listing?
Or tell me if this is a silly question altogether (I am new to perl
programming).
Please send your answer to marc.liegeois@ic24.net
Thanks in advance,
Marc
------------------------------
Date: Sat, 23 Sep 2000 15:40:24 GMT
From: Neil Conway <nconway@klamath.dyndns.org>
Subject: variable -> constant?
Message-Id: <39CCCEE7.7040700@klamath.dyndns.org>
Hello all.
I want a subroutine to be passed a regular scalar, and return the value
of a constant (i.e. via 'use constant'). For example:
#!/usr/bin/perl -w
use strict;
use constant FOO => 'hello world';
print &test('FOO'), "\n";
sub test ($) {
my $val;
eval {
$val = $_[0];
};
return $val;
}
This prints 'FOO', I would like it to print 'hello world'. Is this
possible? How would one do this?
Thanks in advance,
Neil
------------------------------
Date: Sat, 23 Sep 2000 16:05:18 GMT
From: Jim Mauldin <mauldin@netstorm.net>
Subject: Re: variable -> constant?
Message-Id: <39CCD48D.FFA9169C@netstorm.net>
Neil Conway wrote:
>
> Hello all.
>
> I want a subroutine to be passed a regular scalar, and return the value
> of a constant (i.e. via 'use constant'). For example:
>
> #!/usr/bin/perl -w
> use strict;
> use constant FOO => 'hello world';
> print &test('FOO'), "\n";
^ ^
[post rearranged]
> This prints 'FOO', I would like it to print 'hello world'. Is this
> possible? How would one do this?
'FOO' sends a string literal to the subroutine. You want:
print &test(FOO), "\n";
> sub test ($) {
> my $val;
> eval {
> $val = $_[0];
> };
> return $val;
> }
The above can be simplified to:
sub test ($) {
shift;
}
-- Jim
------------------------------
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 4413
**************************************