LPM: Many Monkeys - conditional use

David Hempy hempy at ket.org
Fri Mar 24 13:10:30 CST 2000


At 08:08 AM 3/24/00 -0500, Rich Bowen wrote:
>Oooh, I know!
>use statement get executed first. Always. So your if is ignored. You
>need to require it, or do some fancy magic in BEGIN blocks. Perhaps ...
>
>
>BEGIN {
>     unless (Win32::FsType() =~ /NTFS/) {
>             print "Can't do permissions here...it's as safe
>                       as its going to get.\n ";
>             exit(0);
>}
>use Win32::FileSecurity;
>
>The BEGIN block gets done before the use.
>
>Or, if you use require, and then call import() to get the functions that
>you want ... which is sort of what use does anyway.
>
>Rich

Hmm... I'll have to read up on BEGIN blocks.  Sounds like its got a lot of 
potential.

This isn't exactly what I'm looking for.  I'd still like to run my program 
on Win98, but just have it skip the file permission parts it can't 
handle.  I've got the skip part working (at least, it looks like it should 
work), but the program never gets executed due to the compiler error.

Any other ideas?  Is there anything like C's preprocessor commands I could 
connive into working?


(time passes in the R&D department...)


Okay...I found a functional way of doing it.  require appears to be a 
runtime thing, as opposed to use which is handled early in the compilation. 
(As Rich explained)  This gives two advantages: you can employ a variable 
with require, where you cannot with use; you can call require 
conditionally, where use is called regardless.

Programming Perlm, pp. 285-6 says that:

         use Module LIST

is exactly equivalent to:

         require "Module.pm";
         Module->import(LIST);


So, I changed my faulty OS-detecting code from:


         if (Win32::IsWinNT()) {
                 use Win32::FileSecurity;
         }


...to the following, which actually works:




         $package = "Win32\\FileSecurity.pm";

         if (Win32::IsWinNT()) {
                 print "We're on NT!  Let's require $package!\n";
                 require $package ;
                 Win32::FileSecurity->import(qw(MakeMask Set Get));
         } else {
                 print "We're NOT on NT! Let's NOT require $package!\n";
         }

         ## lots of interesting code goes here.

         if (Win32::FsType() =~ /NTFS/) {
                 $readonly_access        = MakeMask( qw( READ ) );
                 print "Successfully set up a mask.\n";
         } else {
                 print "Can't do permissions on a FAT partition.  Sorry.\n ";
         }



Hope this helps someone some day.

-dave


--
David Hempy
Internet Database Administrator
Kentucky Educational Television
<hempy at ket.org> -- (606)258-7164 -- (800)333-9764 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/archives/lexington-pm/attachments/20000324/60e76eb3/attachment.htm


More information about the Lexington-pm mailing list