Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How to execute succcessful this Perl script in Linux, window and solaris?

by wxfjordan (Initiate)
on Jun 29, 2006 at 04:01 UTC ( #558207=perlquestion: print w/ replies, xml ) Need Help??
wxfjordan has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks: I am a Perl beginner. Hope don't mind my simple question of Perl. I first time come to this site. So difficult find Perl good site. I want this perl script to run in Linux, solaris and windowXP. How to modify this script and make it running in different OS. Hope some Perl senior help me. Thanks!!!!
#!/usr/bin/perl -w if ( $^O =~ /^(MS)?Win/ ) { eval{ require Win32:: DriveInfo; $TotalNumberOfFreeBytes = (Win32:: DriveInfo:: DriveSpace('c:'))[6]; $TotalNumberOfBytes = (Win32:: DriveInfo:: DriveSpace('c:'))[5]; print "This is $^O \n"; print "Total Free: $TotalNumberOfFreeBytes\tTotal size: $TotalNumberOf +Bytes\n"; }; print $@,"\ndone!"; } elsif ( $^O =~ /^linux/ ) { print "This is Linux OS!!!\n"; }
This perl script can execute successful in linux, but print out a message when I execute this Perl script in windoowXP.
Too late to run INIT block at C:/Perl/site/lib/Win32/API/Type.pm line +71. This is MSWin32 Total Free: 26845294592 Total size: 31461662720 done! How to handle the first message in windowXP : Too late to run INIT block at C:/Perl/site/lib/Win32/API/Type.pm line +71.

Added code tags - dvergin 2006-06-28

Comment on How to execute succcessful this Perl script in Linux, window and solaris?
Select or Download Code
Re: How to execute succcessful this Perl script in Linux, window and solaris?
by ioannis (Priest) on Jun 29, 2006 at 04:52 UTC
    Perhaps you are already aware; but as a reminder, the standard first top to documentation is the Perl Portability Guide perlport
Re: How to execute succcessful this Perl script in Linux, window and solaris?
by ikegami (Pope) on Jun 29, 2006 at 05:11 UTC

    Update: While trying to reproduce the problem minimally, I realized I misunderstood what I read earlier or what I read was wrong. Disregard this post.

    The problem is that DriveSpace has a prototype, but Perl didn't know that when it compiled the call to DriveSpace. You therefore end up with a prototype mismatch. Someone asked the very same question yesterday. See Sidhekin's answer for solutions. The most straightforward solution is to replace
    Win32::DriveInfo::DriveSpace(...)
    with
    &Win32::DriveInfo::DriveSpace(...)
    This bypasses the prototype.

Re: How to execute succcessful this Perl script in Linux, window and solaris?
by shmem (Canon) on Jun 29, 2006 at 06:03 UTC
    Maybe this happens because you use require instead of use, thus no import.

    update - been too dizzy this morning :-(

    Turn the eval block into a string eval and wrap it in a BEGIN block. This way the eval is executed and the module loaded before the call to Win32::DriveInfo::DriveSpace is even compiled. I can't test, because I don't have a Windows box at hand.

    Put this at the very beginning of your script:

    BEGIN { if ($^O =~ /^(MS)?Win/) { eval "use Win32::DriveInfo"; die $@ if $@; } }

    What happens if you turn the eval block into a string eval? I can't test, because I don't have a Windows box at hand.

    if ( $^O =~ /^(MS)?Win/ ) { eval "use Win32::DriveInfo"; $TotalNumberOfFreeBytes = (Win32::DriveInfo::DriveSpace('c:'))[6]; $TotalNumberOfBytes = (Win32::DriveInfo::DriveSpace('c:'))[5]; print "This is $^O \n"; print "Total Free: $TotalNumberOfFreeBytes\tTotal size: $TotalNumb +erOfBytes\n"; print $@,"\ndone!"; } elsif ( $^O =~ /^linux/ ) { print "This is Linux OS!!!\n"; }

    --shmem

    update: changed "require" to "use" in the string eval. Small negligence after copy & paste.

    _($_=" "x(1<<5)."?\n".q/)Oo.  G\        /
                                  /\_/(q    /
    ----------------------------  \__(m.====.(_("always off the crowd"))."
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      makes no difference. It is still a require.
        I actually meant use. I forgot to change 'require' to 'use' after copy & paste. Fixed. You could have /msg'ed me, btw ,-)

      Update: While trying to reproduce the problem minimally, I realized I misunderstood what I read earlier or what I read was wrong. Disregard this post.

      I don't have that module, but I don't see how your solution could work. Win32::DriveInfo::DriveSpace('c:') is still being compiled before use Win32::DriveInfo; is executed, so the prototype mismatch is still present.

        Darn... too early in the morning.

        what I really meant is the following at the very beginning of the script:

        BEGIN { if ($^O =~ /^(MS)?Win/) { eval "use Win32::DriveInfo"; } }

        Thanks for another slap ;-)

        --shmem

        _($_=" "x(1<<5)."?\n".q/)Oo.  G\        /
                                      /\_/(q    /
        ----------------------------  \__(m.====.(_("always off the crowd"))."
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: How to execute succcessful this Perl script in Linux, window and solaris?
by esskar (Deacon) on Jun 29, 2006 at 06:08 UTC
    well, it is just a WARNING, and nothing you must care about. it' beacuse you are using require to import the module. There is already a bug-report-ticket issued on that "problem".

      another thing. if you do not need STDERR you can do a dirty trick! ;)
      #!/usr/bin/perl -w use strict; use warnings; if ( $^O =~ /^(MS)?Win/ ) { eval { #dirty hack to avoid the warning close STDERR; require Win32::DriveInfo; my $TotalNumberOfFreeBytes = (Win32::DriveInfo::DriveSpace('c: +'))[6]; my $TotalNumberOfBytes = (Win32::DriveInfo::DriveSpace('c:'))[ +5]; print "This is $^O \n"; print "Total Free: $TotalNumberOfFreeBytes\tTotal size: $Total +NumberOfBytes\n"; }; print $@, "\ndone!"; } elsif ( $^O =~ /^linux/ ) { print "This is Linux OS!!!\n"; }
      HTH

        close(STDERR) affects the whole program. There are better ways of suppressing warnings: (best to worse)

        • { no warnings 'warningtype'; ... } (Compile- and run-time, specific)
        • { no warnings; ... } (Compile- and run-time, broad)
        • { local $SIG{__WARN__} = {}; ... } (Run-time, broad)
        • { local *STDERR; ... } (Run-time, broad, has side effects.)

        And you're only hiding the symptom, not fixing the problem. Like the warning says, a piece of code that should get executed is not getting executed.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://558207]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (15)
As of 2014-07-30 19:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (240 votes), past polls