Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: check for latest file, remove and touch file?

by mr_mischief (Monsignor)
on Jan 05, 2016 at 15:31 UTC ( #1151969=note: print w/replies, xml ) Need Help??


in reply to check for latest file, remove and touch file?

This seems overcomplicated.

Do you want the "latest file" as per the filesystem or the highest-sequence file as per your naming convention? Is the inode change time important? The modification time? Is it just the filename? What if the files get out of sync and you end up having S0007 but not S0006? Do you want to still delete S0005 if it exists? Why not sort your files, pop the two newest off the array, then delete the rest if making sure you have the two newest available is what you're actually trying to do?

You should learn to use Perl modules, but if you're in a hurry what does the Unix touch command buy you here that open and close don't?

sub my_touch { my $f = shift; open my $fh, '>>', $f or die "Can't write to $f: $!\n"; close $fh; }

Why are you shelling out to ls when you have opendir and readdir or glob?

Also, I'm not going to recommend a formatting standard because that's kind of your own choice, but I do recommend you choose one and stick to it. It will be easier to pick one that is fairly common.

Update: changed the mode of the open in the example code to append per the suggestion by AnomalousMonk.

Replies are listed 'Best First'.
Re^2: check for latest file, remove and touch file?
by AnomalousMonk (Chancellor) on Jan 05, 2016 at 19:19 UTC
    open my $fh, '>', $f or die "Can't write to $f: $!\n";

    Won't open-ing the file named  $f in  '>' write mode clobber the current contents of the file at the same time it changes time/date? Wouldn't  '>>' append mode be better?


    Give a man a fish:  <%-{-{-{-<

      At least in my environment (AIX), opening the file in append mode without adding any content does not update the modification date of the file.

      $: date Thu Jan 7 09:37:01 CST 2016 $: touch step.file $: ls -l step.file -rw-r--r-- 1 wlsedi wlsedi 0 Jan 07 09:37 step.file $: date Thu Jan 7 09:38:01 CST 2016 $: touch step.file $: ls -l step.file -rw-r--r-- 1 wlsedi wlsedi 0 Jan 07 09:38 step.file $: date Thu Jan 7 09:39:38 CST 2016 $: perl -de 1 Loading DB routines from perl5db.pl version 1.28 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 1 DB<1> open $fh,'>>','step.file' DB<2> close $fh DB<3> q $: ls -l step.file -rw-r--r-- 1 wlsedi wlsedi 0 Jan 07 09:38 step.file

      Opening in write does have the un-touchlike side effect of wiping out any content. In this particular instance, that is of no consequence, but using the code as a generic replacement for touch is problematic.

      But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

      Assuming the file already exists, yes append mode would be better. If it doesn't already exist, then the two are equivalent. To make it more similar to touch it should be appending. You're right about that.

Re^2: check for latest file, remove and touch file?
by fasoli (Beadle) on Jan 05, 2016 at 16:25 UTC

    "Do you want the "latest file" as per the filesystem or the highest-sequence file as per your naming convention? Is the inode change time important? The modification time? Is it just the filename? What if the files get out of sync and you end up having S0007 but not S0006? Do you want to still delete S0005 if it exists? Why not sort your files, pop the two newest off the array, then delete the rest if making sure you have the two newest available is what you're actually trying to do?"

    Sorting the files and popping the two newest off the array does sound like a good idea but I'll have to think about it and see if it works for my case. Thank you for the recommendation.

    I thought about this and I think the best way to proceed in my case will be to look for a file with the biggest number (S0007 over S0005 for example), then look if the previous one is there, remove the previous one and proceed to create the next one. So if 7 is there, check if 6 is there, delete 6 and proceed to create 8. I'll have to think about the rest of my script and read up a tutorial to figure out my other issues, but if I can get past the thing that I just described it would be great. Can someone help me regarding how I can do that within my $step variable? How can I say "if we are at step x, check step x-1 exists and create step x+1"?

      Can someone help me regarding how I can do that within my $step variable? How can I say "if we are at step x, check step x-1 exists and create step x+1"?

      At this point you really should step back from your thinking on how to solve your "problem" and reexamine the overall task. Please say what you're really trying to do (beyond manipulating filenames), and especially what data you want to have available as your "backup."

      It's possible that your whole premise "a script for data handling that will loop through a variety of files and do a variety of things" could be misguided.

      The way forward always starts with a minimal test.
Re^2: check for latest file, remove and touch file?
by fasoli (Beadle) on Jan 05, 2016 at 15:48 UTC

    Thank you very much for your comment. I'll have to study it a bit but quickly let me ask you what you mean with sticking to a formatting standard? What formatting are you referring to? Sorry, I'm really a beginner, as if it's not obvious.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1151969]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-06-20 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (117 votes). Check out past polls.

    Notices?