[18885] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1053 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jun 4 14:06:02 2001

Date: Mon, 4 Jun 2001 11:05:19 -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: <991677919-v10-i1053@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Mon, 4 Jun 2001     Volume: 10 Number: 1053

Today's topics:
    Re: "The Camel Book", "The Ram Book" or "Perl : The Pro <reader@newsguy.com>
        2 questions about flock <m.grimshaw@salford.ac.uk>
    Re: 2 questions about flock <keesh@users.sf.net>
    Re: 2 questions about flock <m.grimshaw@salford.ac.uk>
    Re: Appending to  a Text file <mischief@velma.motion.net>
        Background start of program in CGI? <CAB@earthling.net>
        Count Occurences of a character in a string (Gary Baker)
    Re: Count Occurences of a character in a string (John Joseph Trammell)
    Re: Count Occurences of a character in a string (Tad McClellan)
        dos commands (AtnNn)
    Re: Frustrated people (not) answering questions <newspost@coppit.org>
    Re: Help a script to prompt to input and have those inp <lmoran@wtsg.com>
        Help with Range operators <jhill@technoslave.net>
    Re: Help with Range operators <jhill@technoslave.net>
    Re: Help with Range operators (Jay Tilton)
    Re: Help with Range operators <jhill@technoslave.net>
    Re: Help with the $0 equivalent, portable for use in DO <c_clarkson@hotmail.com>
    Re: HTTP::Request GET <m.astrakhanskaya@gte.net>
    Re: Komodo <gordon@med.unc.edu>
    Re: locking files before writing data (?) (Mark Jason Dominus)
    Re: locking files before writing data (?) (Mark Jason Dominus)
    Re: locking files before writing data (?) <muddycat@hotmail.com>
    Re: locking files before writing data (?) <iltzu@sci.invalid>
        Looking for critique - Please be kind ! (Matthew J. Salerno)
    Re: Looking for critique - Please be kind ! (John Joseph Trammell)
    Re: Looking for critique - Please be kind ! (Tad McClellan)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 04 Jun 2001 08:08:11 -0700
From: Harry Putnam <reader@newsguy.com>
Subject: Re: "The Camel Book", "The Ram Book" or "Perl : The Programmer's Companion - Nigel Chapman"
Message-Id: <m166ecjoue.fsf@totally-fudged-out-message-id>

merlyn@stonehenge.com (Randal L. Schwartz) writes:

> >>>>> "Markus" == Markus Gyger <mgyger@gmu.edu> writes:
> 
> Markus> Actually, now: "The Perl CD Bookshelf, Version 2.0", ISBN 0-596-00164-9,
> Markus> US$ 79.95, http://www.oreilly.com/catalog/perlcdbs2/ which includes:
> 
> Markus>   - Perl in a Nutshell
> Markus>   - Programming Perl, 3rd Edition
> Markus>   - Advanced Perl Programming
> Markus>   - Perl Cookbook
> Markus>   - Perl for System Administration
> 
> Yeah, sadly, Learning Perl 3rd ed was not quite ready for this CD,
> although Learning Perl 2ed was outdated.  Oh well, you can still get
> Learning Perl 3rd ed in a searchable online form through the Safari
> service the moment it comes out.

Are there plans for another release of `The Perl CD Bookshelf' that
will include `Learning Perl' (3rd ed).  So a prospective user could
just wait for the more complete set?

Alternatively, can you elaborate a little about the Safari service for
the uninitiated?  Will that release have some kind of thorough search
tool? 

For that matter, I wondered how good the search tools on the current
CD are?  For example, is it possible to do a full body search of the
whole thing by regexp?  How is the html stripping or rewriting
handled?
 
Someone mentioned a Java based search engine... Is it also regexp
based or some less accurate technique?  Is it WAIS like?


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

Date: Mon, 04 Jun 2001 17:55:50 +0100
From: Mark Grimshaw <m.grimshaw@salford.ac.uk>
Subject: 2 questions about flock
Message-Id: <3B1BBD96.3A2F8809@salford.ac.uk>

