[21719] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3923 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Oct 6 00:11:18 2002

Date: Sat, 5 Oct 2002 21:10:12 -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: 3923

Today's topics:
    Re: Script to Change Filename (Tad McClellan)
    Re: Script to Change Filename <NO_SPAM_pangjoe@rogers.com>
    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>
        Sharing a program? <joec@annuna.com>
    Re: Truncated file write <bongie@gmx.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 5 Oct 2002 18:24:30 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Script to Change Filename
Message-Id: <slrnaput5e.2mf.tadmc@magna.augustmail.com>

William Alexander Segraves <wsegrave@mindspring.com> wrote:
> "Tad McClellan" <tadmc@augustmail.com> wrote in message
> news:slrnapu6qs.20d.tadmc@magna.augustmail.com...

>> (?:  )  gives you grouping but does not give you memory, leaving
>> the counts unsullied.


> BTW, I'm persuaded by your able commentary that the use of (?: is not needed
> in the context of the solutions to the OP's original question, as modified
> later by the OP.


Right. Neither () nor (?:) is needed for the OP's problem.


> Please bear with me. I'm trying to learn why (?: would have been used at all
> by David Britten.


I dunno either.


> So, please consider:
> 
> A:
> s/((?:\d)+)([a-zA-Z]\.dat)$/$1-$2/;
> #regex from David Britten,
> # with Tad's suggested $ anchor for .dat end of the filename,
> # and Tad's suggested deletion of curly brackets
> 
> Q:
> What is a regex that will convert a filename composed of any number of
> characters and numbers 


Natural language is horribly imprecise for describing patterns
to match, but let's do the best we can.

"numbers" is ambiguous. I would count all of these as numbers:

   -1.5
   12e5
   XXIII
   2 + 2

none of them are what we mean to speak of here.

"digit characters" would be better. But they are just a subset
of "characters", that we have already stated, so we can take
the "and numbers" right out of there altogether.


> ending in at least one letter, 


It (the part before the matched characters) is not required to 
end with a letter character.

   "123a.dat", "-123a.dat" and "\n123a.dat" would all match.


> followed by any number
> of numbers 
     ^^^^^^^

digit characters   :-)

And talking about the characters that come before the digits
is not something that we can do for the pattern above. It does
not say anything about what comes before it. We would need
a start of string anchor to enforce that.


> and any single letter, followed by the filename extension .dat,
> inserting a dash between all of the the rightmost numbers and the single
> letter?

[snip]


