< < E-NEF > >
Perl Programming | Roman Art | Travel! | Maps ()
                  dragon                     e-nef                 Plan du Site
Contact
my.e-nef v0.1
  • Stable Linux Kernel: 2.6
  • Perl: 5.8
  • Apache: 2.0
  • mod_perl: 2.0
  • postgres: 8.0


Featuring


Search the Site





Bookmarks


Recommandation de Lecture


Questions ?
Tell us what you
thought of this page

 Excellent
 Worth reading
 Not worth reading

 Not technical enough
 Just right
 Too technical



Menu


Perl with NT

version franšaise

N.B. I have done those pages because it is really difficult to find these piece of informations (mid-1998). The ideal would be to do a full site on Perl and NT. If you have any comment or if you want to contribute, don't hesitate to write to me

Installing Perl

The best way to have an up-to-date Perl is to take the latest version, called perl5.xxxx-bindistxx-bc.zip available on all CPAN (Comprehensive Perl Archive Network) mirrors.

The best Path to install it is c:\perl.

As few are the NT that comes with a C compiler (apart from the commercial compilers or free e.g. the GCC portage by cygnus), at installation it is asked if you want to have the source code.

For that reason, many modules needing compilation come pre-compiled, the libnet network functions and LWP, Tk interface, Database Interface, HTML, HTTP, MIME, Graphic Device (GD), MD5 and of course Win32 (OLE, ODBC, ...).

It is also proposed to have the HTML documentation, which is more practical without a command-line facilitating environnement.

N.B. The versions distributed by ActiveWare always seems to be a version in late, and poorly furnished with with precompiled modules ...

Installing an environnement variable in the register database

Let's take the example of adding the path to the Perl interpreter in the NT environment variables.

It is in SYSTEM\CurrentControlSet\Control\Session Manager\Environment, so we open this key:

$p="SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
$main::HKEY_LOCAL_MACHINE->Open($p,$srv)||die "open: $!";
and we read the values in a Hash table:
$srv->GetValues(\%vals)||die "QueryValue: $!";
then we verify if the path isn't already set, if not, we add it:
if (!($vals{'Path'}[2] =~/perl/i)) {
        $value=$vals{'Path'}[2];
        $srv->SetValueEx("Path",0,REG_SZ,"c:\\perl\\bin;$value");
}

Install a Perl Script as a NT service

A NT service answers to an interface message call, to start it, pause it and stop it. So a signle script cannot be installed. The NT Ressource Kit 3 brings INSTSRV and ANYSRV, the first one to install a service name and the second one which is the service interface.

For example we create a service called PerlService :

`c:\\peliproc\\instsrv.exe PerlService c:\\peliproc\\srvany.exe`;
This installs an entry in the Registry Database. You can then finish hand installation as specified in the documentation or modify though the Win32::Registry the entries:

$p = "SYSTEM\\CurrentControlSet\\Services\\PerlService";
$main::HKEY_LOCAL_MACHINE->Open($p,$srv)||die "open: $!";
we are to create the key Parameters containing :
  • Application : the full path to the script
  • AppParameters : the execution parameters
  • AppDirectory : the path where from it will be executed
what we do this way:
$srv->Create("Parameters",$param)||die "Create: $!";
$param->SetValueEx("Application",0,REG_SZ,"c:\\perlproc\\scan.cmd");
$param->SetValueEx("AppParameters",0,REG_SZ,"2>c:\\tetmp\\toto.out");
$param->SetValueEx("AppDirectory",0,REG_SZ,"c:\\perlproc\\");
So we launch scan.cmd in the c:\perlproc directory (nota bene : it is needed to repeat the \ directory separator under NT, which serves to 'escape' i.e. use the specials caracters under Perl, so to have a \ we double it: \\). the argument is 2>c:\temp\toto.out, we redirect the standard error output (StdERR under perl, device 2 under each O.S.) to a file.