Hi all,

Looked through books, perldoc -f flock etc. but none the wiser.

1/
flock operates on a filehandle.  How/can I implement it on a db_file
that has been opened with tie?

2/
Can someone explain, in plain English please, the difference between
LOCK_EX and LOCK_SH?


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

Date: Mon, 04 Jun 2001 17:23:52 GMT
From: "Ciaran McCreesh" <keesh@users.sf.net>
Subject: Re: 2 questions about flock
Message-Id: <IuPS6.386$IY1.106734@news1.cableinet.net>

In article <3B1BBD96.3A2F8809@salford.ac.uk>, "Mark Grimshaw"
<m.grimshaw@salford.ac.uk> said:
> Can someone explain, in plain English please, the difference between
> LOCK_EX and LOCK_SH?

Sure. With LOCK_EX, the file handle which has the lock can do what it
wants with a file, but no-one else can use that file for any purpose
whatsoever whilst the lock is active. Obviously, there can only ever be
one LOCK_EX on a file at a time.

With LOCK_SH, however, anyone can come along and add another LOCK_SH to
the file. They cannot, however, add a LOCK_EX whilst a LOCK_SH is
present.

In practice, this means that LOCK_EX is used when you want to write to a
file, since you don't want anyone else trying to read or write when
you're writing, and LOCK_SH is used for reading, since any number of
people can read a file at once, but whilst you're reading you don't want
someone else to start writing.

There's a bit more too it than that, but that's the basic idea... Any
more detail would get into platform-specific stuff.

-- 
Ciaran McCreesh
mail:         keesh at users dot sf dot net
web:          http://www.opensourcepan.com


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

Date: Mon, 04 Jun 2001 18:47:07 +0100
From: Mark Grimshaw <m.grimshaw@salford.ac.uk>
Subject: Re: 2 questions about flock
Message-Id: <3B1BC99B.5076A474@salford.ac.uk>

Thanks.  So to be safe/efficient, use LOCK_EX for writing and LOCK_SH
for reading?  


When you say this:
"Obviously, there can only ever be
> one LOCK_EX on a file at a time."

do you actually mean file or filehandle?

Ciaran McCreesh wrote:
> 
> In article <3B1BBD96.3A2F8809@salford.ac.uk>, "Mark Grimshaw"
> <m.grimshaw@salford.ac.uk> said:
> > Can someone explain, in plain English please, the difference between
> > LOCK_EX and LOCK_SH?
> 
> Sure. With LOCK_EX, the file handle which has the lock can do what it
> wants with a file, but no-one else can use that file for any purpose
> whatsoever whilst the lock is active. Obviously, there can only ever be
> one LOCK_EX on a file at a time.
> 
> With LOCK_SH, however, anyone can come along and add another LOCK_SH to
> the file. They cannot, however, add a LOCK_EX whilst a LOCK_SH is
> present.
> 
> In practice, this means that LOCK_EX is used when you want to write to a
> file, since you don't want anyone else trying to read or write when
> you're writing, and LOCK_SH is used for reading, since any number of
> people can read a file at once, but whilst you're reading you don't want
> someone else to start writing.
> 
> There's a bit more too it than that, but that's the basic idea... Any
> more detail would get into platform-specific stuff.
> 
> --
> Ciaran McCreesh
> mail:         keesh at users dot sf dot net
> web:          http://www.opensourcepan.com


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

Date: Mon, 04 Jun 2001 15:37:19 -0000
From: Chris Stith <mischief@velma.motion.net>
Subject: Re: Appending to  a Text file
Message-Id: <thnapfhi8a4p0d@corp.supernews.com>

Henry Hartley <hartleh1@westat.com> wrote:
> "<< SilverSting >>" wrote:
>> 
>> Can anyone tell me how to append text to a file.
>> 
>> My open file code:
>> open(CLIENTINFO,">client.txt");

> first, you want to be sure the file was opened:

