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


    In reply to Balancing Coding Time And Code Quality by Limbic~Region

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.