[30909] in Perl-Users-Digest
Perl-Users Digest, Issue: 2154 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jan 24 18:09:41 2009
Date: Sat, 24 Jan 2009 15:09:06 -0800 (PST)
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, 24 Jan 2009 Volume: 11 Number: 2154
Today's topics:
Re: [DGBI] Congrats, our prize goes to Sinan <whynot@pozharski.name>
Re: how to use "while" with <<EOF? sln@netherlands.com
Re: inputting the ephemerides <whynot@pozharski.name>
Re: inputting the ephemerides sln@netherlands.com
Re: Mathematica 7 compares to other languages <scuzwalla@gmail.com>
Re: OCaml, Language syntax, and Proof Systems <xahlee@gmail.com>
Peer Review for Folder Delete Script <XXjbhuntxx@white-star.com>
Re: Peer Review for Folder Delete Script <uri@stemsystems.com>
Re: Peer Review for Folder Delete Script <XXjbhuntxx@white-star.com>
Re: Peer Review for Folder Delete Script <uri@stemsystems.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 24 Jan 2009 15:28:34 +0200
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: [DGBI] Congrats, our prize goes to Sinan
Message-Id: <slrngnm5s2.ptc.whynot@orphan.zombinet>
On 2009-01-23, A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
> Uri Guttman <uri@stemsystems.com> wrote in
> news:x7d4ee37r0.fsf@mail.sysarch.com:
>
>>>>>>> "EP" == Eric Pozharski <whynot@pozharski.name> writes:
>>
>> >> but i suspect there isn't a need for undefing like that. whenever
>> >> i see a bunch of that sort of code i smell a poor design of the
>> >> loop or whatever. designing things so you don't need that is not
>> >> difficult and you can use scope, sub return, etc. to handle things
>> >> without explicit calls to undef.
>>
>> EP> Though I should admit, that I would stay with Uri's idea
>> (whatever name EP> of that idea is).
>>
>> call it the "don't use undef unless you must" rule. :)
My point was aimed to OP (though useles, I think).
> Would you believe that post where my main purpose was to come up with a
> quick example of using the range operator to solve the OP's problem was
> the first time I undef'ed a bunch of variables like that?
>
>> similar to the string eval and symrefs rules i have posted many times
>> - don't use them unless you know when not to use them!
>
> Good, now my name is mentioned along with string eval and symrefs. I'll
> never recover from that ;-)
Sorry, it was by accident :(
--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
------------------------------
Date: Sat, 24 Jan 2009 22:54:27 GMT
From: sln@netherlands.com
Subject: Re: how to use "while" with <<EOF?
Message-Id: <8v6nn45gldsm7sij3ef8o8sjm9h3brfd3o@4ax.com>
On Sat, 24 Jan 2009 16:59:51 +0100, "Dr.Ruud" <rvtol+usenet@xs4all.nl> wrote:
>jidanni@jidanni.org wrote:
>
>> Gentlemen, the perl docs don't anywhere reveal how to use "while" with
>> <<EOF.
>>
>> How do you say this right?:
>> while(<<EOF;){my @F=split; if((pop @F) =~ $ARGV[0]){print "@F\n"; last}
>> nurds bla.html
>> ...
>> turds surds mo.html
>> EOF
>>
>> Nope, there's just no guessing what's right. Who knows. Mystery.
>> {my @F=split; if((pop @F) =~ $ARGV[0]){print "@F\n"; last}}while <<'EOF';
>>
>> while(<<EOF){
>> a
>> b
>> c
>> EOF
>> print;
>> }
>
>perl -we'
> while(<<EOF =~ /^(.*)/mg) {
>a
>b
>c
>EOF
> print $1, $/;
> }
>'
>a
>b
>c
sln
-----------------------
use strict;
use warnings;
while("a
b
c
" =~ /^(.*)/mg) {
print $1, $/;
}
__END__
a
b
c
------------------------------
Date: Sat, 24 Jan 2009 15:51:07 +0200
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: inputting the ephemerides
Message-Id: <slrngnm76b.ptc.whynot@orphan.zombinet>
On 2009-01-23, Peter J. Holzer <hjp-usenet2@hjp.at> wrote:
> On 2009-01-22 23:19, Eric Pozharski <whynot@pozharski.name> wrote:
*SKIP*
>> I assume that LG didn't copy-paste. What a surprise...
>
> He did copy-paste (as you would have seen yourself if you had read the rest
> of my (or his) posting). He just just copy-pasted one character too
> little (or too much), splitting a sub-pattern in half. Which could
> either be just a copy-paste error (indicating sloppy editing) or a
> failure to understand how regular expressions are constructed.
My fault.
--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
------------------------------
Date: Sat, 24 Jan 2009 20:31:00 GMT
From: sln@netherlands.com
Subject: Re: inputting the ephemerides
Message-Id: <cktmn4h716fhf53ev5j7tqosd4ej3glqv4@4ax.com>
On Mon, 19 Jan 2009 22:04:32 -0700, Larry Gates <larry@example.invalid> wrote:
>
>Happy Bye George Day!
>
>I've been chipping away at a long-term project: investigating the
>ephemeris. I think it would make a great way to continue exploring perl's
>pattern-matching capabilities.
>
>So I'll have a program that looks like this:
>
> my $filename = 'eph3.txt';
> open(my $fh, '<', $filename) or die "cannot open $filename: $!";
> while (<$fh>) {
> print $_;
> }
> close($fh)
>
># perl faulk10.pl
>
>or
>
> open(my $fh, '<', 'eph3.txt');
> while (my $line = <$fh>) {
> print $line;
> }
> close($fh)
>
># perl faulk7.pl
>
>I'll want to have an explicit variable for the line, so I'll use the better
>parts of the above.
>
>The first thing I'll want to do is capture the first seven characters in a
>line. We can assume that these will always be letters or spaces padded out
>to the right.
>
>After that, I want to strip away all the characters, as does the following
>fortran routine. In this treatment $line would be inrec .
>
> subroutine WasteNonDigits(inrec)
> character*80 inrec
> character*1 c1,c2
> character*13 ValidDigits
> data ValidDigits/'0123456789.-+'/
> n=13
> do i=1,80
> c1=' '
> c2=inrec(i:i)
> do j=1,n
> if(c2.eq.ValidDigits(j:j)) c1=c2
> end do
> inrec(i:i)=c1
> end do
> return
> end subroutine
>
>Ultimately, I want to populate an object that I think would be pretty tame
>by perl standards.
>
>This is the data set:
>
>C:\MinGW\source>type eph3.txt
>! yesterday
># another comment
>
>Sun 18h 41m 55s -23? 5.4' 0.983 10.215 52.155 Up
>Mercury 20h 2m 16s -22? 12.5' 1.102 22.537 37.668 Up
>Venus 21h 55m 33s -14? 16.3' 0.795 39.872 11.703 Up
>Moon 21h 17m 19s -15? 2.4' 62.4 ER 36.796 22.871 Up
>Mars 18h 11m 59s -24? 6.1' 2.431 4.552 56.184 Up
>Jupiter 20h 3m 35s -20? 49.4' 6.034 23.867 38.203 Up
>Saturn 11h 32m 59s +5? 8.6' 9.018 -47.333 157.471 Set
>Uranus 23h 21m 30s -4? 57.9' 20.421 48.328 -18.527 Up
>Neptune 21h 39m 30s -14? 22.8' 30.748 38.963 16.599 Up
>Pluto 18h 4m 34s -17? 44.5' 32.543 7.443 62.142 Up
>
>C:\MinGW\source>
>
>Thanks for your comment.
Dropping this reply in the outter reply structure. From the inner posts
it looks like you just are trying to learn regular expressions.
Therefore, the set of data would present a significant formatting project
to cut your teeth on. This is probably a good thing.
After a point you would look at this data and try to factor out the real
things you want here, instead of redundant groupings. The fact is that
you don't need to validate numbers, letters, locations in the parsing regular
expression as this can be done later.
What each data line is in reality is either a group of numbers or a group
of letters. The letters can only be at the end or beginning.
So beginning or end its either ^[a-z] or [a-z]$
In the middle its [\d.+-]
Put them together with an or '|' and you have the parsing regex. Its that simple.
Validate later if you really need to.
There is no need to make Mount Everest out of a mole hill.
This one's for free George.
sln
-----------------------------------------------------
use strict;
use warnings;
my @fldnames = ('fld01','fld02','fld03','fld04','fld05','fld06','fld07','fld08','fld09','fld10');
my @Records = ();
while (<DATA>)
{
# Strip leading/trailing white space's
s/^\s*//; s/\s*$//;
next if (!length());
# Parse regex, only get whats needed
my @rec = /(^\s*[a-z]+|[\d.+-]+|[a-z]+\s*$)/ig;
# Parse regex alternate, with look ahead
# ---------------------
# or
# my @rec = /(^\s*[a-z]+|[\d.+-]+|[a-z]+?(?=\s*$))/ig;
# ---------------------
# Validate record size
next if (@rec != 10);
# Store record into a hash struct, put ref into Records array
my %struct;
@struct{ @fldnames } = @rec;
push (@Records,\%struct);
# Some debug printing
print "\n$_\n";
print "@rec\n";
print "$_ = $struct{$_}\n" for (sort keys %struct);
}
# Do something with 'Records' structures
# ...
__DATA__
Sun 18h 41m 55s -23? 5.4' 0.983 10.215 52.155 Up
Mercury 20h 2m 16s -22? 12.5' 1.102 22.537 37.668 Up
Venus 21h 55m 33s -14? 16.3' 0.795 39.872 11.703 Up
Moon 21h 17m 19s -15? 2.4' 62.4 ER 36.796 22.871 Up
Mars 18h 11m 59s -24? 6.1' 2.431 4.552 56.184 Up
Jupiter 20h 3m 35s -20? 49.4' 6.034 23.867 38.203 Up
Saturn 11h 32m 59s +5? 8.6' 9.018 -47.333 157.471 Set
Uranus 23h 21m 30s -4? 57.9' 20.421 48.328 -18.527 Up
Neptune 21h 39m 30s -14? 22.8' 30.748 38.963 16.599 Up
Pluto 18h 4m 34s -17? 44.5' 32.543 7.443 62.142 Up
__END__
Output:
Sun 18h 41m 55s -23? 5.4' 0.983 10.215 52.155 Up
Sun 18 41 55 -23 5.4 0.983 10.215 52.155 Up
fld01 = Sun
fld02 = 18
fld03 = 41
fld04 = 55
fld05 = -23
fld06 = 5.4
fld07 = 0.983
fld08 = 10.215
fld09 = 52.155
fld10 = Up
Mercury 20h 2m 16s -22? 12.5' 1.102 22.537 37.668 Up
Mercury 20 2 16 -22 12.5 1.102 22.537 37.668 Up
fld01 = Mercury
fld02 = 20
fld03 = 2
fld04 = 16
fld05 = -22
fld06 = 12.5
fld07 = 1.102
fld08 = 22.537
fld09 = 37.668
fld10 = Up
Venus 21h 55m 33s -14? 16.3' 0.795 39.872 11.703 Up
Venus 21 55 33 -14 16.3 0.795 39.872 11.703 Up
fld01 = Venus
fld02 = 21
fld03 = 55
fld04 = 33
fld05 = -14
fld06 = 16.3
fld07 = 0.795
fld08 = 39.872
fld09 = 11.703
fld10 = Up
Moon 21h 17m 19s -15? 2.4' 62.4 ER 36.796 22.871 Up
Moon 21 17 19 -15 2.4 62.4 36.796 22.871 Up
fld01 = Moon
fld02 = 21
fld03 = 17
fld04 = 19
fld05 = -15
fld06 = 2.4
fld07 = 62.4
fld08 = 36.796
fld09 = 22.871
fld10 = Up
Mars 18h 11m 59s -24? 6.1' 2.431 4.552 56.184 Up
Mars 18 11 59 -24 6.1 2.431 4.552 56.184 Up
fld01 = Mars
fld02 = 18
fld03 = 11
fld04 = 59
fld05 = -24
fld06 = 6.1
fld07 = 2.431
fld08 = 4.552
fld09 = 56.184
fld10 = Up
Jupiter 20h 3m 35s -20? 49.4' 6.034 23.867 38.203 Up
Jupiter 20 3 35 -20 49.4 6.034 23.867 38.203 Up
fld01 = Jupiter
fld02 = 20
fld03 = 3
fld04 = 35
fld05 = -20
fld06 = 49.4
fld07 = 6.034
fld08 = 23.867
fld09 = 38.203
fld10 = Up
Saturn 11h 32m 59s +5? 8.6' 9.018 -47.333 157.471 Set
Saturn 11 32 59 +5 8.6 9.018 -47.333 157.471 Set
fld01 = Saturn
fld02 = 11
fld03 = 32
fld04 = 59
fld05 = +5
fld06 = 8.6
fld07 = 9.018
fld08 = -47.333
fld09 = 157.471
fld10 = Set
Uranus 23h 21m 30s -4? 57.9' 20.421 48.328 -18.527 Up
Uranus 23 21 30 -4 57.9 20.421 48.328 -18.527 Up
fld01 = Uranus
fld02 = 23
fld03 = 21
fld04 = 30
fld05 = -4
fld06 = 57.9
fld07 = 20.421
fld08 = 48.328
fld09 = -18.527
fld10 = Up
Neptune 21h 39m 30s -14? 22.8' 30.748 38.963 16.599 Up
Neptune 21 39 30 -14 22.8 30.748 38.963 16.599 Up
fld01 = Neptune
fld02 = 21
fld03 = 39
fld04 = 30
fld05 = -14
fld06 = 22.8
fld07 = 30.748
fld08 = 38.963
fld09 = 16.599
fld10 = Up
Pluto 18h 4m 34s -17? 44.5' 32.543 7.443 62.142 Up
Pluto 18 4 34 -17 44.5 32.543 7.443 62.142 Up
fld01 = Pluto
fld02 = 18
fld03 = 4
fld04 = 34
fld05 = -17
fld06 = 44.5
fld07 = 32.543
fld08 = 7.443
fld09 = 62.142
fld10 = Up
------------------------------
Date: Sat, 24 Jan 2009 08:14:31 -0800 (PST)
From: Jerry Gerrone <scuzwalla@gmail.com>
Subject: Re: Mathematica 7 compares to other languages
Message-Id: <bb2c74a8-ac4e-49b4-bbb7-ced3091cf253@f11g2000vbf.googlegroups.com>
On Jan 21, 1:06=A0pm, "soul.mirr...@gmail.com" <soul.mirr...@gmail.com>
wrote:
> On Dec 4 2008, 5:11=A0am, Andreas Waldenburger <geekm...@usenot.de>
> wrote:
> > I vaguely remember you plonking [Xah Lee] before. Did you unplonk him i=
n
> > the meantime? Or was that just a figure of speech?
>
> > teasingly yours,
> > /W
>
> Andreas Waldenburger, I hold up a mirror to your soul!
>
> A couple of years ago you started posting to the newsgroup
> comp.lang.java.programmer. Unlike most newbies, instead of lurking for
> a while and then contributing on-topic posts about Java, you jumped
> into the nearest available flamewar and immediately got in up to your
> neck. Then, on November 13, 2007, you stooped to intentionally
> misquoting one of your opponents. When he wrote
>
> http://groups.google.com/group/comp.lang.java.programmer/msg/7797d4e9...
>
> A few days later, you did it again, misquoting this post
>
> http://groups.google.com/group/comp.lang.java.programmer/msg/fca19d41...
>
> in this one:
>
> http://groups.google.com/group/comp.lang.java.programmer/msg/397e1d4b...
>
> In both cases, you publicly portrayed this poor man as a pervert, even
> though, whatever his other [insult deleted], that is clearly not one of
> them.
None of the nasty things that you have said or implied about me are at
all true.
> Repeatedly you have claimed to be primarily motivated by finding the
> disrupting of newsgroups to be entertaining. This is tantamount to
> admitting to being a troll.
Yes, and here you are, feeding him. Way to go, genius.
(And cljp had just gotten peaceful again, too!)
------------------------------
Date: Sat, 24 Jan 2009 14:48:53 -0800 (PST)
From: Xah Lee <xahlee@gmail.com>
Subject: Re: OCaml, Language syntax, and Proof Systems
Message-Id: <de409a37-dfa4-430e-a1a5-c9625779fb35@w1g2000prm.googlegroups.com>
Just a quick relpy.
Jon's tutorial:
http://www.ffconsultancy.com/products/ocaml_for_scientists/chapter1.html
is by far the best tutorial of Ocaml.
It is far better than the official intro to ocaml at
=E2=80=9Ccaml.inria.fr=E2=80=9D or the popularly cited tutorial at
=E2=80=9Cocaml-tutorial.org=E2=80=9D .
Jon's tutorial, namely the free chapter 1 of his book, is concise, to
the point, well written, well organized, does not unnecessarily use
abstruse jargons or concepts, does not pitch or preach engineering
practices or paradigms.
Jon's book title says it all: Ocaml for Scientist.
Scientists are intelligent. All programing language tutorials should
be modeled like this. For some detail, see:
=E2=80=A2 Examples Of Quality Documentation In The Computing Industry
http://xahlee.org/perl-python/quality_docs.html
Btw, i've learned far more Ocaml in the past 3 days than the about 1
month of full time trying to learn Haskell. Mostly in 2006 or 2007. I
did not even obtain a basic understanding of the syntax. I do not have
a basic understanding of its types or how to define a type (was quite
confused in this). I don't even have a good idea what the lang's
syntactical elements or structural elements or semantic elements. In
fact, i have no basic understanding of the language. I tried. I tried
about 4 online tutorials or downloadable paper-published books. They
are extremely low quality and or idiotic. Half of the time is wasted
on finding a good tutorial or reading unreadable ones, and time is
spent on huge garbage texts about reading why haskell is better or
currying this or monads that (they idiots lacking mathematician's
perspicacity don't really understand the subject). Motherfucking
idiots. (i even tried to start a mailing list and drew a web-badge for
haskell by my enthusiasm. See: A Haskell A Day http://xahlee.org/haskell/ha=
skell.html
(it went no where and is now on hold indefinitely))
I really believed in Haskell, almost just by its =E2=80=9Cwe don't allow
assignments and we have =E2=80=98lazy evaluation=E2=80=99=E2=80=9D. I belie=
ved it for 10
years. No more.
Jon wrote:
> And the freely-available first chapter of The OCaml Journal:
>
> http://www.ffconsultancy.com/products/ocaml_journal/free/introduction...
>
> I also recommend Jason Hickey's book which, I believe, is due to be
> published by Cambridge University Press soon:
>
> http://www.cs.caltech.edu/courses/cs134/cs134b/book.pdf
Thanks. Am still reading your chapter 1 yet. Will check those out
later.
Xah
=E2=88=91 http://xahlee.org/
=E2=98=84
On Jan 23, 4:36 pm, Jon Harrop <j...@ffconsultancy.com> wrote:
> Xah Lee wrote:
> > ok, i've been reading these Ocaml tutorials in the past few days:
>
> > intro to ocaml, from official site
> > http://caml.inria.fr/pub/docs/manual-ocaml/manual003.html
>
> > =E2=80=9CObjective CAML Tutorial=E2=80=9D, most cited tutorial on the w=
eb
> > http://www.ocaml-tutorial.org/
>
> > The best one, is the one is
> > =E2=80=9CIntroduction to Caml=E2=80=9D
> > http://www.cs.jhu.edu/~scott/pl/lectures/caml-intro.html
> > by Dr Scott Smith of Johns Hopkins U, apparently a lecture note.
> > I found it by as one of the top result from google search.
>
> You may also appreciate the freely-available first chapter of my book OCa=
ml
> for Scientists:
>
> http://www.ffconsultancy.com/products/ocaml_for_scientists/chapter1.html
>
> And the freely-available first chapter of The OCaml Journal:
>
> http://www.ffconsultancy.com/products/ocaml_journal/free/introduction...
>
> I also recommend Jason Hickey's book which, I believe, is due to be
> published by Cambridge University Press soon:
>
> http://www.cs.caltech.edu/courses/cs134/cs134b/book.pdf
>
> --
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.http://www.ffconsultancy.com=
/?u
------------------------------
Date: Sat, 24 Jan 2009 18:55:12 GMT
From: Cosmic Cruizer <XXjbhuntxx@white-star.com>
Subject: Peer Review for Folder Delete Script
Message-Id: <Xns9B9D6ED82B3ADccruizermydejacom@207.115.33.102>
I'm looking for a peer review of the following code. I ran into quite a
few issues while developing it, so I think it best if I have additional
eyes looking at it.
There is a W2K3 drive that contains a folder that has multiple "job"
folders. Each job folder may contain additional folders, along with a
number of files. When the job folder's creation date exceeds a certain
number of days, the complete folder and all its contents need to be
deleted.
Here is my code. All feedback is welcomed.
#########################################################################
######
# File Name: directory_cleanup.pl
#
# WARNING: This script can be very unforgiving and damaging. Make sure
the
# the correct path is listed, otherwise extreme data loss my
occur!
#
# Assumes: Perl is installed on the Windows computer used to run this
script.
# (NOTE: This script was designed to run on a Windows
computer.) The
# user or account running this script has permissions to delete
all
# files and folders in the targeted path.
#
# This script removes folder structures that are past a listed number of
days.
# It will only analyze folders in the base directory and will ignore
files.
#
# $days is the number of days that are kept. All directories older than
$days
# will be deleted along with all of the contents of those directories.
#
# $base_dir is the base directory where the targeted directories are
checked
# for the date they were created and deleted if older than $days.
#
# A logfile is created on first use of this script and updated there
after
# each time this script runs. The current local time is added, then each
# directory that is deleted is recorded. If no directories are delete,
an
# entry noting "No directories deleted" is added.
#
# If a directory cannot be deleted, it will have two entries in the log:
# The first will say "Could not remove...", but then it will follow
that
# it was removed. It may be the case were most of the contents were
# removed, but not the final directory. In this case, remove the
directory
# manually. (You may need to unlock some items or they were in use.)
#
# NOTE: Once a directory and its files are deleted, they cannot be
undeleted!
#
# Created by: xxxxxxxxx
# Date: January 22, 2009
#
#########################################################################
######
use strict;
use File::Find;
my $base_dir='E:\Jobs'; # Add root name of folder to be
managed
my $days=31; # Change to number of days to keep
### Do not change anything below this line ###
my $output = 'delete_directories.txt';
my $deltime=time-$days*86400;
my $delete_flag = 0; # Flag to check for any deletions
my ($month, $day, $year) = (split / /, scalar localtime)[1,2,4];
open(OUT," >> $output") || die $!; # Open output file for logging
print OUT "$month $day, $year\n"; # Add current date to output file
opendir(DIR,$base_dir) || die $!; # Get list of directories
my @children = grep !/^\./, readdir (DIR);
closedir (DIR);
foreach my $child (@children) {
my $entry = $base_dir . '\\' . $child;
if (-d $entry){ # If the "entry" is a folder, process
it
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,
$ctime,$blksize,$blocks)=stat($entry);
my $ftime=$mtime;
if ($ftime<$deltime) { # If folder is older than $days,
remove it
# The next two lines finds the directory structure and does the
removal
finddepth (\&remove_dir, "$entry");
rmdir ( "$entry" ) or print OUT "\tCould not remove $entry\n";
$delete_flag = 1; # Set flag to record a directory was
deleted
print OUT "\t$entry\n"; # Write delete directory to log
file
}
}
}
print OUT "\tNo directories deleted\n", if($delete_flag eq 0);
print OUT "\n"; # Add a final line feed for readability
close(OUT);
exit;
### Subroutines ###
sub remove_dir { # Remove files and directories
# for a directory, this will be 0
if ( ! (stat("$File::Find::name"))[7] ) {
rmdir("$File::Find::name");
}
else {
unlink("$File::Find::name");
}
}
------------------------------
Date: Sat, 24 Jan 2009 14:41:01 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Peer Review for Folder Delete Script
Message-Id: <x763k4wkz6.fsf@mail.sysarch.com>
>>>>> "CC" == Cosmic Cruizer <XXjbhuntxx@white-star.com> writes:
CC> #
CC> # A logfile is created on first use of this script and updated there
CC> after
first off, paste it cleanly so you don't get line wrapping. this will
not compile as is since there are lines with comment text without
leading # chars. this is all over the script.
CC> use strict;
use warnings too.
CC> use File::Find;
CC> my $base_dir='E:\Jobs'; # Add root name of folder to be
CC> managed
another wrapped line.
CC> my $days=31; # Change to number of days to keep
use horizontal whitespace. it is free.
my $days = 31;
don't put comments on a line with the code. better to put them on the
line before the code. you have more room to work with and it reads much better.
CC> ### Do not change anything below this line ###
CC> my ($month, $day, $year) = (split / /, scalar localtime)[1,2,4];
if you called localtime in a list context, you could slice out the parts
you want without split. or use strftime.
CC> opendir(DIR,$base_dir) || die $!; # Get list of directories
CC> my @children = grep !/^\./, readdir (DIR);
that removes ALL files starting with ., not just . and .. which is what
i think you are trying to remove.
CC> closedir (DIR);
just a little tweak here. File::Slurp has a read_dir func which does
that and it removes . and .. for you.
use File::Slurp ;
my @children = read_dir($base_dir) ;
CC> foreach my $child (@children) {
CC> my $entry = $base_dir . '\\' . $child;
interpolation is easier to read:
my $entry = "$base_dir\\$child" ;
also iirc you can use / for dir separators on windows for all file
operations. only the cmd shell requires \.
CC> if (-d $entry){ # If the "entry" is a folder, process
CC> it
CC> my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,
CC> $ctime,$blksize,$blocks)=stat($entry);
CC> my $ftime=$mtime;
why declare and not use all those stat values? you know how to do a
slice (you did one with localtime) so do that here.
CC> if ($ftime<$deltime) { # If folder is older than $days,
CC> remove it
CC> # The next two lines finds the directory structure and does the
CC> removal
CC> finddepth (\&remove_dir, "$entry");
useless quoting of a scalar. this could be a bug in some cases.
CC> rmdir ( "$entry" ) or print OUT "\tCould not remove $entry\n";
same thing.
CC> ### Subroutines ###
CC> sub remove_dir { # Remove files and directories
CC> # for a directory, this will be 0
CC> if ( ! (stat("$File::Find::name"))[7] ) {
CC> rmdir("$File::Find::name");
CC> }
CC> else {
CC> unlink("$File::Find::name");
all those are just scalars with a long name. no need for the quotes
and -d tests for a directory so why the complex code? you use -d
earlier so you know it.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Sat, 24 Jan 2009 21:19:28 GMT
From: Cosmic Cruizer <XXjbhuntxx@white-star.com>
Subject: Re: Peer Review for Folder Delete Script
Message-Id: <Xns9B9D874D5731Accruizermydejacom@207.115.33.102>
Thanks for your input uri.
Some of my code probably ended up pretty sloppy since my 60 minute project
turned into about 3 hours worth of frustrations. (Fortunately, I was
running it on a test system, so no real data loss occurred.)
For example, I should not have needed someone to point out I should have
used: my $mtime = (stat($entry))[9] instead of the long drawn out method I
originally used. But then again... that's why we have code reviews. ;)
I also simplified a few other items and cleaned up the superfluous syntax.
(But I kept the end of line comments because that's how I like to do it...
and at least I add comments.)
Thank you very much!
...Cos
------------------------------
Date: Sat, 24 Jan 2009 17:10:34 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Peer Review for Folder Delete Script
Message-Id: <x7vds4uzhh.fsf@mail.sysarch.com>
>>>>> "CC" == Cosmic Cruizer <XXjbhuntxx@white-star.com> writes:
CC> Thanks for your input uri.
CC> Some of my code probably ended up pretty sloppy since my 60 minute project
CC> turned into about 3 hours worth of frustrations. (Fortunately, I was
CC> running it on a test system, so no real data loss occurred.)
CC> For example, I should not have needed someone to point out I
CC> should have used: my $mtime = (stat($entry))[9] instead of the
CC> long drawn out method I
and there are -X entries that will get you info like that so you can
avoid the stat slice too.
CC> I also simplified a few other items and cleaned up the superfluous
CC> syntax. (But I kept the end of line comments because that's how I
CC> like to do it... and at least I add comments.)
good for you that you add comments. how you like to do it is not as
important as how people like to read. perl style is generally not to
comment on the same line and you don't see it much in perl code. so it
is better for you to adhere to the more common style of comments on
their own lines. and as i said you get more room and multiline comments
work. also you can line wrap those comments (most decent editors can do
this) with a command to keep them readable.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 V11 Issue 2154
***************************************