> open(CLIENTINFO,">client.txt") || die "Could not open client.txt:
> $!\n\n" ;
> print CLIENTINFO "A string you want printed to that file\n" ;

This doesn't show how to append to a file. It shows how to
overwrite your file with one line of text. Bad advice is worse
than none at all. It is good advice, however, to always check
the success of a call to open. This may be all Mr. Hartley was
trying to convey, but the post as presented was misleading.
Please read responses before posting to make sure they do not
read contrary to what you intend. 

Change the mode to '>>' instead of '>'. The docs for open
tell you this. You did read the docs for the function you
are using, right? Also, 'perldoc -q append' tells you
similar things. It is considered impolite to post to the
group without checking the Perl FAQ for your solution.

Chris

-- 
It's not the U in UBE that pisses people off. It's the B.
  -- Martien Verbruggen in clp.misc



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

Date: Mon, 04 Jun 2001 19:02:09 +0200
From: Steen Suder <CAB@earthling.net>
Subject: Background start of program in CGI?
Message-Id: <3B1BBF11.83E898C@earthling.net>

I have a script, A, that conditionally starts another script, B. B runs
some commands and executes a program, C, that will continue after B is
done, on the box (Linux).

Problem: when B, and in turn, C, is run, the webpage either hangs and
the program runs or the webpage returns (a redirect) and the program is
not run.

I've tried with system(), exec(), backticks and qx{}, neither does what
I what:
B runs C, that runs in the background after B is terminated.

It might be a problem with the program (pptpd), but is there a way that
I can use to force background run/detach of C?

-- 
Steen Suder

"We reject kings, presidents and voting. 
We believe in rough consensus and running code."


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

Date: 4 Jun 2001 06:53:07 -0700
From: bakerg@volpe.dot.gov (Gary Baker)
Subject: Count Occurences of a character in a string
Message-Id: <3580d191.0106040553.2e187ba5@posting.google.com>

How can you count the number of times a character occurs in
a string?  Seems like there has to be a one liner to do it
but I can't find a way, maybe it's just that it is monday 
morning.  Thanks, Gary


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

Date: Mon, 04 Jun 2001 13:59:19 GMT
From: trammell@bayazid.hypersloth.invalid (John Joseph Trammell)
Subject: Re: Count Occurences of a character in a string
Message-Id: <slrn9hn2h6.hhi.trammell@bayazid.hypersloth.net>

On 4 Jun 2001 06:53:07 -0700, Gary Baker <bakerg@volpe.dot.gov> wrote:
> How can you count the number of times a character occurs in
> a string?  Seems like there has to be a one liner to do it
> but I can't find a way, maybe it's just that it is monday 
> morning.  Thanks, Gary

y///

-- 
I don't know what the hell is going on dude, but this suspension gives
me more time for fraggin'.  Yee haw!


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

Date: Mon, 4 Jun 2001 09:24:20 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Count Occurences of a character in a string
Message-Id: <slrn9hn304.vs4.tadmc@tadmc26.august.net>

Gary Baker <bakerg@volpe.dot.gov> wrote:
>How can you count the number of times a character occurs in
>a string? 

   perldoc -f tr

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


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

Date: Mon, 04 Jun 2001 16:54:36 GMT
From: atnnn@hotmail.com (AtnNn)
Subject: dos commands
Message-Id: <3b1bbd17.9129518@news.aei.ca>

How can I use dos commands in perl, and control the input and output?

AtnNn


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

Date: Mon, 4 Jun 2001 09:58:50 -0400
From: David Coppit <newspost@coppit.org>
To: brian d foy <comdog@panix.com>
Subject: Re: Frustrated people (not) answering questions
Message-Id: <Pine.SUN.4.33.0106040957250.9001-100000@mamba.cs.Virginia.EDU>

[posted & mailed]

On Sun, 3 Jun 2001, brian d foy wrote:

> > That's all most of his "customers"
> > care about. AFAIK, most critics of his work don't provide any
> > alternative, or even offer to help him update his code to help reduce
> > the support burden.
>
> that's simply not true.  Matt refused help and walked away
> from supporting his stuff when he realized it was beyond his
> ability.  still, he distributes the bad code.

