[30669] in Perl-Users-Digest
Perl-Users Digest, Issue: 1914 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 10 14:14:30 2008
Date: Fri, 10 Oct 2008 11:14:24 -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 Fri, 10 Oct 2008 Volume: 11 Number: 1914
Today's topics:
finding newest file in a directory and removing the res <beefstu350@hotmail.com>
Re: finding newest file in a directory and removing the <jurgenex@hotmail.com>
Re: finding newest file in a directory and removing the <ben@morrow.me.uk>
Re: finding newest file in a directory and removing the <tadmc@seesig.invalid>
Re: Inplace editing the elegant way <JustMe@somewhere.de>
Re: Inplace editing the elegant way <jurgenex@hotmail.com>
Loading the shell environment? <g4173c@motorola.com>
Re: Loading the shell environment? <g4173c@motorola.com>
Re: Loading the shell environment? <ben@morrow.me.uk>
Re: Loading the shell environment? <jurgenex@hotmail.com>
Re: Loading the shell environment? <smallpond@juno.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 10 Oct 2008 07:27:35 -0700 (PDT)
From: Stu <beefstu350@hotmail.com>
Subject: finding newest file in a directory and removing the rest
Message-Id: <fc8d70bb-eacf-462f-ac22-8fd93dc9a8a4@z6g2000pre.googlegroups.com>
I was wondering if somebody can point me in the right direction?
I have a requirement to remove all the files in a given directory
except for the one that has been
created last. In ksh, to find this file I would not remove I would do
a ls -ltr | tail -1. Can somebody provide me with a function that can
do something similiar but with perl code.
Thanks in advance to all who answer this post
------------------------------
Date: Fri, 10 Oct 2008 08:45:02 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: finding newest file in a directory and removing the rest
Message-Id: <9atue4h19k6l3bsqmf4il4eu27agdiv49i@4ax.com>
Stu <beefstu350@hotmail.com> wrote:
>I have a requirement to remove all the files in a given directory
>except for the one that has been
>created last. In ksh, to find this file I would not remove I would do
>a ls -ltr | tail -1. Can somebody provide me with a function that can
>do something similiar but with perl code.
perldoc -f opendir
perldoc -f readdir (or instead perldoc -f glob)
perldoc -f -M (for a finer granularity see perldoc -f stat); Please be
advised that this provides the inode change time which may or may not
coincide with the file creating time. However this is the best guess on
many (most?) file systems because they don't track file creation time.
Besides, "creation time" is ambigious at best. If you copy a photo from
the memory card to you computer, at what moment was that file created:
when you hit the shutter or when you copied the file?
perldoc -f sort
perldoc -f unlink
jue
------------------------------
Date: Fri, 10 Oct 2008 17:14:16 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: finding newest file in a directory and removing the rest
Message-Id: <ojb4s5-dh3.ln1@osiris.mauzo.dyndns.org>
Quoth Stu <beefstu350@hotmail.com>:
> I was wondering if somebody can point me in the right direction?
>
> I have a requirement to remove all the files in a given directory
> except for the one that has been
> created last. In ksh, to find this file I would not remove I would do
> a ls -ltr | tail -1. Can somebody provide me with a function that can
> do something similiar but with perl code.
perldoc -f readdir
perldoc -f stat
Maybe perldoc -f -X
perldoc -f sort
perldoc File::stat
Ben
--
Many users now operate their own computers day in and day out on various
applications without ever writing a program. Indeed, many of these users
cannot write new programs for their machines...
-- F.P. Brooks, 'No Silver Bullet', 1987 [ben@morrow.me.uk]
------------------------------
Date: Fri, 10 Oct 2008 11:14:14 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: finding newest file in a directory and removing the rest
Message-Id: <slrngeuvqm.n6e.tadmc@tadmc30.sbcglobal.net>
Stu <beefstu350@hotmail.com> wrote:
> I was wondering if somebody can point me in the right direction?
>
> I have a requirement to remove all the files in a given directory
> except for the one that has been
> created last.
That is only possible on file systems that keep track of
a file's creation time.
Most *nix filesystems do not record creation time at all.
If you can live with modification time instead of creation time,
then this should do it:
my(undef, @f) = sort { -M $a <=> -M $b } grep -f, glob '*';
unlink @f;
(but that does not remove all old files, old files that start
with a dot will be left alone.
)
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Fri, 10 Oct 2008 19:23:06 +0200
From: Hartmut Camphausen <JustMe@somewhere.de>
Subject: Re: Inplace editing the elegant way
Message-Id: <MPG.2359ade7f9f4eb83989697@news.t-online.de>
J=FCrgen Exner schrieb:
> I'd probably store the REs pattern and replacement text as key/value
> pairs in a hash(*) and just loop through them.
> And a separate hash or array for the "delete" patterns.
>=20
> *: that should work, because REs are just strings, too.
I'd consider to store the /compiled/ patterns + replacement strings in=20
an array - if efficiency is an issue.
Else each pattern would have been recompiled for each chunk of data.
jm2p + mfg, Hartmut
=20
--=20
------------------------------------------------
Hartmut Camphausen h.camp[bei]textix[punkt]de
------------------------------
Date: Fri, 10 Oct 2008 10:40:51 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Inplace editing the elegant way
Message-Id: <ns4ve4lnt6eq1v4ak57v8901dtjafgkjde@4ax.com>
Hartmut Camphausen <JustMe@somewhere.de> wrote:
>Jürgen Exner schrieb:
>
>> I'd probably store the REs pattern and replacement text as key/value
>> pairs in a hash(*) and just loop through them.
>> And a separate hash or array for the "delete" patterns.
>>
>> *: that should work, because REs are just strings, too.
>
>I'd consider to store the /compiled/ patterns + replacement strings in
>an array - if efficiency is an issue.
Nice idea!
jue
------------------------------
Date: Fri, 10 Oct 2008 08:22:00 -0700 (PDT)
From: T <g4173c@motorola.com>
Subject: Loading the shell environment?
Message-Id: <bac5321a-9817-4f3a-8911-88098cdb0706@h60g2000hsg.googlegroups.com>
Greetings:
We use Modules ( I hate that they chose that name) to load our
shell environment here. So I can do:
mod load myproject
"mod" is an alias to:
/cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load hagrid
------------------------------
Date: Fri, 10 Oct 2008 08:26:47 -0700 (PDT)
From: T <g4173c@motorola.com>
Subject: Re: Loading the shell environment?
Message-Id: <bfdb2723-23dd-48cd-9ff3-6da2ddb0fe1f@w39g2000prb.googlegroups.com>
On Oct 10, 11:22=A0am, T <g41...@motorola.com> wrote:
> Greetings:
>
> =A0 =A0 We use Modules ( I hate that they chose that name) to load our
> shell environment here. So I can do:
>
> =A0 =A0 mod load myproject
>
> =A0 =A0"mod" is an alias to:
>
Sorry about that...
geting back
/cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load myproject
in a bash shell script I can do:
eval `/cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load
myproject`
and this works, I tried the equivlant in perl:
eval `/cae/Modules/$ENV{'MODULE_VERSION'}/bin/modulecmd tcsh load
myproject`
along with several other variants without any luck. I was hoping to
avoid writing
a shell wrapper around my Perl script. Does anyone know how to load
the shell environment
in a Perl script? This script will run in crontab file that's why I
need to load the env.
Thanks in Advanced for any help!
Tom
------------------------------
Date: Fri, 10 Oct 2008 17:17:15 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Loading the shell environment?
Message-Id: <bpb4s5-dh3.ln1@osiris.mauzo.dyndns.org>
Quoth T <g4173c@motorola.com>:
>
> in a bash shell script I can do:
>
> eval `/cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load
> myproject`
>
> and this works, I tried the equivlant in perl:
>
> eval `/cae/Modules/$ENV{'MODULE_VERSION'}/bin/modulecmd tcsh load
> myproject`
>
> along with several other variants without any luck. I was hoping to
> avoid writing
> a shell wrapper around my Perl script. Does anyone know how to load
> the shell environment
> in a Perl script? This script will run in crontab file that's why I
> need to load the env.
In general there isn't any simple way. If the output of `modulecmd` is
simple, perhaps something like
export FOO="BAR"
export BAZ="QUUX"
without any tricky shell syntax, you could parse it in Perl and insert
the results into %ENV. Otherwise, you will need a shell wrapper.
Ben
--
For far more marvellous is the truth than any artists of the past imagined!
Why do the poets of the present not speak of it? What men are poets who can
speak of Jupiter if he were like a man, but if he is an immense spinning
sphere of methane and ammonia must be silent? [Feynmann] ben@morrow.me.uk
------------------------------
Date: Fri, 10 Oct 2008 09:23:21 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Loading the shell environment?
Message-Id: <750ve4pk6nv2hmd3l3rhc6a27o8agftbss@4ax.com>
T <g4173c@motorola.com> wrote:
>a shell wrapper around my Perl script. Does anyone know how to load
>the shell environment
>in a Perl script? This script will run in crontab file that's why I
>need to load the env.
This is a variation of 'perldoc -q environment'.
The `...` starts a child process, runs the modulecmd command in that
child process, and then throws away the results because parent processes
don't inherit environment changes from their children.
jue
------------------------------
Date: Fri, 10 Oct 2008 09:26:56 -0700 (PDT)
From: smallpond <smallpond@juno.com>
Subject: Re: Loading the shell environment?
Message-Id: <cf095c05-5882-47cb-8ae9-a4f3b584b200@h2g2000hsg.googlegroups.com>
On Oct 10, 11:26 am, T <g41...@motorola.com> wrote:
> On Oct 10, 11:22 am, T <g41...@motorola.com> wrote:> Greetings:
>
> > We use Modules ( I hate that they chose that name) to load our
> > shell environment here. So I can do:
>
> > mod load myproject
>
> > "mod" is an alias to:
>
> Sorry about that...
> geting back
>
> /cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load myproject
>
> in a bash shell script I can do:
>
> eval `/cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load
> myproject`
>
> and this works, I tried the equivlant in perl:
>
> eval `/cae/Modules/$ENV{'MODULE_VERSION'}/bin/modulecmd tcsh load
> myproject`
>
> along with several other variants without any luck. I was hoping to
> avoid writing
> a shell wrapper around my Perl script. Does anyone know how to load
> the shell environment
> in a Perl script? This script will run in crontab file that's why I
> need to load the env.
>
> Thanks in Advanced for any help!
>
> Tom
1) Don't use pronouns without a referent. Nobody here knows who
"they" are.
2) perl is run from a shell, it doesn't run the shell. If you try to
start another shell from perl
it will end when the perl script does.
3) perl eval executes perl code, not shell code. Calling a function
without reading the documentation
because the name sounds right and it might do what you want is derided
as "Cargo Cult Programming"
4) there is no CPAN code for improving your luck. Feel free to write
back when you understand how to
check the error status from calls and can describe an actual error.
Make sure you have these two
lines at the beginning of your script:
use warnings;
use strict;
------------------------------
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 1914
***************************************