[21714] in Perl-Users-Digest
Perl-Users Digest, Issue: 3918 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Oct 5 06:06:28 2002
Date: Sat, 5 Oct 2002 03:05:09 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sat, 5 Oct 2002 Volume: 10 Number: 3918
Today's topics:
Re: Eliminating right-end whitespace <goldbb2@earthlink.net>
Re: ignore case in Greek <aragorn@freemail.gr.gr>
Re: ithreads, perl 5.8 and shared objects <uri@stemsystems.com>
Re: ithreads, perl 5.8 and shared objects <derek@wedgetail.com>
Re: ithreads, perl 5.8 and shared objects <uri@stemsystems.com>
Kill current process(es) (BUCK NAKED1)
Re: Kill current process(es) <nospam@nospam.com>
Re: nice comments in linux src (Vishal Agarwal)
Re: Script to Change Filename <krahnj@acm.org>
Re: Script to Change Filename <bongie@gmx.net>
Re: Script to Change Filename <wsegrave@mindspring.com>
Re: Script to Change Filename <wsegrave@mindspring.com>
Re: Security-Hole in module Safe.pm <goldbb2@earthlink.net>
Re: Security-Hole in module Safe.pm <rgarciasuarez@free.fr>
What does [^\n] mean <No_Mail_Address@cox.net>
Re: What does [^\n] mean <nospam@nospam.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 05 Oct 2002 01:13:42 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Eliminating right-end whitespace
Message-Id: <3D9E7506.EACAF076@earthlink.net>
Amittai Aviram wrote:
>
> Is there a function in core Perl that gets rid of all (any number of)
> whitespace characters from the right or left end of a string? (This
> would be equivalent to PHP's ltrim() and rtrim().) I know about
> chop() and chomp(), but chop only chops one character off and chomp
> only gets rid of the newline or record separator, if I'm not mistaken.
> Would I have to use a regular expression? Thanks!
Functions which return the trimmed string:
sub rtrim {
wantarray ?
map unpack('A*', $_), @_ :
unpack('A*', $_[0])
}
sub ltrim {
wantarray ?
map m/\s*(.*)/s, @_ :
( $_[0] =~ m/\s*(.*)/s )[0];
}
Functions which trim in-place:
sub rtrim {
$_ = unpack('A*', $_) for @_;
wantarray ? @_ : $_[0];
}
sub ltrim {
s/\s*// for @_;
wantarray ? @_ : $_[0];
}
[untested]
--
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]
------------------------------
Date: Sat, 5 Oct 2002 11:13:56 +0300
From: "aragorn" <aragorn@freemail.gr.gr>
Subject: Re: ignore case in Greek
Message-Id: <anm703$q3d$1@nic.grnet.gr>
the solution Andre seggested worked just fine
thanks
------------------------------
Date: Sat, 05 Oct 2002 04:25:17 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: ithreads, perl 5.8 and shared objects
Message-Id: <x7d6qpseab.fsf@mail.sysarch.com>
>>>>> "DT" == Derek Thomson <derek@wedgetail.com> writes:
DT> Hi Uri,
please don't stealth cc. mark it as mailed and posted.
DT> Sure, I can avoid using threads with an event driven
DT> model. However, sometimes that is not sufficient - what if the
DT> server calls itself directly or indirectly? There's also the
DT> problem that you have no chance of exploiting potiential
DT> parallelism ie. all requests must wait until the current request
DT> has completed - even if the request is just waiting for a result
DT> from a database, or whatever. Other requests, that don't require
DT> database access, could have been completed during that time.
if you divide the system into separate processes and use a clean IPC
(which stem can do trivially via message passing) you can do it all with
events. in fact i am finishing a multibox complex crawler/search engine
that could be done easily with threads. it has multiple DBI proxy
processes which support parallel DB access from a single server. in fact
the number of processes can be changed without any additional coding,
just by changing some config params. so there is no DB blocking in that
system.
and what do you mean by the server calling itself directly or
indirectly? with message passing that is less than trivial, you just
send a message to yourself (or whatever). as for exploiting parallelism
the above mentioned project partly on a 4 way cpu and it uses processes
and message passing to get parallelism. in fact when we added that
feature, the time for a multiple query search went down by factor that
was almost linear with the number of parallel processes running. the key
to parallel processing is the IPC and message passing can be simpler and
cleaner than threads and locks. also it is more scalable as the same
architecture can be spread to multiple boxes with no new coding and
threads can't do that at all.
threads have their place but the do everything with threads bandwagon is
just so overloaded. threads have many major weaknesses which are just
not mentioned enough. i like to point them out. events also have
weaknesses too. but since they are not as popular as threads they don't
get slammed. the biggest diff is that they require some intelligence to
work with. simple threads (which don't do much anyhow) can be coded by
kiddies. :) anything more complex and you have to really think hard
about sharing and mutexes and locks.
DT> Besides that, I get asked "does Perl support threads?" quite a bit
DT> - I'd like to be able to say "yes" confidently, without fear that
DT> they are going to discover that shared objects don't work.
5.8 is supposed to be thread stable. i don't use 5.8 yet and i doubt i
will for at least a year. i stick with older more stable versions of perl.
DT> Thanks for your answer, but I really need to know if shared objects do
DT> in fact work.
i get that and i can't answer it. i have been doing event loop designs
for 20 years and i just stay away from threads if i can (i have worked
with them). life is simpler with a single global data space which needs
no locks or sharing and with real parallel scalability. :)
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Sat, 05 Oct 2002 16:15:19 +1000
From: Derek Thomson <derek@wedgetail.com>
Subject: Re: ithreads, perl 5.8 and shared objects
Message-Id: <3d9e8390$0$18870$afc38c87@news.optusnet.com.au>
Uri Guttman wrote:
>>>>>>"DT" == Derek Thomson <derek@wedgetail.com> writes:
>>>>>
>
> DT> Hi Uri,
>
> please don't stealth cc. mark it as mailed and posted.
I'll just stick to posting I think :(
>
> DT> Sure, I can avoid using threads with an event driven
> DT> model. However, sometimes that is not sufficient - what if the
> DT> server calls itself directly or indirectly? There's also the
> DT> problem that you have no chance of exploiting potiential
> DT> parallelism ie. all requests must wait until the current request
> DT> has completed - even if the request is just waiting for a result
> DT> from a database, or whatever. Other requests, that don't require
> DT> database access, could have been completed during that time.
>
> if you divide the system into separate processes and use a clean IPC
> (which stem can do trivially via message passing) you can do it all with
> events. in fact i am finishing a multibox complex crawler/search engine
> that could be done easily with threads. it has multiple DBI proxy
> processes which support parallel DB access from a single server. in fact
> the number of processes can be changed without any additional coding,
> just by changing some config params. so there is no DB blocking in that
> system.
The only way to achieve this that I'm aware of is to hook the DB into
your event loop. This is too much effort for most people and it means
that you need to do it not just for databases, but for everything you
could potentially block on. File I/O, GUI events, anything. Whereas with
threads, the thread scheduler gives you all that.
>
> and what do you mean by the server calling itself directly or
> indirectly?
Let's say a client calls method 'foo' on object 'A' in one process.
Method 'foo' now calls method 'bar' on object 'B' in another process. As
part of the execution of method 'bar', method A.fubar is called. As
A.foo is already being handled in A's process, we have a deadlock situation.
The trivial (direct) case is when A.foo calls A.fubar directly, but that
can be avoided by having the ORB (or whatever infrastructure) skip the
message dispatch by detecting that it's a local call. That's impossible,
however, in the indirect case described above.
> with message passing that is less than trivial, you just
> send a message to yourself (or whatever). as for exploiting parallelism
> the above mentioned project partly on a 4 way cpu and it uses processes
> and message passing to get parallelism. in fact when we added that
> feature, the time for a multiple query search went down by factor that
> was almost linear with the number of parallel processes running. the key
> to parallel processing is the IPC and message passing can be simpler and
> cleaner than threads and locks. also it is more scalable as the same
> architecture can be spread to multiple boxes with no new coding and
> threads can't do that at all.
I don't really understand most of that, as I'm not familiar with this
project the you're talking about. Speaky generically, you have two
choices if you want to get useful work done while blocking on I/O 1) You
use threading, or 2) You make the I/O handler part of the event loop.
2) Is harder to get right, and has it's own inherent problems, as it
will require tweaking as you've described to take advantage of multiple
CPUs. Not saying that threading is not without problems, but I think the
Perl threading model, with it's "everything is separate by default"
model, sheds the first new light on the problem in a long time! I'm very
interested in applying it and seeing how it works.
>
> threads have their place but the do everything with threads bandwagon is
> just so overloaded. threads have many major weaknesses which are just
> not mentioned enough. i like to point them out. events also have
> weaknesses too. but since they are not as popular as threads they don't
> get slammed. the biggest diff is that they require some intelligence to
> work with. simple threads (which don't do much anyhow) can be coded by
> kiddies. :) anything more complex and you have to really think hard
> about sharing and mutexes and locks.
I take your point about threading. Trust me, I understand the issues.
The worst thing that happened to threading was Java. People can just
type "new Thread()" or whatever and get a new thread. Previously, you
had to go through the pthreads API, and that meant you'd probably read a
good-sized book or two on threads/parallelism, and understood the
underlying issues. Everyone's doing it at a whim now, with predictable
results ;)
However, I can't see how you avoid locking - with events you just force
everything to be serialized, therefore you've lost any potential
parallelism. And if you do hook your I/O events into the event loop,
you're back to worrying about race conditions again (although DB
transactions might deal with some of that for you, but what about files?).
>
> DT> Besides that, I get asked "does Perl support threads?" quite a bit
> DT> - I'd like to be able to say "yes" confidently, without fear that
> DT> they are going to discover that shared objects don't work.
>
> 5.8 is supposed to be thread stable. i don't use 5.8 yet and i doubt i
> will for at least a year. i stick with older more stable versions of perl.
Yes, but I want to try ithreads. And the lack of thread support is
really hurting any attempts I make to use Perl in the "enterprise" (*)
space.
(*) Yes, meaningless terms'r'us. Let's just say "instead of Java" :)
>
> DT> Thanks for your answer, but I really need to know if shared objects do
> DT> in fact work.
>
> i get that and i can't answer it. i have been doing event loop designs
> for 20 years and i just stay away from threads if i can (i have worked
> with them). life is simpler with a single global data space which needs
> no locks or sharing and with real parallel scalability. :)
I respectfully question this "real parallel scalability" assertion. I
think you're being just a teensy bit zealot-ish here. I'm sure I can do
just as well with a threaded server, and you can't *really* avoid race
conditions (and locking) unless you throw parallelism out. I don't see
how your model as described can really exploit multiple CPUs, without
exchanging more messages between seperate processes, which is certainly
a little inelegant. Sure, it probably works in specific cases, but I'm
writing an ORB, so I can't be telling people how they should be using it
- I just have to give them all the options: that is, threads *and* event
loops.
I'm starting with threads, as that's what people expect, it's easier to
get started with, and damnit, I want to show that it *works* in Perl!
Thanks for you reply anyhow,
Derek Thomson.
------------------------------
Date: Sat, 05 Oct 2002 06:49:02 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: ithreads, perl 5.8 and shared objects
Message-Id: <x7vg4hqt29.fsf@mail.sysarch.com>
>>>>> "DT" == Derek Thomson <derek@wedgetail.com> writes:
DT> The only way to achieve this that I'm aware of is to hook the DB into
DT> your event loop. This is too much effort for most people and it means
DT> that you need to do it not just for databases, but for everything you
DT> could potentially block on. File I/O, GUI events, anything. Whereas
DT> with threads, the thread scheduler gives you all that.
nope. you can use proxy processes to handle blocking events. gui events
are not blocking and already have event loops. POE has the ability to
use any of them and stem will too (when i steal the poe code :).
>> and what do you mean by the server calling itself directly or
>> indirectly?
DT> Let's say a client calls method 'foo' on object 'A' in one
DT> process. Method 'foo' now calls method 'bar' on object 'B' in another
DT> process. As part of the execution of method 'bar', method A.fubar is
DT> called. As A.foo is already being handled in A's process, we have a
DT> deadlock situation.
not with message passing since all remote calls are asynchronous. no
deadlocks are possible unless you try hard. timeouts can always be used
to get out of them too.
DT> The trivial (direct) case is when A.foo calls A.fubar directly, but
DT> that can be avoided by having the ORB (or whatever infrastructure)
DT> skip the message dispatch by detecting that it's a local call. That's
DT> impossible, however, in the indirect case described above.
again with good message passing that is trivial. a stem message can be
sent to a local or remote object just as easily. the advantage is that
the code never needs to know anything about the location of the
destination object except for an address that can be set from an
external configuration.
DT> I don't really understand most of that, as I'm not familiar with this
DT> project the you're talking about. Speaky generically, you have two
DT> choices if you want to get useful work done while blocking on I/O 1)
DT> You use threading, or 2) You make the I/O handler part of the event
DT> loop.
nope. multiple processes and IPC is another solution. stem does all the
work for that already. since you don't know about it, take a gander at
stemsystems.com
DT> 2) Is harder to get right, and has it's own inherent problems, as it
DT> will require tweaking as you've described to take advantage of
DT> multiple CPUs. Not saying that threading is not without problems,
DT> but I think the Perl threading model, with it's "everything is
DT> separate by default" model, sheds the first new light on the
DT> problem in a long time! I'm very interested in applying it and
DT> seeing how it works.
not really harder. just a different mental model. many people have
trouble with event models and the best way to use them. they seem to
ask, "who calls me?" and don't understand the concept of non-blocking
code. if you need blocking either use threads (events and threads can
coexist and i have done that), or multiple processes. the key is a good
IPC that makes life easy. stem has that built in. sending a message to a
local object or a remote one is the same call.
DT> However, I can't see how you avoid locking - with events you just
DT> force everything to be serialized, therefore you've lost any
DT> potential parallelism. And if you do hook your I/O events into the
DT> event loop, you're back to worrying about race conditions again
DT> (although DB transactions might deal with some of that for you,
DT> but what about files?).
you keep thinking in the single process model. think multiprocess and
proxies for that. stem already has a DBI proxy which can be run in
multiple processes which fixes the single queue issue. and a remote file
module is under development (i hope one of the stem apprentices will do
it).
>> i get that and i can't answer it. i have been doing event loop
>> designs for 20 years and i just stay away from threads if i can (i
>> have worked with them). life is simpler with a single global data
>> space which needs no locks or sharing and with real parallel
>> scalability. :)
DT> I respectfully question this "real parallel scalability" assertion. I
DT> think you're being just a teensy bit zealot-ish here. I'm sure I can
DT> do just as well with a threaded server, and you can't *really* avoid
DT> race conditions (and locking) unless you throw parallelism out. I
DT> don't see how your model as described can really exploit multiple
DT> CPUs, without exchanging more messages between seperate processes,
DT> which is certainly a little inelegant. Sure, it probably works in
DT> specific cases, but I'm writing an ORB, so I can't be telling people
DT> how they should be using it - I just have to give them all the
DT> options: that is, threads *and* event loops.
well, the whole idea is to HAVE parallelism. throwing it out makes no
sense. the limit of threads to a single box is a bigger problem
IMO. true scalability means not limiting a solution to a single box. and
message passing is used in many larger projects. did you know that there
are several commercial message passing systems out there including
mqseries (ibm), tibco, sonic (progress) and of course our favorite msmq
from redmond. i may be a zealot about message passing but there are
plenty of others who like it too. i agree it is not as 'simple' to code
as threads for simple programs. but when the system gets larger, it can
be easier as you lose the mutex and lock problems and can scale as
needed.
and i say give them both options if you can design it that way. one day
when 5.8 is really stable, i will add thread support to stem. but not
for a little while. i hear POE and threads already have problems playing
well together.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Sat, 5 Oct 2002 01:06:25 -0500 (CDT)
From: dennis100@webtv.net (BUCK NAKED1)
Subject: Kill current process(es)
Message-Id: <17837-3D9E8161-131@storefull-2276.public.lawson.webtv.net>
I've looked at the kill and signal docs and don't quite understand how
to use them. If you want to make sure that all processes are killed in a
perl script file, in case it hangs or the user backs out. Will this do
it?
END {
kill -9 $_;
}
------------------------------
Date: Sat, 5 Oct 2002 00:38:17 -0700
From: "Tan Nguyen" <nospam@nospam.com>
Subject: Re: Kill current process(es)
Message-Id: <3d9e9660$1_7@nopics.sjc>
"BUCK NAKED1" <dennis100@webtv.net> wrote
> I've looked at the kill and signal docs and don't quite understand how
> to use them. If you want to make sure that all processes are killed in a
> perl script file, in case it hangs or the user backs out. Will this do
> it?
>
> END {
> kill -9 $_;
> }
If your script hangs. the piece of code within END block will never be
executed, so it has no effect. If the user interrupts the script, the END
block doesn't have a chance to run either unless you trap the signal and
gracefully exit.
You probably meant kill 9 => -$$ . This will _forcefully_ kill the current
process and any children it has spawned or any of its parents (unless you
change its group id). When you send a signal to a negative pid, you will
send the signal to the entire group that pid belongs to. In your case, you
might want to kill your processes nicely by sending a TERM (15) signal
instead of a KILL (9) signal. kill 15 => -$$. This will give the processes
a chance to clean up before exitting.
------------------------------
Date: 5 Oct 2002 01:52:03 -0700
From: avishal@vsnl.net (Vishal Agarwal)
Subject: Re: nice comments in linux src
Message-Id: <be1aeb16.0210050052.838ea12@posting.google.com>
"Tassilo v. Parseval" <Tassilo.Parseval@post.rwth-aachen.de> wrote in
[snip]
> Since this is meant to be a funny piece of code, I am just commenting
> about one thing that will make it run much quicker. Yet, one thing
> almost got me: the use of symbolic references. It took me a whole to
> find out how you actually used the arrays with the patterns that you
> defined at the beginning. It's probably better to populate a hash with
> array-refs.
>
Thanks Tassilo, its much faster now! BTW, I also incorporated some
changes suggested by perl monks guys.. so here's the latest one:
> Still, it's a fun piece of script. I wonder whether this also works on
> the Perl source tree. Heh, it does. :-)
>
> ./pp.c
> ------
> /* Do I look like I trust gcc with long longs here?
> Do I hell. */
Haven't tried it on other sources, but i see no reason why it
shouldn't...provided the comments are extracted properly (like it's
done for C type comments)
:o)
Once again thanks Tassilo!
-Vishal
================== <CODE START> ==============
#!/usr/bin/perl
# Author: avishal@vsnl.net
# Date: 5 Oct 2002
# Version: 0.4 (faster than before, thanks Tassilo)
# Usage: program_name [ENTER] /* assuming you're in /usr/src/linux/
(and linux .c and .h are present) */
# Synopsis: print all (nice) comments in .c/.h files underneath
#categories (improved by: Tassilo.Parseval@post.rwth-aachen.de)
@hard = map { qr/$_/ }
qw# damn \b[fsdl]uck sh[ia]t [^sS]hello? piss fart
\bwtf\b\barse\b \bass\b bitch moron stupid bsex #;
@mild = map { qr/$_/ }
qw# \b[ht]umou?r(ous|\b) \bhillarious \bbeaut heaven cow
\bm[ou]m\b mother\b bible love gods? myst fish thank
please crazy mad fun(ny|\b) (in)?sane shame sick
\bugly \bdumb silly \bhate die\b lunatic horrible
(d|\b)evil dedicate #;
@really_mild = map { qr/$_/ }
qw# window(s|\$) micro(s|\$)oft mess\b \bh[ae]ck(er|\b)
\bhol(e|y|\b) dead prett(y|ie|\b) weird blind hopeless
\bbomb\b #;
@grunts = map { qr/$_/ }
qw# \b(ha){2,} \b(he){2,} \bwuff \b[yh]i[^tg] \bugh \bo{2,}ps
\bphew+ \bgrr \bduh \bargh \bwhoo \bwou \!\! \?\? #;
$req = 6; # $points required to pass
use File::Find; # some perl-wisdom bestowed upon me
find({ wanted => \&print_comments, follow => 1 }, '.');
sub print_comments {
return unless /\.(c|h)$/ && -f && -r; # plain, readable .c/.h file
$filename = $File::Find::fullname;
return unless (open(FH,"<$filename"));
undef @nice_comments;
undef $/;
$_ = <FH>;
@comments = m|(/\*.*?\*/)|gsx;
foreach (@comments) {
$comment_lines = (tr/\n//) + 1;
next if $comment_lines > 4; # ignore REALLY big comments
$points = (4-$comment_lines); # shorter is preferred (well, some
times)
$points += &do_count('hard',$_)*5;
$points += &do_count('grunts',$_)*4 if ($points < $req);
# don't have to take another test if you've already got passing
marks
$points += &do_count('mild',$_)*3 if ($points < $req);
$points += &do_count('smileys',$_)*3 if ($points < $req);
$points += &do_count('really_mild',$_)*2 if ($points < $req);
$points += &do_count('wtfs',$_) if ($points < $req);
$points ++ if defined(@nice_comments); # probability increases
exponentially
push @nice_comments, $_ if $points >= $req;
}
undef %saw; # to remove duplicates
@nice_comments = grep(!$saw{$_}++, @nice_comments);
if (defined(@nice_comments)) {
print "$filename\n","-"x(length($filename)),"\n";
print "$_\n" foreach @nice_comments;
print "_"x75,"\n";
}
}
# count the occurances of each $keyword (supplied with categories) in
the given $cmt
sub do_count {
my ($kw,$cmt) = @_;
$points = 0;
if ($kw =~ /smileys/) { # not more than 2 points per comment
$points++ if /\s(\:|\;|\$|(8-)|(B-))(o|O|\-|\=)?(\)|\(|\]|\[|P|D|S|O|\@|\/|\\|X|\|)\s/;
# :o)
$points++ if /\s(\)|\(|\]|\[|S|D|O|\/|\\|X|\|)(o|O|\-|\=)(\:|\;|\$)\s/; #
(O:
} elsif ($kw =~ /wtfs/i) {
$points++ if /\s((wh?at|where|wh?en|(wh)?y|who|how|eh).*(\?|\!))/i;
} else {
return 0 unless (defined(@$kw));
foreach (@$kw) {
$points++ if $cmt =~ /$_/i;
}
}
return $points;
}
================== <CODE ENDS> ==============
------------------------------
Date: Sat, 05 Oct 2002 04:15:22 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Script to Change Filename
Message-Id: <3D9E6754.B272925B@acm.org>
JP wrote:
>
> The script displays a list of files with the names which are exactly what I
> need. It leaves other un-related files untouched. For now, it is just
> showing the new filenames. I have to figure out how to do the actual
> rename.
perldoc -f rename
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sat, 05 Oct 2002 06:44:23 +0200
From: "Harald H.-J. Bongartz" <bongie@gmx.net>
Subject: Re: Script to Change Filename
Message-Id: <1247807.NLplHqR3Oh@nyoga.dubu.de>
William Alexander Segraves wrote:
> s/^((?:\d){3,8})([a-zA-Z]\.dat)$/${1}-${2}/; #regex from David
^^^^^^
Could someone please explain to me why this strange complication of \d
is occuring repeatedly in this thread? I cannot see the advantage of
'((?:\d){3,8})' compared with '(\d{3,8})', as suggested by others.
> Britten, with mods from Tad McClellan
> in place of David's regex to meet revised spec.
Ciao,
Harald
--
Harald H.-J. Bongartz <bongie@gmx.net>
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
$r='$_=reverse q q}_@tnirp{print busq;eval;';eval$r;$r^=pack'c39',eval
"qw'".'0 'x17 .'4 28 0 28 4 0 30 29 29 15 84 '.'0 'x11 ."'";eval$r;
aton(74.117.115.116)=>aton(32.97.110.111)=>aton(116.104.101.114);
aton(32.80.101.114)=>aton(108.32.104.97)=>aton(99.107.101.114);
------------------------------
Date: Sat, 5 Oct 2002 01:07:20 -0500
From: "William Alexander Segraves" <wsegrave@mindspring.com>
Subject: Re: Script to Change Filename
Message-Id: <anm2qs$1s6$1@slb6.atl.mindspring.net>
"JP" <NO_SPAM_pangjoe@rogers.com> wrote in message
news:Mgtn9.20894$Aiq1.19462@news04.bloor.is.net.cable.rogers.com...
<snip>
> #! Perl
> use strict;
> my $dir = 'usr/whatever';
> opendir (DIR, $dir) or die "Cannot locate directory $dir:$!\n";
>
> foreach (readdir DIR) {
> s/^((?:\d){3,8})([a-zA-Z]\.dat)$/${1}-${2}/;
> print $_, "\n";
> }
<snip>
JP.
I'm seeing some signs here that you may not be testing your script as you're
developing it. Did you try
perl -c yourscript.pl
at the command line (MS-DOS Prompt) window on your Windows computer?
IMO, your development routine should include a thorough search of the
resources available to you to try to find the answers to your own questions
before bringing them here. Whilst searching, you'll learn other things about
Perl that may not apply now, but will apply later.
I'm certainly not an expert, having only done Perl for a few years; but
rather, I'm still a (grateful) learner at the feet of the Masters who are
good enough to help when we go astray. One of the reasons I contribute is to
give back some of what I've received/learned, thanks to the generosity of
the experts who share their know-how here.
>
> The script displays a list of files with the names which are exactly what
I
> need. It leaves other un-related files untouched. For now, it is just
> showing the new filenames. I have to figure out how to do the actual
> rename.
We've already told you how to do that. Please review the suggestions that
preceded your current post. You may wish to Google over to the whole thread
when you have time, and review all of the suggestions that have been given
to you.
One precaution: Be certain you _really_ want to rename files before you turn
this thing loose on data about which you care alot. You'll see from my
script that I'm testing in a temp directory, rather than using a directory
that contains valuable data.
Here's my latest version, combining some of what you wrote above with some
of the previous suggestions, with no attempts toward consistent style.
Remember TIMTOWDI. And fix the line-wraps introduced by Outlook Express
before you use this.
#!perl -w
# filedasher_v4.pl- adds dashes into filenames, e.g., 12345a.dat becomes
12345-a.dat
use strict;
my $dir = 'c:\indigoperl\tmp'; #my path
opendir (DIR, $dir) or die "Cannot locate directory $dir:$!\n";
foreach (readdir DIR) {
my $old = $_;
s/^((?:\d){3,8})([a-zA-Z]\.dat)$/${1}-${2}/; #regex from David Britten, with
mods from Tad McClellan
my $new = $_;
unless ($old eq $new){
print "Renaming $old to $new\n";
rename ($old, $new) || warn "Can't rename $old to $new: $!";
}
}
I used the bunch of files in c:\indigoperl\tmp to run this script. It works
fine on Win32, so far. Your testing should include scenarios aimed at
testing the "or die" and "|| warn" branches, e.g., I just renamed directory
tmp to temp to (successfully) check the "or die" branch.
Testing the "|| warn" branch is a little trickier, I've discovered, as the
script routinely ignores Win95(32bit) file permissions (such as they are,
i.e., none). OTOH, I was able to successfully test the branch just now by
creating an Access97 database 123d.dat and opening it. The script warned it
couldn't rename the file (Permission denied at ...). On closing Access, I
was able to successfully rename the file with the script.
Well, Joe. That's about it for me for tonight. I'm confident others can
produce something far more elegant than the above. You should try to learn
from allof the suggestions, especially those of the experts on this ng.
Thanks for letting me learn a bit more along with you. And a big "Thank
You!" to those who shortened _my_ search for answers with useful
suggestions.
Cheers.
Bill Segraves
------------------------------
Date: Sat, 5 Oct 2002 02:03:07 -0500
From: "William Alexander Segraves" <wsegrave@mindspring.com>
Subject: Re: Script to Change Filename
Message-Id: <anm2qt$1s6$2@slb6.atl.mindspring.net>
"Harald H.-J. Bongartz" <bongie@gmx.net> wrote in message
news:1247807.NLplHqR3Oh@nyoga.dubu.de...
> William Alexander Segraves wrote:
> > s/^((?:\d){3,8})([a-zA-Z]\.dat)$/${1}-${2}/; #regex from David
> > Britten, with mods from Tad McClellan
> > in place of David's regex to meet revised spec.
> ^^^^^^
> Could someone please explain to me why this strange complication of \d
> is occuring repeatedly in this thread? I cannot see the advantage of
> '((?:\d){3,8})' compared with '(\d{3,8})', as suggested by others.
Perhaps those who made the other suggestions can explain the advantages of
the latter over the former. Using the latter, i.e.,
# change to test Harald H.-J. Bongartz issue
s/^(\d{3,8})([a-zA-Z]\.dat)$/${1}-${2}/;
appears to work the same for the examples I had tested. Frankly, I hadn't
tested the other suggestions, as David Britten's regexp, with Tad
McClellan's suggested mods, worked fine.
Thanks for mentioning this, Harald. You've aroused my curiosity. After
several reads of pertinent material in the Camel Book, however, what I've
found is I need to study it in more depth (after a good night's sleep).
Cheers.
Bill Segraves
------------------------------
Date: Sat, 05 Oct 2002 00:09:59 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Security-Hole in module Safe.pm
Message-Id: <3D9E6617.476C1C4D@earthlink.net>
Rafael Garcia-Suarez wrote:
>
> Andreas Jurenda wrote in comp.lang.perl.misc :
> > Safe::reval() execute a given code in a safe compartment.
> >
> > But this routine has a one-time safeness.
> > If you call reval() a second (or more) time with the same
> > compartment, you are potential unsafe.
> >
> > These depends on the values of @_ at the entrypoint of the safe
> > compartment.
>
> While we're at it, and in a full-disclosure mood, a similar bug
> affects Safe::rdo(). It's fixed in the development version of Perl.
>
> I'd like to stress the fact that the only way to be absolutely sure
> that no particular opcode will be executed by perl is to compile a
> separate perl without this opcode. I think this is easy : deleting the
> appropriate lines from opcode.pl and running "make regen_headers"
> before "make" should be enough.
This seems to be a bit much work for such a trivial result... surely it
would be sufficient to use the 'ops' pragma to disable opcodes?
open( my $safe, "-|", $^X, "-Mops=:default", "-e", $str )
or die horribly;
print while <$safe>;
close $safe or die horribly;
--
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]
------------------------------
Date: 05 Oct 2002 08:40:39 GMT
From: Rafael Garcia-Suarez <rgarciasuarez@free.fr>
Subject: Re: Security-Hole in module Safe.pm
Message-Id: <slrnapt9kq.jk.rgarciasuarez@rafael.example.com>
Benjamin Goldberg wrote in comp.lang.perl.misc :
> Rafael Garcia-Suarez wrote:
>> I'd like to stress the fact that the only way to be absolutely sure
>> that no particular opcode will be executed by perl is to compile a
>> separate perl without this opcode. I think this is easy : deleting the
>> appropriate lines from opcode.pl and running "make regen_headers"
>> before "make" should be enough.
>
> This seems to be a bit much work for such a trivial result... surely it
> would be sufficient to use the 'ops' pragma to disable opcodes?
Not for the truly paranoids ;-) the C code corresponding to the perl
opcode is still there. -- and 'ops' has unfortunaly a global and
irreversible effect, making it difficult to use.
------------------------------
Date: Sat, 05 Oct 2002 06:33:48 GMT
From: Fred <No_Mail_Address@cox.net>
Subject: What does [^\n] mean
Message-Id: <3D9E87E9.E3068570@cox.net>
The following code is from a script for cleaning mailfiles, it removes
the (multiline) "Received:" sections:
....
$/ = undef;
while (<INPUT>) {
s/^(Received:\s[^\n]+\n)(\s+[^\n]+\n)*//mg;
print OUTPUT $_,"\n";
}
.....
My problem is that I cannot understand how it works. I made searches
for [^\n] and ^\n in the CSPAN-manual and in Perl book CD-ROM's but
could not find anything useful. Also I thought that with $/ = undef;
all \n are removed.
--
Fred
------------------------------
Date: Sat, 5 Oct 2002 00:19:28 -0700
From: "Tan Nguyen" <nospam@nospam.com>
Subject: Re: What does [^\n] mean
Message-Id: <3d9e91f8$1_3@nopics.sjc>
"Fred" <No_Mail_Address@cox.net> wrote in message
news:3D9E87E9.E3068570@cox.net...
> The following code is from a script for cleaning mailfiles, it removes
> the (multiline) "Received:" sections:
>
> ....
> $/ = undef;
> while (<INPUT>) {
> s/^(Received:\s[^\n]+\n)(\s+[^\n]+\n)*//mg;
> print OUTPUT $_,"\n";
> }
> .....
>
> My problem is that I cannot understand how it works. I made searches
> for [^\n] and ^\n in the CSPAN-manual and in Perl book CD-ROM's but
> could not find anything useful. Also I thought that with $/ = undef;
> all \n are removed.
>
Quick explanation....It means "not newline". The substitution says -
substitute a string starting(^) with Received: followed by a space (\s) and
anything that isn't a newline ([^\n])+ and then a newline ....and so on with
an empty string.
Setting $/ to undef (local $/ = undef) will only change the _input_ record
separator (for example, when you read in a file).
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 3918
***************************************