Okay, peace. I guess I didn't realize that "some solution is better
than none" is not true in general--negligence can cause more harm than
any benefit gained from the "solution".

Regards,
David



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

Date: Mon, 04 Jun 2001 11:06:12 -0400
From: Lou Moran <lmoran@wtsg.com>
Subject: Re: Help a script to prompt to input and have those input add or append certain lines in a text file
Message-Id: <vp8nhtk9qmqmcdrq990ptlmhh9ljkfdf73@4ax.com>

On 26 May 2001 19:43:28 GMT, anno4000@lublin.zrz.tu-berlin.de (Anno
Siegel) wrote wonderful things about sparkplugs:

SNIP
>
>Since you are frank about it, let me be too:  If we did this, we
>would be giving away work that other people, or even the very same
>people, get paid for.  This rarely happens in real life.
>
>Anno

Yikes!  I was merely translating the OP's (Steve?) message.  So for
Anno and posts below I would like to say; wasn't me.
--
BSOD? In my day we didn't have 0000FF!
lmoran@wtsg.com


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

Date: Mon, 04 Jun 2001 11:43:33 -0400
From: "Jason C. Hill" <jhill@technoslave.net>
Subject: Help with Range operators
Message-Id: <9fgad5$d09@spamz.news.aol.com>

I'm having problems with my range operators not working correctly.

I have a hash of arrays with data in them.  The data looks something like
this:

Process Name:
Current Date/Time
Collection

Response

Value in percentages
Stored Proc Name
=========
Data I want
=========

Ok

