Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The first comment I would make is "if it works, don't fix it". The next comment would be that if you find you do need to fix it at some point, or need to write something else of a similar nature, doing it with fewer lines will generally be better. I'm not pushing for a crypto/obfu/golf style -- just use a few more of the syntactic options that perl offers for the sake of clear brevity; e.g.:
# instead of this: if ($user_name eq "") { print STDOUT "User name cannot be blank.\n"; exit 1; } # do this die "User name cannot be blank.\n" unless ( $user_name ); # (numerous examples of that sort) #instead of this: my $out_FH=""; if ($output_file eq "") { $out_FH = \*STDOUT; } else { unless (open OUT_FH,">$output_file") { return 0; } $out_FH=\*OUT_FH; } return $out_FH; # do this: my $out_FH = ( $output_file eq "" ) ? \*STDOUT : ( open( OUT_FH, ">$output_file" )) ? \*OUT_FH : undef; return $out_FH; # and similarly, instead of this: if (!defined($conf_groups->{$ln_group})) { return 0; } return 1; # do this: return ( defined( $conf_groups->{$ln_group} ));
You could reduce your "Open_input_file" a lot by simplifying the error checking: don't bother with "-s", just read the file into @list (grepping off unwanted lines (blanks/comments) as needed) chomp @list, and return it, and have the caller check for an empty list.

You could also put some of the option strings into a hash (user_name, password, input_file, etc) -- since you prompt for these when they aren't stated on the command line, it will be easier to loop through the hash elements to do the "if(empty){prompt,read,check}" stuff, rather than coding a separate block to do this for each parameter; e.g.:

for my $opt ( qw/user_name password input_file/ ) { next if ( $opthash{$opt} ); print "You need to specify a $opt: " if ( $opt eq "password" ) { ReadMode( "noecho" ); else { ReadMode( "normal" ); } $opthash{$opt} = ReadLine(0); ReadMode( "restore" ); die "You really should have typed something for $opt\n" unless ( $opthash{$opt} ); } # this input mechanism only needs to be written once
Finally, when you have a long usage message to print, this sort of "here-document" layout is easiest:
my $Usage = <<ENDUSE; This is a great program and I'd like to fill up your console with pithy lines of information about all the stuff it can do. blah blah blah blah ENDUSE

In reply to Re: Batch Router by graff
in thread Batch Router by crackotter

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others admiring the Monastery: (6)
    As of 2024-07-17 10:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.