[16611] in Perl-Users-Digest
Perl-Users Digest, Issue: 4023 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Aug 15 11:10:27 2000
Date: Tue, 15 Aug 2000 08:10:16 -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: <966352216-v9-i4023@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Tue, 15 Aug 2000 Volume: 9 Number: 4023
Today's topics:
Re: Pattern match - Extract info from a page (Greg Bacon)
Re: Perfect Place to find Perl Jobs <mjcarman@home.com>
Perl ? - How to do a count? (Louis)
Re: push (@INC <newsposter@cthulhu.demon.nl>
Re: regex <care227@attglobal.net>
setuid question (?) <james@goodwill.globalnet.co.uk>
Re: strict and recursive functions <Uwe.Doetzkies@Dresdner-Bank.com>
Re: strict and recursive functions (Greg Bacon)
Re: strict and recursive functions <mjcarman@home.com>
Re: strict and recursive functions <Uwe.Doetzkies@Dresdner-Bank.com>
Re: translating tr/a-z/A-Z/ best or s/..... (Decklin Foster)
Re: translating tr/a-z/A-Z/ best or s/..... <bcaligari@shipreg.com>
Re: What's the best way to overwrite a string with '*? (Teodor Zlatanov)
Re: What's the best way to overwrite a string with '*? <alex.buell@tahallah.clara.co.uk>
Re: What's the best way to overwrite a string with '*? (Decklin Foster)
Re: What's the best way to overwrite a string with '*? (Greg Bacon)
Re: What's the best way to overwrite a string with '*? <mjcarman@home.com>
Re: What's the best way to overwrite a string with '*? <alex.buell@tahallah.clara.co.uk>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 15 Aug 2000 13:21:45 GMT
From: gbacon@HiWAAY.net (Greg Bacon)
Subject: Re: Pattern match - Extract info from a page
Message-Id: <spigv991kn9117@corp.supernews.com>
In article <8nbdhs$2v4g$1@msunews.cl.msu.edu>,
wu.s <valued_customers@emai.com> wrote:
: Could someone share a piece of code or point me to another source? I want to
: index job information that posted on serveral different websites. Suppose I
: download the following HTML page:
:
: <html>
: <body>
: .....
: <table>
: <tr><td>Title 1</td>
: <td> Category 1 </td>
: <td> <a href="detail1.html" Detail 1 </a> </td>
: </tr>
: <tr><td> Title 2 </td>
: <td> Category 1</td>
: <td> <a href="detail2.html" Detail 2 </a></td>
: </tr>
: ......
: </table>
: </body>
: </html>
:
: How do I extract the three parts and save them into a text file:
:
: Title 1 | Category 1 | detail1.html
: Title 2 | Category 2 | detail2.html
: ......
Use the HTML::Parser module:
#! /usr/local/bin/perl -w
use strict;
use HTML::Parser ();
my @info;
my $tds = 0;
my @stack;
sub start {
my $tag = shift;
my $attr = shift;
return unless $tds || $tag eq 'td';
if ($tds == 3 && $tag eq 'a') {
push @info, [ grep /\S/, @stack, $attr->{href} || "<No URL>" ];
@stack = ();
$tds = 0;
}
else {
++$tds;
}
}
sub text {
return unless $tds;
my $text = shift;
$text =~ s/^\s+//;
$text =~ s/\s+$//;
push @stack, $text;
}
my $p = HTML::Parser->new(
api_version => 3,
start_h => [\&start, "tagname, attr"],
text_h => [\&text, "dtext"],
);
$p->parse_file("file.html");
my @max = (0, 0, 0);
for (@info) {
my @lengths = map length($_), @$_;
for (@max) {
my $v = shift @lengths;
$_ = $v if $v > $_;
}
}
my $fmt = (join ' | ', map "%-${_}s", @max) . "\n";
printf $fmt, @$_ for @info;
Beware that code like this isn't very robust in the face of changing
HTML (e.g., if the owner of the page changes the structure of the
table or the composition of the page).
Greg
--
As in certain cults it is possible to kill a process if you know its true
name.
-- ken and dmr
------------------------------
Date: Tue, 15 Aug 2000 08:06:31 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: Perfect Place to find Perl Jobs
Message-Id: <39994057.ABC8431D@home.com>
sammytalin wrote:
>
> This guy is probably bitter cuz he can't find a job!
Shhh! If they don't know I don't really work there, maybe they'll keep
paying me.
-mjc
------------------------------
Date: 15 Aug 2000 14:55:20 GMT
From: louis@aol.com (Louis)
Subject: Perl ? - How to do a count?
Message-Id: <20000815105520.00174.00003164@ng-ch1.aol.com>
Hi. I was hoping someone might be able to help me out with how to do something
in perl.
Let's say I have a text file that looks like this, with thousands of entries?
John www.somewhere.com
Mary www.jodesigner.com
Tom www.tomsplace.com
John www.somewhere.com
What I want to do is aggragate all the names and display it or write it to a
file.
Total unique users: 1234
john 34
Mary 10
Tom 1
Sarah 12
Total unique urls: 35
www.somewhere.com 432
www.yahoo.com 345
Can anyone show me how to do this? If you could send me email with an example,
I would greatly appreciate it.
Many thanks,
Louis
Louis@aol.com
------------------------------
Date: 15 Aug 2000 14:18:36 GMT
From: Erik van Roode <newsposter@cthulhu.demon.nl>
Subject: Re: push (@INC
Message-Id: <8nbjfs$801$1@internal-news.uu.net>
dschl@my-deja.com wrote:
> Why doesn't push(@INC,...) work?
> My source file contains these lines:
> BEGIN{
> push(@INC, '/var/ns-home/cgi-bin');
> use qqemail;
> }
> When run, I get the error:
> Can't locate qqemail.pm in @INC (@INC contains:
- 'use' gets done compiletime while encountering it.
- BEGIN blocks get executed after they have been fully parsed.
So your code starts parsing the BEGIN block, find and handles the use
(which in this case fails because the push hasn't been done yet). If
it had not failed, parsing would have continued until the BEGIN
block was finished, and then the block would be run (the push). Then
parsing would continue with the rest of the script.
So move the 'use qqemail;' outside of the BEGIN block and it should work.
Erik
------------------------------
Date: Tue, 15 Aug 2000 09:46:56 -0400
From: Drew Simonis <care227@attglobal.net>
Subject: Re: regex
Message-Id: <399949D0.5C8B6286@attglobal.net>
Gwyn Judd wrote:
>
> I was shocked! How could Ala Qumsieh <aqumsieh@hyperchip.com>
> say such a terrible thing:
> >
> >Drew Simonis <care227@attglobal.net> writes:
> >> $string = 1234.567;
> >> $string =~ s/(\d\.\d{2})\d*/$1/ if $string =~ /\d\.\d{3,}/;
> >
> >The if() condition is not really necessary if you change the '*' to a
> >'+' in your s///.
>
> The if isn't needed at all is it?
>
> $string =~ s/(\d\.\d\d)\d*/$1/; # slightly golfed
>
My first solution didn't have an if(), but I thought I'd follow
the request of the OP exactly so I threw it in.
------------------------------
Date: Tue, 15 Aug 2000 14:18:46 +0100
From: "James Goodwill" <james@goodwill.globalnet.co.uk>
Subject: setuid question (?)
Message-Id: <8nbfv0$s05$1@gxsn.com>
Hi,
I have written a UNIX batch scheduler which allows you to run scripts owned
by various UNIX accounts.
Currently the Perl script runs as 'root' user and performs a UNIX 'su' to a
specified account, and then executes a script owned by that account. It
works just fine.
However, what I'd really like to be able to do, is run the scheduler from a
none 'root' account, say a user called 'genbatch' and run jobs owned by
other users on the system, including 'root' user.
I think I need to use setuid to specify the UNIX UID of the account to use,
and then execute the script.
I've seen a couple of postings about setuid, and they suggested using
'wrapsuid'. But this seems to only allow you to run 'root' owned scripts
from a non root user. As I say, I need to run scripts owned by 'root' and
other UNIX users.
Any help would be appreciated.
James G.
------------------------------
Date: Tue, 15 Aug 2000 16:00:17 +0200
From: Uwe Doetzkies <Uwe.Doetzkies@Dresdner-Bank.com>
Subject: Re: strict and recursive functions
Message-Id: <39994CF1.689327F3@Dresdner-Bank.com>
Greg Bacon wrote:
>
> In article <39992CF1.437ABCE8@Dresdner-Bank.com>,
> Uwe Doetzkies <Uwe.Doetzkies@Dresdner-Bank.com> wrote:
>
> ...
> :
> : Global symbol "$cur" requires explicit package name at fak.pl line 9.
> : Execution of fak.pl aborted due to compilation errors.
>
> What did the perldiag manpage have to say about the error message?
# manpage wrote...
# You've said "use strict vars", which indicates that
right.
# all variables must either be lexically scoped (using
# "my"), or explicitly qualified to say which package the
# global variable is in (using "::").
okay. lets try:
-------
#!/usr/bin/perl
use strict;
my $cur;
my $val = shift;
print "$val! = ${\fak($val)}\n";
sub fak {
local $cur = shift;
return 1 if $cur == 1;
return $cur * fak ($cur-1);
}
------
perl says:
Can't localize lexical variable $cur at fak.pl line 10.
# manpage says:
# You used local on a variable name that was
# previously declared as a lexical variable using "my".
# This is not allowed.
why?
# If you want to localize a package
# variable of the same name, qualify it with the package
# name.
no, i don't "want to localize a package variable", i want to use a
variable (normally more, many more then one - the factorial function is
a simple example) in a recursive function. and i don't see why it must
be a package variable.
okay, i can edit the whole "::"-stuff, but this is not, what _i_ want,
it's what _perl_ wants. normally, perl does what _i_ want :-(
uwe
------------------------------
Date: Tue, 15 Aug 2000 14:09:21 GMT
From: gbacon@HiWAAY.net (Greg Bacon)
Subject: Re: strict and recursive functions
Message-Id: <spijoht5kn962@corp.supernews.com>
In article <39994CF1.689327F3@Dresdner-Bank.com>,
Uwe Doetzkies <Uwe.Doetzkies@Dresdner-Bank.com> wrote:
: perl says:
: Can't localize lexical variable $cur at fak.pl line 10.
:
: # manpage says:
: # You used local on a variable name that was
: # previously declared as a lexical variable using "my".
: # This is not allowed.
:
: why?
That's just the rule. Chip Salzenberg submitted a patch that would
allow local lexicals, but people (including Larry) objected, saying
people wouldn't understand it. That makes you a counterexample. :-)
: # If you want to localize a package
: # variable of the same name, qualify it with the package
: # name.
:
: no, i don't "want to localize a package variable", i want to use a
: variable (normally more, many more then one - the factorial function is
: a simple example) in a recursive function. and i don't see why it must
: be a package variable.
Right. The other solution is to use a lexical inside your subroutine:
sub fak {
my $cur = shift;
return 1 if $cur == 1;
return $cur * fak($cur-1);
}
Remember that my() creates a new variable. local() creates a temporary
value for an existing[*] variable.
Greg
[*] Not strictly true, but close enough.
--
In a literary light, if UNIX is the Great Novel, Perl is the Cliffs Notes.
-- Thomas Scoville
------------------------------
Date: Tue, 15 Aug 2000 08:55:07 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: strict and recursive functions
Message-Id: <39994BBB.497842E4@home.com>
Uwe Doetzkies wrote:
>
> what is the problem to use strict and local simultaneous? look at this
> script:
Assuming you aren't using a decrepit version of perl, replace local()
with my(). You only need local() to limit the scope when changing global
vars.
-mjc
------------------------------
Date: Tue, 15 Aug 2000 16:34:09 +0200
From: Uwe Doetzkies <Uwe.Doetzkies@Dresdner-Bank.com>
Subject: Re: strict and recursive functions
Message-Id: <399954E1.D8F82CDA@Dresdner-Bank.com>
Greg Bacon wrote:
> Right. The other solution is to use a lexical inside your subroutine:
>
> sub fak {
> my $cur = shift;
> return 1 if $cur == 1;
> return $cur * fak($cur-1);
> }
>
> Remember that my() creates a new variable. local() creates a temporary
> value for an existing[*] variable.
okay - it works fine. and with my more complicated subroutine too. i
don't know, why it doesn't work two hours ago. maybe the error where
anywhere else.
thank you - you save me from using local in all future recursive
functions :-)
uwe
------------------------------
Date: Tue, 15 Aug 2000 13:54:21 GMT
From: decklin+usenet@red-bean.com (Decklin Foster)
Subject: Re: translating tr/a-z/A-Z/ best or s/.....
Message-Id: <hYbm5.12$CW2.362@news1.rdc1.ct.home.com>
Jimmy Lantz <jimmy.lantz@ostas.lu.se> writes:
> Hi,
> which is the fastet and the most secure /*reliable*/ way to do this(see
> code below)
> The correct values of $some_code could be as follow
> KIN231
> KIN221d
> OSH765
> OSH765h
If we can assume that these take the form qr/[A-Z]{3}\d{3}[a-z]?/,
then use substr as an lvalue:
$_ = 'Kin231D';
tr/a-z/A-Z/;
(substr $_, length($_)-1) =~ tr/A-Z/a-z/;
print;
Of course is you input 'kin' you'll get 'KIn', which may or may not be
a problem.
--
There is no TRUTH. There is no REALITY. There is no CONSISTENCY. There
are no ABSOLUTE STATEMENTS. I'm very probably wrong. -- BSD fortune(6)
------------------------------
Date: Tue, 15 Aug 2000 16:50:21 +0200
From: "Brendon Caligari" <bcaligari@shipreg.com>
Subject: Re: translating tr/a-z/A-Z/ best or s/.....
Message-Id: <8nbkmi$7e4$1@news.news-service.com>
"Brendon Caligari" <bcaligari@shipreg.com> wrote in message
news:8nbb37$sk9$1@news.news-service.com...
> "Jimmy Lantz" <jimmy.lantz@ostas.lu.se> wrote in message
> news:399929E5.43CDCCA@ostas.lu.se...
> > Hi,
> > which is the fastet and the most secure /*reliable*/ way to do this(see
> > code below)
> > The correct values of $some_code could be as follow
> > KIN231
> > KIN221d
> > OSH765
> > OSH765h
> >
> > ########### Working code
> > #!perl
> > $some_code = 'Kin231D'; #exampel of incorrect value to be changed
> > $some_code =~ /\d\d\d/;
> >
> > $a = $`;
> > $b = $&;
> > $c = $';
> > $a =~ tr/a-z/A-Z/;
> > $c =~ tr/A-Z/a-z/;
> >
> > $some_code = $a . $b . $c;
> > print $some_code;
> > exit;
> >
> > Is there any faster solution without having to use $` $& $'
> > I 'v heard that they can slow the process down a little.
> > / Jimmy L.
>
> $some_code = uc($some_code);
> or
> $some_code =~ tr/a-z/A-Z/;
>
doh...failed to notice the non-upcaseable chars at the end..was wondering
what the point was in trying to avoid 'upcasing' a few digits....should stop
looking at monitors for so many hours a day
------------------------------
Date: 15 Aug 2000 09:38:32 -0500
From: tzz@iglou.com (Teodor Zlatanov)
Subject: Re: What's the best way to overwrite a string with '*?
Message-Id: <399947d8$1_2@news.iglou.com>
<oj7ips41l5q3e1mciqtoe91t4i3e5rh571@4ax.com>:Alex Buell (alex.buell@tahallah.clara.co.uk):comp.lang.perl.misc:Tue, 15 Aug 2000 11:44:20 +0100:quote:
: For example, I have a string 'Hello world, I am 29!' that I want to
: overwrite with '*' (asterisks) such that it becomes the following:
: '*********************'.
:
: What's the best way to do this?
# replace all characters in the range 0x01 to 0xFF with '*'
$string =~ tr/\x01-\xFF/*/;
Adjust the range to fit your needs.
--
Teodor Zlatanov <tzz@iglou.com>
"Brevis oratio penetrat colos, longa potatio evacuat ciphos." -Rabelais
------------------------------
Date: Tue, 15 Aug 2000 14:38:24 +0100
From: Alex Buell <alex.buell@tahallah.clara.co.uk>
Subject: Re: What's the best way to overwrite a string with '*?
Message-Id: <mthipso9mm8cujqh56j1gq4fii5lndka8v@4ax.com>
On 15 Aug 2000 09:38:32 -0500, tzz@iglou.com (Teodor Zlatanov) wrote:
># replace all characters in the range 0x01 to 0xFF with '*'
>$string =~ tr/\x01-\xFF/*/;
Thanks!
Cheers,
Alex.
--
Bring on the music and lights!
http://www.tahallah.clara.co.uk
------------------------------
Date: Tue, 15 Aug 2000 13:47:12 GMT
From: decklin+usenet@red-bean.com (Decklin Foster)
Subject: Re: What's the best way to overwrite a string with '*?
Message-Id: <ARbm5.11$CW2.362@news1.rdc1.ct.home.com>
Alex Buell <alex.buell@tahallah.clara.co.uk> writes:
> For example, I have a string 'Hello world, I am 29!' that I want to
> overwrite with '*' (asterisks) such that it becomes the following:
> '*********************'.
>
> What's the best way to do this?
Probably with tr.
--
There is no TRUTH. There is no REALITY. There is no CONSISTENCY. There
are no ABSOLUTE STATEMENTS. I'm very probably wrong. -- BSD fortune(6)
------------------------------
Date: Tue, 15 Aug 2000 14:11:06 GMT
From: gbacon@HiWAAY.net (Greg Bacon)
Subject: Re: What's the best way to overwrite a string with '*?
Message-Id: <spijrqrmkn990@corp.supernews.com>
In article <399947d8$1_2@news.iglou.com>,
Teodor Zlatanov <tzz@iglou.com> wrote:
: # replace all characters in the range 0x01 to 0xFF with '*'
: $string =~ tr/\x01-\xFF/*/;
Even easier:
$string =~ tr//*/c;
Greg
--
Must one first batter their ears, that they may learn to hear with
their eyes? Must one clatter like kettledrums and penitential
preachers? Or do they only believe the stammerer?
-- Nietzsche
------------------------------
Date: Tue, 15 Aug 2000 08:37:14 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: What's the best way to overwrite a string with '*?
Message-Id: <3999478A.D8BE4A96@home.com>
Alex Buell wrote:
>
> For example, I have a string 'Hello world, I am 29!' that I want to
> overwrite with '*' (asterisks) such that it becomes the following:
> '*********************'.
my $string = 'Hello world, I am 29!';
$string = '*' x length($string);
The obvious question is, what's the point? Taking a WAG, I'd say you
want to display asterisks instead of characters in a password prompt. If
so, this isn't the way. Look at perlfaq8 "How to I ask the user for a
password?" or perlfaq9 "How do I put a password on my web pages?"
-mjc
------------------------------
Date: Tue, 15 Aug 2000 15:51:02 +0100
From: Alex Buell <alex.buell@tahallah.clara.co.uk>
Subject: Re: What's the best way to overwrite a string with '*?
Message-Id: <f5mipskam9akth9l9drap0ehis51sr2cj0@4ax.com>
On Tue, 15 Aug 2000 08:37:14 -0500, Michael Carman <mjcarman@home.com>
wrote:
>The obvious question is, what's the point? Taking a WAG, I'd say you
>want to display asterisks instead of characters in a password prompt. If
>so, this isn't the way. Look at perlfaq8 "How to I ask the user for a
>password?" or perlfaq9 "How do I put a password on my web pages?"
No actually, it's not for the password thing.
Rather, it's displaying a text string that I overwrite with asterisks.
Cheers,
Alex.
--
Bring on the music and lights!
http://www.tahallah.clara.co.uk
------------------------------
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 4023
**************************************