http://www.perlmonks.org?node_id=312051

Salutations:
Today at work, I received the following request:

Quick, I need to know every file that only exists on one of two servers and which server it is on as fast as possible

I started the following command on both servers, while I worked on the code:

# find / > /tmp/`hostname`
Now since I was pretty sure the requirements and formatting would change, I didn't spend any time with a 1 liner. I am not any good at them anyway. Here is what I came up with in an honest 5 minutes:
#!/usr/bin/perl use strict; use warnings; use Getopt::Std; my %opt; Get_Args(); open (MASTER , '<' , $opt{m}) or die "Unable to open $opt{m} as master + : $!"; open (SLAVE , '<' , $opt{s}) or die "Unable to open $opt{s} as slave +: $!"; open (OUTPUT , '>' , $opt{o}) or die "Unable to open $opt{o} for outpu +t : $!"; select OUTPUT; my (%master , %slave); %slave = map {chomp; $_ => undef} <SLAVE>; while ( <MASTER> ) { chomp; print "$_ exists on master but not slave\n" if ! exists $slave{$_} +; $master{$_} = undef; } delete @slave{ keys %master }; print "$_ exists on slave but not master\n" for keys %slave; sub Get_Args { my $Usage = qq{Usage: $0 -m <master file> -s <slave file> -o <outp +ut file> -h : This help message. -m : master file -s : slave file -o : output file } . "\n"; getopts( 'hm:s:o:' , \%opt ) or die $Usage; die $Usage if $opt{h} || ! $opt{m} || ! $opt{s} || ! $opt{o}; }
I keep a template file around with the Get_Args() sub in it already, so other than tweaking it, it wasn't factor.

Ok - so it did the job. Now I am wondering if I should modify it at all and, if so, how. It obviously isn't optimized to be memory efficient, but I am not sure that it needs to be. It is simple enough that maintainability isn't a concern. I have the time to work on it if I feel like it, but I also should use my time wisely.

So as I meditate on this, I would appreciate some of your insight and feedback.

  • In my haste, did I miss any gotchas?
  • What would you have come up with in the same circumstances?
  • Assuming my circumstances and code, what would you do now?

    Cheers - L~R