Now we can start the service.
`d:\\winnt\\system32\\net.exe start PerlService`;
The only thing that example hides is that the service is started with the SYSTEM account which under NT has more priorities than other account, even Administrator. It is possible through the Registry database to specify another user, but ... a password is needed, and I don't know how to add it trough the Registry database ...

Emulate a CRONTAB under NT

The at command and its interface winat can execute only asingle task a day, and this under the SYSTEM account. For some tasks, this is too constraining, e.g. scan a directory every 10 mn.

So, the best way is to create a NT service, and in the Perl script add an endless loop, and a sleep(600) for 10 mn i.e. 10*60 seconds.

Accessing an UNC reference under NT

It is easily done using the external command NET:

`d:\\winnt\\system32\\net.exe use \\server\ressource "password"
 /USER:"user_name"'
The best way to catch the return message is to redirect with 2> to a file and scann it afterward.

Hence, the current environment (CurrentSet) has memorized the acces granting to this resource, and to access it after, you only need to specify the path as a normal path :
open(FIC,"\\serveur\ressource\vol1\DD-XF-FF.080") || warn "$0 $!";
and it is all !

Map a drive under NT

To map a drive is to associate to a resource(share) on the network, a device, i.e. driver, shown as a: Ó z: .

Thsi is done the same way under NT, but to use it as a service this introduces the problem that the current user may already use this device (only 23 available at least). A drive mapped is only under the current user profile; connection-less or under another login, it will not be anymore, unless this new profile map it itself.

Sending mail under NT

There is two ways:

  • Outlook:
    The NT resource Kit brings a program called MAPISEND which permits to send a mail through an existing locally installed Outlook. It then need to only use the command line arguments.

  • SMTP Server or SMTP gateway under Exchange:
    This more standard method, thanks to the libnet library from Perl (not available with ActiveWare, you then need to use hand-made sockets...) which gives through Net::SMTP all the necessary interface to this protocol;

    The call is done with use Net::SMTP; , then you need to connect a SMTP Server :
    $smtp = Net::SMTP->new("$MAILHOST");
    and say who emits :
    $smtp->mail("epierre\@e-nef.com");
    and says to whom it is destined :
    $smtp->to("$EMAIL");
    and then we send the data :
    smtp->data();
    $smtp->datasend("To: $EMAIL\n");
    $smtp->datasend("Cc: $CONTACT\n");
    $smtp->datasend("Subject: Perl under NT, hard ?\n");
    
    Here, we specify again who we write to, then we define a CC (a Carbon Copy, a copy to another person), and then the subject after a keyword Subject.We then add the content of the mail by $smtp->datasend("") that we repeat as long as we have lines to send, and we close the mail by
    $smtp->dataend();
    $smtp->quit(); 
    
    The mail is now sent !

Using an Access Database through ODBC

Three steps are needed :

  1. Have the corresponding ODBC driver (Start/Control Panel/ODBC 32/)
  2. Add your base to the User DSN (see ODBC Home Page
  3. Access it
The last step is done through Perl :
  • the database has an alias name through the DSN which serves to call it. We create the Database Connector Object :
    $db = new Win32::ODBC($dsn);
  • we then form the SQL request :
    $sql = "SELECT * FROM directory WHERE priority <= $priority2 
    ORDER BY name,firstname ";
    for example.
  • we then emit the command :
    $db->Sql($sql);
  • we get the result (if many) :
    while ($db->FetchRow()) {
  • and we put in the variable the content of the record :
    ($name,$firstname,$tel)=$db->Data("name","firstname","tel")
    }
  • we can check the return code through :
    $db->Error();
  • and we close the database connection :
    $db->Close();
Simple, simple ... a lot, yes !

Other Links



PublicitÚ

© Copyright 1998-2005 Emmanuel PIERRE. Free reproduction under LLDDv1 License.
Send all comments to webmaster@e-nef.com
Last Update

Valid XHTML 1.0!

No Patents/