Obviously I want the stuff between the ==== and the ==== (variable length
but pattern match doesn't care).  Anyway, this is what I have:

sub formatData {
    
    for $hostName ( keys %infoOfHosts ) {
        for $row ( @{ $infoOfHosts{$hostName} }) {
            if ( $row =~ /^====/ ... /^====/) {
                print "$row";
            } ## end if
        } ## end for

    } ## end foreach
} ## End sub formatData

So, it goes through and grabs the array of each host in the hash.  It
then goes through each array and looks for ====

It returns the data back that I want semi-correctly for the first host.
Upon encountering the second host in the hash, it prints out the entire
array instead of just the parts between ==== and ====

The reason I say semi correctly on the first host is, for some odd reason
it prints out the Ok at the end of the array.  Which would lead me to
believe that searching for something that is the same doesn't want to
work, despite having used '...'

Thanks for any help.

	-Jason


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

Date: Mon, 04 Jun 2001 12:42:35 -0400
From: "Jason C. Hill" <jhill@technoslave.net>
Subject: Re: Help with Range operators
Message-Id: <9fgdrr$dn0@spamz.news.aol.com>

Well...I think I have what I want, but the way it works is kinda weird.

What I've done is this:

sub formatData {
    
    for $hostName ( keys %infoOfHosts ) {
        for  ( @{ $infoOfHosts{$hostName} }) {
            my $lines = $_;
            if (/====/ ... /====/) {
                print "$lines";
            }
        } ## end for

    } ## end foreach
} ## End sub formatData

Anyway, this posts the stuff I want, however, is there an easy way to get
rid of the extra ==== in there?

Right now I get returned data looking like 

======== ===== ==== == ====
data
data
data
======== ===== ==== == ====

other than doing:
 if ( ... ) {
	if ($lines !~ /====/){
		print "$lines";
	}
}

Thanks...


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

Date: Mon, 04 Jun 2001 16:43:03 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Help with Range operators
Message-Id: <3b1bb93e.178618688@news.erols.com>

On Mon, 04 Jun 2001 11:43:33 -0400, "Jason C. Hill"
<jhill@technoslave.net> wrote:

>  if ( $row =~ /^====/ ... /^====/) {

The first match test is bound to $row.
The second is bound to $_ .
Try this instead:

  if ( $row =~ /^====/ ... $row =~ /^====/) {



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

Date: Mon, 04 Jun 2001 12:48:07 -0400
From: "Jason C. Hill" <jhill@technoslave.net>
Subject: Re: Help with Range operators
Message-Id: <9fge67$dob@spamz.news.aol.com>

That just gets a DUH...I know this...why didn't I do it?!?!!? *grin*
I've run in to this problem before...that's the solution I took...I just
didn't go through and reuse any of my code ;-)

Thanks,

	-J

In article <3b1bb93e.178618688@news.erols.com>, "Jay Tilton"
<tiltonj@erols.com> wrote:

> On Mon, 04 Jun 2001 11:43:33 -0400, "Jason C. Hill"
> <jhill@technoslave.net> wrote:
> 
>>  if ( $row =~ /^====/ ... /^====/) {
> 
> The first match test is bound to $row. The second is bound to $_ .
> Try this instead:
> 
>   if ( $row =~ /^====/ ... $row =~ /^====/) {
>


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

Date: Mon, 4 Jun 2001 03:32:45 -0500
From: "Charles K. Clarkson" <c_clarkson@hotmail.com>
Subject: Re: Help with the $0 equivalent, portable for use in DOS/Windows/Win32-based environment.
Message-Id: <BD3B46323E48E9C0.2C8936796E0D029D.635E13AE4BBE4E20@lp.airnews.net>

codeslayer <weedmonster_99@yahoo.com> wrote:
: Greetings:
: 
: I have a simple question:
: 
: I am not using UNIX, (though I usually do at work),
: and I need a RELIABLE method for automatically grabbing
: the name of a PERL program and telling the user what it
: is, in DOS or Windows. I have already tried the
: following:
: 
:    print "The name of this file is $0 \n";

    This works fine on my win98 machine.

:      and
: 
:    print "The name of this file is $ARGV[0] \n";

    $ARGV[0] is not the program name in perl. You may
be thinking of C and argv[0]. perl uses $ARGV[0] as
the first argument on the command line.

HTH,
Charles K. Clarkson


Successful people do the things
 unsuccessful people don't like to do.





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

Date: Mon, 04 Jun 2001 14:05:23 GMT
From: "Maya" <m.astrakhanskaya@gte.net>
Subject: Re: HTTP::Request GET
Message-Id: <DAMS6.268$Lx.89407@paloalto-snr1.gtei.net>

Yes, the string should become part of the URL

<nobull@mail.com> wrote in message news:u97kyww42n.fsf@wcl-l.bham.ac.uk...
> "Maya" <m.astrakhanskaya@gte.net> writes:
>
> > Hi, what is the maximum length of the XML string I can send with a GET
> > method?
>
> Is this XML string to become part of the URL?
>
> AFAIK the Perl LWP, HTTP and CGI modules do not impose any length
> limits on URLs.
>
> Of course other things like web servers may impose a limit but that
> has nothing to do with Perl.
>
> --
>      \\   ( )
>   .  _\\__[oo
>  .__/  \\ /\@
>  .  l___\\
>   # ll  l\\
>  ###LL  LL\\




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

Date: Mon, 04 Jun 2001 10:48:16 -0400
From: Jerry <gordon@med.unc.edu>
Subject: Re: Komodo
Message-Id: <3B1B9FB0.875065AF@med.unc.edu>

Jerry wrote:
> 
> Hello,
>         I installed ActivePerl-5.6.1.626-MSWin32-x86-multi-thread.msi
> and
> Komodo-1.0.0-18562.msi and the license file for Komodo. If I start
> Komodo and immediately close it without opening a file my computer shuts
> down correctly. If I start Komodo and open a Perl source file and then
> close Komodo my computer will not shut down correctly even after killing
> mozilla, w9xpopen, and winoldapp. Please help.
>         I have uninstalled Komodo and Perl and reinstalled them.
>         There are some errors reported in the diagnostic files.
>         diagnostic files included below.
>         win98 se on a 450 MHz pentium with 384 MB RAM
>         Mcafee antivirus, Netscape, acrobat reader, usually running
>         Thanks very much,
> Jerry
> --
> Gerald W. Gordon, Ph.D.
> Department of Cell and Developmental Biology
> Taylor Hall, CB 7090
> University of North Carolina
> Chapel Hill, NC 27599

Hello,
	The problem described in the original post goes away if I turn off
virus protection. Originally I used Mcafee 4.x. Since then I have also
tried Norton AV 7.5 enterprise. In both cases when the virus protection
is active mozilla, w9xpopen, and winoldapp are still running after
Komodo per se is closed and I have problems shutting down the computer.
If I turn off the virus protection mozilla, w9xpopen, and winoldapp dies
with Komodo and I have no problem shutting down the computer.
	Cheers,
Jerry
-- 
Gerald W. Gordon, Ph.D.
Department of Cell and Developmental Biology
Taylor Hall, CB 7090
University of North Carolina
Chapel Hill, NC 27599

919/966-2941 (vox)
919/966-1856 (fax)
Gordon@med.unc.edu (email)


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

Date: Mon, 04 Jun 2001 15:07:29 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: locking files before writing data (?)
Message-Id: <3b1ba41e.34e1$19e@news.op.net>

In article <thmrs0pib1ks3c@corp.supernews.co.uk>,
Muddy Cat <muddycat@hotmail.com> wrote:
>> >&trim_log;
>> >open(USERLOG, ">>log.dat");
>> >seek(USERLOG,0,0);
>
>Oddly... the code has just been appending
>to the end of the file, rather than overwriting. 

That's because you opened it in append mode.  That is what >> means.

Try

        open(USERLOG, "+<log.dat") or die "Couldn't open log.dat: $!";

instead.

-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


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

Date: Mon, 04 Jun 2001 15:10:15 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: locking files before writing data (?)
Message-Id: <3b1ba4b4.34ed$1d5@news.op.net>

In article <thmqf2kcqmi922@corp.supernews.co.uk>,
Muddy Cat <muddycat@hotmail.com> wrote:>
>Is this safe to use "as is", 

If you are 100% sure that only one copy of the program will ever be
run at the same time.

If there is even a small chance that two copies might be running at
the same time, you need to consider locking issues.

>Is it worth doing for any size of project, or only if you are
>expecting an awful lot of traffic?

The size of the project does not matter.  Even tiny programs can show
bizarre failures if you do not get the locking right.

-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


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

Date: Mon, 4 Jun 2001 17:38:20 +0100
From: "Muddy Cat" <muddycat@hotmail.com>
Subject: Re: locking files before writing data (?)
Message-Id: <thne9lhhpmt0cd@corp.supernews.co.uk>

Thanks Mark.

is flock() the way to go? I don't really want a full answer or anything
laborious... rather just pointing in the right direction.






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

Date: 4 Jun 2001 17:50:22 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: locking files before writing data (?)
Message-Id: <991676644.12165@itz.pp.sci.fi>

In article <thne9lhhpmt0cd@corp.supernews.co.uk>, Muddy Cat wrote:
>
>is flock() the way to go? I don't really want a full answer or anything

Yes.

You don't necessarily need to lock a file for appending to it, provided
that certain conditions are met.  (See the Perl FAQ for details.)  But
if you want to remove old data, as you indicate, you _will_ need to lock
the file.

Of course, if your script is not used very frequently, you might get
lucky and never have any problems without locking.  But your luck might
run out at any time.

-- 
Ilmari Karonen - http://www.sci.fi/~iltzu/
Please ignore Godzilla / Kira -- do not feed the troll.


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

Date: 4 Jun 2001 07:25:43 -0700
From: msalerno@my-deja.com (Matthew J. Salerno)
Subject: Looking for critique - Please be kind !
Message-Id: <6622f4b.0106040625.48f2b659@posting.google.com>

I am working on a script that will sort 20+ gigs mp3's and I am almost
finished.  All I have left to do is make sure that no file is
overwrittn.  I am hoping that someone can give me some advice about
optomizing the script.  There will be 9000+ records in the initial
array.  Any assistance will be appreciated.

1. Remove "the" from the beginning of any file name.
2. Change all "_" into blank spaces.
3. Capitalize the first letter in each word.
4. Move all files into a specific dir depending on their first
character.

Thanks,
Matt

#!/usr/bin/perl -w
use strict;
use File::Find;
use File::Copy;

my $path = "/foo/bar/";
my $destpath = "/bar/foo/testdir";
my $extension = ".mp3";
my $logfpath = "/foo/log/cap.log";
my ($outpt, $var, $value, @files, @renary, %masterlst);

&getfiles;

open(LOGFILE, ">$logfpath") || die "Cannot create output log \-
$logfpath \- $!\n";

&renfiles;

&mvfiles;

close LOGFILE;

exit;


sub getfiles {
	find sub { push @files, $File::Find::name if /$extension$/i && -e; },
     	"$path";
	if ( $#files < 0 ){
		die "\nNo files found\n\n";
		}
}

sub renfiles {
	foreach (@files){
		my $fletter;
		my $i = 0;
		my @filepath = split ("/" , $_);
		my $filename = $filepath[$#filepath];
		$filename =~ s/^the//i;
		$filename =~ tr/_/ /;
		$filename =~ s/\b(\w)/\u$1/g;
		$filepath[$#filepath] = $filename;
		my $finalnm = join("/", @filepath);
		$filename =~ /\b./;
		$fletter = $&;
		$masterlst{$finalnm} = $fletter;
		if ( $_ ne $finalnm){
			$outpt = "Renaming $_ to $finalnm\n";
			$i++;
			}
		else {
			$outpt = "No Change - $_\n";
			}
		print "$outpt";
		print LOGFILE $outpt;
		push @renary, $finalnm;
		rename $_, $finalnm if $i > 0;
			}
}

sub mvfiles {
	my $destination;
	foreach $var (keys %masterlst) {
		$destination = "$destpath\/$masterlst{$var}" if $masterlst{$var} =~
/[a-z]/i;
		$destination = "$destpath\/other" if $masterlst{$var} !~ /[a-z]/i;
		if (!-e $destination && !-d $destination){
			mkdir $destination, 0777;
			}
		move ($var, $destination) || print "Cannot move file - $var - $!\n";
		my $mvoutpt = "Moving $var to $destination\/\n";
		print $mvoutpt;
		print LOGFILE $mvoutpt;
		}
}


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

Date: Mon, 04 Jun 2001 15:18:40 GMT
From: trammell@bayazid.hypersloth.invalid (John Joseph Trammell)
Subject: Re: Looking for critique - Please be kind !
Message-Id: <slrn9hn75u.i07.trammell@bayazid.hypersloth.net>

On 4 Jun 2001 07:25:43 -0700, Matthew J. Salerno <msalerno@my-deja.com> wrote:
[snip]
> 1. Remove "the" from the beginning of any file name.
> 2. Change all "_" into blank spaces.
> 3. Capitalize the first letter in each word.
> 4. Move all files into a specific dir depending on their first
> character.
[snip]
> #!/usr/bin/perl -w
> use strict;
> use File::Find;
> use File::Copy;
> 
> my $path = "/foo/bar/";
> my $destpath = "/bar/foo/testdir";
> my $extension = ".mp3";
> my $logfpath = "/foo/log/cap.log";
> my ($outpt, $var, $value, @files, @renary, %masterlst);

How about putting these into the scopes in which they're used,
rather than making them globally available?

> &getfiles;

So this might become:

  my @files = getfiles;


> open(LOGFILE, ">$logfpath") || die "Cannot create output log \-
> $logfpath \- $!\n";
> 
> &renfiles;
> 
> &mvfiles;
> 
> close LOGFILE;
> 
> exit;
> 
> 
> sub getfiles {

    my @files;

> 	find sub { push @files, $File::Find::name if /$extension$/i && -e; },
>      	"$path";
> 	if ( $#files < 0 ){
> 		die "\nNo files found\n\n";
> 		}

    return @files;

> }
> 
> sub renfiles {
> 	foreach (@files){
> 		my $fletter;
> 		my $i = 0;
> 		my @filepath = split ("/" , $_);
> 		my $filename = $filepath[$#filepath];
> 		$filename =~ s/^the//i;
> 		$filename =~ tr/_/ /;

Probably you want tr/ /_/ here; maybe you want something even more
restrictive, like tr/A-Za-z0-9/_/c ?

> 		$filename =~ s/\b(\w)/\u$1/g;
> 		$filepath[$#filepath] = $filename;
> 		my $finalnm = join("/", @filepath);
> 		$filename =~ /\b./;
> 		$fletter = $&;

Probably easier to use substr() here.

> 		$masterlst{$finalnm} = $fletter;
> 		if ( $_ ne $finalnm){
> 			$outpt = "Renaming $_ to $finalnm\n";
> 			$i++;
> 			}
> 		else {
> 			$outpt = "No Change - $_\n";
> 			}
> 		print "$outpt";
> 		print LOGFILE $outpt;
> 		push @renary, $finalnm;
> 		rename $_, $finalnm if $i > 0;
> 			}
> }
> 
> sub mvfiles {
> 	my $destination;
> 	foreach $var (keys %masterlst) {
> 		$destination = "$destpath\/$masterlst{$var}" if $masterlst{$var} =~
> /[a-z]/i;
> 		$destination = "$destpath\/other" if $masterlst{$var} !~ /[a-z]/i;

No need to escape the "/" character.

> 		if (!-e $destination && !-d $destination){
> 			mkdir $destination, 0777;
> 			}
> 		move ($var, $destination) || print "Cannot move file - $var - $!\n";
> 		my $mvoutpt = "Moving $var to $destination\/\n";
> 		print $mvoutpt;
> 		print LOGFILE $mvoutpt;
> 		}
> }

-- 
According to the Genesis account, the tower of Babel was man's second
major engineering undertaking, after Noah's ark.  Babel was the first
engineering fiasco.
                                - F. Brooks, _The Mythical Man-Month_


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

Date: Mon, 4 Jun 2001 11:12:43 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Looking for critique - Please be kind !
Message-Id: <slrn9hn9bb.f8.tadmc@tadmc26.august.net>

Matthew J. Salerno <msalerno@my-deja.com> wrote:

>All I have left to do is make sure that no file is
>overwrittn.  I am hoping that someone can give me some advice about
>optomizing the script.


I'm just going to address the overwriting issue.


>sub renfiles {
>	foreach (@files){

[snip]

>		my $finalnm = join("/", @filepath);
>		$filename =~ /\b./;
>		$fletter = $&;
>		$masterlst{$finalnm} = $fletter;


   if ( -e $finalnm ) {   # see: perldoc -f -e
      warn "Uh oh! '$finalnm' already exists. Delete it and try again\n";
   }


>		if ( $_ ne $finalnm){
>			$outpt = "Renaming $_ to $finalnm\n";
>			$i++;
>			}

[snip]

>sub mvfiles {
>	my $destination;
>	foreach $var (keys %masterlst) {
>		$destination = "$destpath\/$masterlst{$var}" if $masterlst{$var} =~
>/[a-z]/i;
>		$destination = "$destpath\/other" if $masterlst{$var} !~ /[a-z]/i;
                                         ^^
                                         ^^ don't need the backslash


>		if (!-e $destination && !-d $destination){
>			mkdir $destination, 0777;
>			}


Your logic is messed up there.

The -d test can only be reached if the $destination does not exist.

If it does not exist, in cannot be a directory, so no need to test :-)

   if ( -e $destination && -d $destination ) {
      # do what? Delete the dir's contents maybe...?
   }
   elsif ( -e $destination ) {
      warn "Uh oh! '$destination' already exists. Delete it and try again\n";
   }
   else {
      mkdir $destination, 0777;
   }


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


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

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


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