> It appears to me that (?: is exhibiting the behaviour suggested (to me) by
> the Associativity-Operators table shown in "Operators", p. 77, of the
> previously cited Camel Book, 2nd. ed, i.e., the use of (?: appears to force
> the match to the right.


That is not (?: 

That is your friendly neighborhood "ugly 3-part C-like thingie" again  :-)

That table is for _Perl_ operators, not regex operators.

(and only people who have bought that edition of the book can
 see what you're talking about. Better to cite the std docs
 when possible so everyone can play. perlop.pod has Perl's
 precedence and associativety table in it too.
)

regexes are a "little language" inside of a bigger language (Perl).
What the funny characters mean depends on which language they
appear in. There is even a "littler language" inside of the
regex language, character classes.

For example, caret (^) in the Perl language means "bitwise or",
in the regex language it means "start of string", in the char
class language in means "negate the class".



(?: )  has no effect (in this particular pattern). 

Take it out, same matched chars. Use () instead, same matched chars
(but saved in different places).


> Is this a correct interpretation? 


No. The lack of start anchor and presense of end anchor is what
"forces it to the right".


> Is there a better way to match as many
> numbers as possible to the left of any_single_letter.dat, stopping the match
> at the first encountered letter, but retaining all of the characters to the
> left of the last matched consecutive number?


I think natural language is failing us here. I can't grok that. 


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Sat, 05 Oct 2002 23:22:15 GMT
From: "JP" <NO_SPAM_pangjoe@rogers.com>
Subject: Re: Script to Change Filename
Message-Id: <HuKn9.25911$Aiq1.14645@news04.bloor.is.net.cable.rogers.com>

Folks,

I have to thank everyone of you for being so helpful in perfecting the
script.  I ended up using the following code by combining your efforts:

==========================================================
use strict;

my $dir = '/usr/data';
my $logfile = '/tmp/rename.log';
my $counter = 0;

opendir (DIR, $dir) || die "Cannot open directory $dir:$!\n";
open (LOG, ">$logfile") || die "Cannot open log!\n";

foreach (readdir(DIR)) {

  my $old_filename = $_;

  if (/^((?:\d){3,8})([a-zA-Z]\.dat)$/)  {
    my $new_filename = "${1}-${2}";
    print "$new_filename\n";
    unless($old_filename eq $new_filename) {
      print "Renaming $old_filename to $new_filename\n";
      rename ($old_filename, $new_filename) || warn "Cannot rename
$old_filename:$!\n";
      print LOG "$old_filename\t-->\t$new_filename\n";
      $counter += 1
    }
   }
}

closedir DIR;
print "$counter files are renamed.\n";

==========================================================

I did learnt a lot from all of you.  Thank you, Bill, for your quick
response and enthusiasm.

Cheers,

Joe








"Tina Mueller" <usenet@tinita.de> wrote in message
news:anjlqn$eqiqg$1@fu-berlin.de...
> JP <NO_SPAM_pangjoe@rogers.com> wrote:
>
> > 1.  Select only those filenames with .dat extension
> > 2.  The sournce filename must be all numeric but ends with a character
> > digital, e.g. 12345a.dat, 234567b.dat
>
> opendir DIR, "path";
> my @files = grep {m/^\d+[a-z]\.dat$/i} readdir DIR;
>
> > 3.  If it meets the above two criteria, rename the file by inserting a
dash
> > in between the numerals and character, e.g. 12345a.dat --> 12345-a.dat,
> > 234567b.dat -->234567-b.dat
>
> for (@files) {
>  m/^(\d+)([a-z])\.dat$/i and rename $_, "$1-$2.dat";
> }
> hth, tina
> --
> http://www.tinita.de/        \  enter__| |__the___ _ _ ___
> http://Movies.tinita.de/      \     / _` / _ \/ _ \ '_(_-< of
> http://PerlQuotes.tinita.de/   \    \ _,_\ __/\ __/_| /__/ perception




------------------------------

Date: Sat, 5 Oct 2002 22:31:01 -0500
From: "William Alexander Segraves" <wsegrave@mindspring.com>
Subject: Re: Script to Change Filename
Message-Id: <anob64$4pi$1@nntp9.atl.mindspring.net>

"JP" <NO_SPAM_pangjoe@rogers.com> wrote in message
news:HuKn9.25911$Aiq1.14645@news04.bloor.is.net.cable.rogers.com...
> Folks,
>
> I have to thank everyone of you for being so helpful in perfecting the
> script.  I ended up using the following code by combining your efforts:
<snip>
>   if (/^((?:\d){3,8})([a-zA-Z]\.dat)$/)  {
>     my $new_filename = "${1}-${2}";

JP, I haven't time now to check the rest of your code; but you may wish to
follow Tad's most recent suggestions on the regex and the curly brackets.

<snip>
> I did learnt a lot from all of you.  Thank you, Bill, for your quick
> response and enthusiasm.

You're very welcome.

Cheers.

Bill Segraves




------------------------------

Date: Sat, 5 Oct 2002 22:35:41 -0500
From: "William Alexander Segraves" <wsegrave@mindspring.com>
Subject: Re: Script to Change Filename
Message-Id: <anob65$4pi$2@nntp9.atl.mindspring.net>

"Tad McClellan" <tadmc@augustmail.com> wrote in message
news:slrnaput5e.2mf.tadmc@magna.augustmail.com...
> William Alexander Segraves <wsegrave@mindspring.com> wrote:
> > "Tad McClellan" <tadmc@augustmail.com> wrote in message
> > news:slrnapu6qs.20d.tadmc@magna.augustmail.com...
>

Thanks for your comments, Tad. Saved for future study in detail.

<snip>

> I think natural language is failing us here. I can't grok that.

Indeed! It was difficult to write.

Cheers.

Bill Segraves




------------------------------

Date: Sat, 05 Oct 2002 22:16:59 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Security-Hole in module Safe.pm
Message-Id: <3D9F9D1B.B2B31415@earthlink.net>

Rafael Garcia-Suarez wrote:
> 
> 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.

Recompiling perl has a global and irreversible effect, too, doesn't it?

Which means, then, that the only way to "safely" run code with some
opcodes disabled, from a perl which doesn't have them disabled, is to
start a new process, and run your code in that other process...
something like:

   open( my $safe, "-|", "perl-with-ops-disabled", "-e", $untrusted );

Which is no less cumbersome than:

   open( my $safe, "-|", $^X, "-Mops=:default", "-e", $untrusted );

As for the truly paranoid...  they shouldn't be running untrusted code
in the first place, even with opcodes disabled one way or another.

-- 
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
 ."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]


------------------------------

Date: Sun, 06 Oct 2002 01:53:39 GMT
From: Joe Creaney <joec@annuna.com>
Subject: Sharing a program?
Message-Id: <3D9F97AD.30905@annuna.com>

This is a multi-part message in MIME format.
--------------020403040708050809030100
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

I am fairly new to perl and programming but I wrote a very simple 
adventure program is there any place I can share this program?

--------------020403040708050809030100
Content-Type: text/plain;
 name="SimpleRPG.pl"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="SimpleRPG.pl"

#! usr/bin/perl -w

use strict;
use integer;

my @maze=(
	[ qw( e  sew we ws )],
	[ qw(ne new sw ns ) ],
	[ qw( ns -  ns wn ) ],
	[ qw(ne w   ne w )],
);
my %direction =  (n=> [ -1, 0], s=> [1, 0], e => [0, 1], w => [0, -1]);

my %full=( e => 'East', n => 'North', w=>'West', s =>'South');
my($curr_x, $curr_y, $x, $y)=(0,0,3,3);
my $move;

my $num;
my $n;
my $c;
my @p=(" ",10,1,0,7,6); 
my @m;
my $mhp = $p[1];

sub disp_loc {
	my($cx, $cy)=@_;
	print "You may move ";
        while($maze [$cx] [$cy]=~/([nsew])/g) {
		print "$full{$1} ";
	}
        print "($maze[$cx][$cy])\n";
}
sub move_to {
	my($new, $xref, $yref)=@_;

	$new=substr(lc($new),0,1);
	if ($maze[$$xref] [$$yref]!~/$new/) {
		print "Can't go that way $new!\n";
		return;
	}
	$$xref += $direction{$new}[0];
	$$yref += $direction{$new}[1];
}

sub xps {
        my ($hp, $lv, $xp, $mxp) = @_;
	print "You got $mxp XP for the battle \n";
        $xp = $xp + $mxp;
	if ($xp > 100) {
		$lv++;
                $xp = $xp - 100;
                ($hp, $mhp) = nlv ($hp, $mxp);         
        	}
        return $hp, $lv, $xp;
        }

sub nlv {
        my ($hp, $mhp) = @_;
        my $num;

        $num = (int(rand(10))+1);
        $hp = $hp + $num;
        $mhp = $mhp + $num;
        return $hp, $mhp;
        }

sub atk {
        my ($hd, $ac) = @_;
        my $roll;
        my $hit;
        my $thaco;

        $roll = int(rand(20))+1;
        $thaco = 19 - $hd;
       if ($roll > ($thaco - $ac)) {
       		 $hit=1;
       		 print "Hit! \n";
         } else {
        	print "Missed... \n";
       		$hit=0;
        }
        return $hit;
        }
sub fight {

        my $ht;
        my ($md, $pd) = @_;
        print "$$pd[0] is Fighting a $$md[0] \n";
        print "You have $$pd[1] HP, it has $$md[2] HP\n";
        $ht = atk ($$pd[2], $$md[3]);
        if ( $ht == 1 ) {
                $$md[2] = $$md[2] - (int(rand($$pd[5]))+1);
        }
        print "Now the $$md[0] attacks \n";
        $ht = atk ($$md[1], $$pd[2]);
        if ( $ht == 1 ) {
                $$pd[1] = $$pd[1] - (int(rand($$md[3]))+1);
                }
        return $$md[2], $$pd[1];
        }
        
sub choosmon {

my $x;
my $v;
my $rm;
my $hp;
my @mm = ();
my @ml = (
        [ "Orc",1,0,6,15 ],
        [ "Gobblin",1,0,4,10 ],
        [ "Hobgobblin",2,0,6,25 ],
        [ "Skelliton",1,0,4,10 ],
        [ "Dragon!",4,0,7,100 ],
        [ "Wolf",2,0,3,20 ],
        );

$rm = int(rand(6));

for ($x=0; $x <= 5; $x++) {
        $mm[$x] = $ml[$rm][$x];
                 }

$v=$mm[1];

for ($x=0; $x <= $v; $x++ ) {
        $hp = $hp + int(rand(6))+1;
        }
       
print "You see a $mm[0] \n";

$mm[2] = $hp;
return @mm;
}


sub run {
        my ($hp)=@_;
        print "Running\n";
        $hp++;
        return $hp;

        }

print "What is your name?";
        $p[0]=<STDIN>; chomp $p[0];
        

until ( $curr_x == $x and $curr_y ==$y ) {
        disp_loc($curr_x, $curr_y);
	print "Which way? ";
	$move=<STDIN>; chomp $move;
	exit if ($move=~/^q/);
	move_to($move, \$curr_x, \$curr_y);

$num = int(rand(4))+1;
print " $num";
if ($num == 1) {
@m = choosmon (@m);
CBT: while ( $p[1] > 0 ) {

if ( $p[1] > $mhp ) {
        
        $p[1] = $mhp;

        }
      
                                                   
        print "You have $p[1]/$mhp HP it has $m[2] \n";
        print "Do you want to f ight, r un d ie: ";
	$c=<STDIN>; chomp $c;
        if ( $c eq "f" ) {
               ($m[2], $p[1]) = fight(\@m, \@p);       
	}

               
        if  ($c eq "r" ) {
                $p[1] = run($p[1]);
                }

        if ( $c eq "l" ) {
                ($p[2], $mhp) = nlv ($p[2], $mhp);
                }
                                 
        if ( $m[2] <= 0 ) {
                print "You killed the $m[0] \n";
		($p[1], $p[2], $p[3]) = xps ($p[1], $p[2], $p[3], $m[4]); 
		last CBT;
        	        }

        if ( $c eq "d" ) {$p[1] = 0;
        }



        }

       
if ( $p[1] <= 0 ) {        
print "You are dead";
die; }

}
}
print "You made it!\n";

--------------020403040708050809030100--



------------------------------

Date: Sun, 06 Oct 2002 01:06:16 +0200
From: "Harald H.-J. Bongartz" <bongie@gmx.net>
Subject: Re: Truncated file write
Message-Id: <2702724.v5ErqvCLOD@nyoga.dubu.de>

Marshall Dudley wrote:
> I have an application where a data file may get read quite often, but
> it only gets written by the manager, which has only one person ever in
> it at a time.

Sure?  No possibility that the action gets called twice within a short 
time, e.g. by some nervous clicking (or impatient user)?

> I am therefore not locking the file, since a lock does
> not lock reads or appends,

Says who?  If your application is the only one touching the file you can 
of course use locking, even for reads and appends.

> and there is no possibility of two writes
> at the same time.

I don't know your application, but something apparently *has* gone 
wrong.  Well, it could still be a transient error in the file system.

> There is a data file which is either appended to when an item is
> added, or the file is read in, one line changes and written back if an
> item is edited.

A classic race condition, if there would be any chance of two processes 
trying to change the file at the same time: Process A reads the file, 
modifies it and starts to write it; process B reads the file when it 
has just been reopened by A, so it is empty or only partly written; 
process A finishes writing the (correctly) modified data; now process B 
starts writing to the file, overwriting the correct data with corrupt 
data.  No good.

So, what was that about not locking a file access? ;-)

Ciao,
        Harald
-- 
Harald H.-J. Bongartz <bongie@gmx.net>
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Usenet is like Tetris for people who still remember how to read.
                -- Joshua Geller


------------------------------

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 3923
***************************************


home help back first fref pref prev next nref lref last post