Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Coding for two platforms in one Script

by Kanji (Parson)
on Dec 05, 2001 at 21:59 UTC ( [id://129684]=note: print w/replies, xml ) Need Help??


in reply to Coding for two platforms in one Script

You're being tripped up timing as use happens at compile-time, but everything else in your script (including the logic to determine the OS) happens afterwards during run-time.

To get around this, you can change the uses to a require/import combo ...

if ( $^O eq "MSWin32" ) { require Win32::EventLog; $eventlog = Win32::EventLog->new( "myscript" ); } else { require Sys::Syslog; import Sys::Syslog qw/ :DEFAULT setlogsock /; setlogsock( 'unix' ); openlog( 'myscript', 'pid', 'INFO' ); }

On a side note, there's not much point in importing methods from an OO-based module like Win32::EventLog; $obj->foo should do the right thing if it's a valid method.

Similarly, it's silly to import from non-OO modules if you're just going to call those functions explicity ( Sys::Syslog::openlog() vs openlog()).

    --k.


Replies are listed 'Best First'.
Re: Re: Coding for two platforms in one Script
by BoredByPolitics (Scribe) on Dec 05, 2001 at 23:14 UTC

    Thanks for the reply Kanji.

    I've restructured my code as you advise, however, and this is why I was using the 'no strict "subs"' clause (although the context in my original post didn't make sense), I get an error on compilation.

    Here's the code snippet -

    if ($os_type eq "NT") { require Win32::EventLog; import Win32::EventLog qw / :DEFAULT /; my $eventlog_type; if ($priority ne "info") { $eventlog_type = EVENTLOG_WARNING_TYPE; } else { $eventlog_type = EVENTLOG_INFORMATION_TYPE; } $eventlog->Report({ EventType => $eventlog_type, Strings => $message, }); }

    This generates the errors -

    Bareword "EVENTLOG_WARNING_TYPE" not allowed while "strict subs" in us +e at scriptname line 2007. Bareword "EVENTLOG_INFORMATION_TYPE" not allowed while "strict subs" i +n use at scriptname line 2010.

    If I prefix the exported variables with Win32::EventLog, I get the same error. It's just struck me that this may be a problem with Win32::EventLog itself - I can see in EventLog.pm where the variables are exported, but I can't see where they're declared.... hmmmm.

    Pete

      The constants EVENTLOG_* are not grouped into an export group called ":DEFAULT". It looks like you'll have to ask for each one that you want:
      require Win32::EventLog; import Win32::EventLog qw / EVENTLOG_WARNING_TYPE EVENTLOG_INFORMATION +_TYPE /;

      buckaduck

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://129684]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-20 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found