Hello Wise People,

I written the peice of code below but i am not sure if it is very good coding (as its my first go at doing perl). Its all in a bit of a jumble and i don't know if there are better ways of doing the tings i am trying to do. Help would be appreciated very much (because as you can see, i can't even lay it out properly).

#!/usr/bin/perl #Grab Input From Command Line use strict; use Getopt::Long; my ($username, $domain, $password, $email, $ip, $service); my $result = GetOptions("username=s" => \$username, "domain=s" => \$domain, "email=s" => \$email, "ip=s" => \$ip, "service=s" => \$service); #Generate Random Password And Encrypt It my @chars = ('a'..'z', 'A'..'Z', 0..9); my $plaintext_pass = do {{ local $_ = join "" => map {$chars [rand @chars]} 1..8; redo unless /[a-z]/ && /[A-Z]/ && /\d/; $_; }}; my $salt = join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]; my $encrypted_pass = crypt($plaintext_pass, $salt); #Take Current System Date And Add Two Weeks On For Trial use constant WEEK => 60 * 60 * 24 * 7 ; my @later = localtime( time + ( WEEK * 2 ) ) ; my $expiry_date = sprintf "%04d-%02d-%02d", $later[5] + 1900, $later[4] + 1, $later[3] ; #Add User if (!system("adduser $username -g 100 -s /bin/false -d /home/$username + -p $encrypted_pass -e $expiry_date")) { #Edit Postfix Virtual File my $datafile = '/etc/postfix/virtual' ; my $req_addr = "$username\@$domain" ; my ( $name, $domain ) = $req_addr =~ /^([\w.-]+)(@[\w.-]+)$/ ; open FH, $datafile or die "Couldn't read $datafile: $!" ; my @virtual = <FH> ; close FH ; open FH, ">$datafile" or die "Couldn't write $datafile: $!" ; my $wrote_it = 0 ; for ( @virtual ) { print FH $_ ; if ( $_ =~ /$domain/ && !$wrote_it ) { print FH "$req_addr\t$username\n" ; $wrote_it++ ; } } close FH ; system ("postmap /etc/postfix/virtual"); #Send Emails open (MAIL, "|/usr/sbin/sendmail -t"); print MAIL "To: bar\@foo.com\n"; print MAIL "Reply-to: $email\n"; print MAIL "From: $email\n"; print MAIL "Subject: AP3k New User Signup\n"; print MAIL "\n\n"; print MAIL "Test Mail1\n"; print MAIL "\n\n"; close (MAIL); open (MAIL, "|/usr/sbin/sendmail -t"); print MAIL "To: $email\n"; print MAIL "Reply-to: bar\@foo.com\n"; print MAIL "From: foo\@bar.com\n"; print MAIL "Subject: Test\n"; print MAIL "\n\n"; print MAIL "Test Mail2\n"; close (MAIL); print "Account Created Successfully\n"; exit 0; } else { print "Account Error\n"; exit 122; }


All Of The Variables Have Been Sanitised By Another Perl Script. Thanks

In reply to Desperate Coding Tidy-up by Anonymous Monk

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.