Hi People

There are two things that i would like to ask you about the code below:

1. Can it be improved, and if so, how?
2. Can someone show me how i can do some kind of error trapping on this? I want the script to 'unlink $to_delete' if there is a problem creating the user account and log the error to a file, or if there is a problem creating quota/opening-closing files just log the error.
#! /usr/bin/perl -w use strict; #################################### # Open Directory And Pick Up Files # #################################### my $path='/var/log/cr_user'; @ARGV=(); my @files=glob "$path/*"; while (@files) { my $to_delete=shift @files; push @ARGV,$to_delete; while (<>) { chomp; my ( $fullname, $username, $mothers, $email, $ip ) = split /,/; ############################### # Encrypt Password Using Salt # ############################### my @chars = ('a'..'z', 'A'..'Z', 0..9); my $password = do {{ local $_ = join "" => map {$chars [rand @chars]} 1..8; redo unless /[a-z]/ && /[A-Z]/ && /\d/; $_; }}; my $salt = '$1$' . join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z') [ map { int rand 64 } 0 .. 7 ]; my $epassword = crypt($password, $salt); ################################# # Create User Account And Quota # ################################# system ('/usr/sbin/useradd', '-g', 'users', '-d', "/home/$username", '-p', $epassword, '-s', '/bin/false', '-c', $fullname, $username); system ('/usr/sbin/edquota', '-p', "control", $username); ################################################ # Edit Postfix Virtual File And Write It Back. # ################################################ my $datafile = '/etc/postfix/virtual'; my @reg_addrs = ("$username\@domain1.com", "$username\@domain2.com"); open FH,"<",$datafile; my @virtual = <FH>; close FH; for my $reg_addr (@reg_addrs) { my ($name,$domain) = split /@/,$reg_addr; my ($i) = 0; for my $entry (@virtual) { ++$i; if ($entry =~ /\Q$domain\E/) { splice @virtual,$i,0,"$reg_addr\t$username\n"; last;}}} open FH,">",$datafile; print FH @virtual; close FH; system ('/usr/sbin/postmap', 'hash:/etc/postfix/virtual'); system ('/usr/sbin/postfix', 'reload'); ######################################## # Send E-Mails To Support And New User # ######################################## open (MAIL, "|/usr/sbin/sendmail -t"); print MAIL <<"END_OF_MAIL"; To: support\@foo.com Reply-to: $email From: $email Subject: foo Blah END_OF_MAIL close (MAIL); open (MAIL, "|/usr/sbin/sendmail -t"); print MAIL <<"END_OF_MAIL"; To: $email Reply-to: support\@foo.com From: support\@foo.com Subject: foo $fullname, You Suck END_OF_MAIL close (MAIL); } unlink $to_delete; }

In reply to Error trapping, and improving code by hiddenlinux

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.