[KLUG Members] RE: To shell or not to shell RESOLVED

Jamie McCarthy members@kalamazoolinux.org
Wed, 17 Jul 2002 12:02:40 -0400


gettig@chartermi.net (Tony Gettig) writes:

> http://www.penguindude.com/ldbat.pl.txt

Cool!

Just some random comments... thoughts on using perl's quirks to
best advantage...


There's no need to test for a file's existence to decide whether to
open it for writing or appending.  If $bat_file doesn't exist,
open(BATFILE, ">>$bat_file") is effectively the same as
open(BATFILE, ">$bat_file").

I would have written this:

    $username=substr($line, 1, 6);
    $firstname=lc(substr($line, 32, 20));
    $middlename=lc(substr($line, 52, 15));
    $middleinitial=uc(substr($middlename, 0, 1));
    $lastname=lc(substr($line, 7, 25));
    $yog=substr($line, 67, 4);
    $location=substr($line, 71, 25);
    $homeroom=substr($line, 96, 5);

slightly more self-documenting, as:

    ($username, $lastname, $firstname,
     $middlename, $middleinitial,
     $yog, $location, $homeroom) =
    $line =~
         m[
            ^
            .               # first char ignored
            (.{6})          # next  6 chars username
            (.{25})         # next 25 chars lastname
            (.{20})         # next 20 chars firstname
            (               # next 15 chars middlename
                (.)         #   (of which first is initial)
                .{14}
            )
            (.{4})          # next  4 chars yog
            (.{25})         # next 25 chars location
            (.{5})          # next  5 chars homeroom
         ]x;

And this:

   $ldif_command="dn: cn=$cn, ou=$ou1, ou=$ou2, o=$o\n";
   $ldif_command.="changetype: $changetype\n";
   $ldif_command.="sn: $sn\n";
   $ldif_command.="givenname: $givenname\n";
   $ldif_command.="objectClass: top\n";
   $ldif_command.="objectClass: $objectclass\n";
   $ldif_command.="fullName: $fullName\n";
   $ldif_command.="ndsHomeDirectory: $ndsHomeDirectory\n";
   $ldif_command.="description: $description\n\n";
   # could add more LDAP fields as necessary

can be written with a "here document" syntax, again, same thing
but maybe a little easier to read:

   $ldif_comment = <<EOT;
dn: cn=$cn, ou=$ou1, ou=$ou2, o=$o
changetype: $changetype
sn: $sn
givenname: $givenname
objectClass: top
objectClass: $objectclass
fullName: $fullName
ndsHomeDirectory: $ndsHomeDirectory
description: $description

EOT
   # could add more LDAP fields as necessary