Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

use strict and Win32::API

by AcidHawk (Vicar)
on Feb 14, 2003 at 09:30 UTC ( [id://235210]=perlquestion: print w/replies, xml ) Need Help??

AcidHawk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have the following code to list the running PIDs on a Windows machine. It uses the Win32::API module.

#! /usr/bin/perl #use strict; use warnings; use Win32::API; #define some constants my $DWORD_SIZE = 4; my $PROC_ARRAY_SIZE = 300; my $MODULE_LIST_SIZE = 300; #define some Win32 API constants my $PROCESS_QUERY_INFORMATION = 0x0400; my $PROCESS_VM_READ = 0x0010; my $enum_proc = new Win32::API( 'psapi.dll', 'EnumProcesses', [P,N, +P], I ) || die "Cannot Enum Processes: $!\n"; #Create a Buffer Array my $proc_array = &MakeBuffer($DWORD_SIZE * $PROC_ARRAY_SIZE); my $proc_num = &MakeBuffer($DWORD_SIZE); if (0 != $enum_proc->Call($proc_array, $PROC_ARRAY_SIZE, $proc_num)) { $proc_num = unpack ("L", $proc_num) / $DWORD_SIZE; print "\$proc_num = $proc_num\n"; my @pid_lst = unpack("L$proc_num", $proc_array); foreach my $pid(@pid_lst) { print "$pid - Running\n"; } } sub MakeBuffer { my ($buffer_size) = @_; return("\x00" x $buffer_size); }
You will notice that use strict; is commented out. The reason is that when I uncomment use strict; I get the following error,
Bareword "P" not allowed while "strict subs" in use at C:\dev\Source_P +erl\MiscTests\pid\roth.pl line 17. Bareword "N" not allowed while "strict subs" in use at C:\dev\Source_P +erl\MiscTests\pid\roth.pl line 17. Bareword "P" not allowed while "strict subs" in use at C:\dev\Source_P +erl\MiscTests\pid\roth.pl line 17. Bareword "I" not allowed while "strict subs" in use at C:\dev\Source_P +erl\MiscTests\pid\roth.pl line 17. Execution of C:\dev\Source_Perl\MiscTests\pid\roth.pl aborted due to c +ompilation errors.
Any ideas how I can still use strict and line 17 together..? I got most of this code from Dave Roths Win32 Perl Scripting book.

-----
Of all the things I've lost in my life, its my mind I miss the most.

Replies are listed 'Best First'.
Re: use strict and Win32::API
by Corion (Patriarch) on Feb 14, 2003 at 09:38 UTC

    Simply put the characters into quotes, as Perl does not want barewords.

    my $enum_proc = new Win32::API( 'psapi.dll', 'EnumProcesses', [qw[P N +P]], qw[I] ) or die "Cannot Enum Processes: $!\n"; # or my $enum_proc = new Win32::API( 'psapi.dll', 'EnumProcesses', ["P", "N +", "P"], "I" ) or die "Cannot Enum Processes: $!\n"; # or, slightly confusing my $enum_proc = new Win32::API( 'psapi.dll', 'EnumProcesses', [ P => N + => P => ], "I" ) or die "Cannot Enum Processes: $!\n"; # or, slightly perverse use constant P => "P"; use constant N => "N"; use constant I => "I"; my $enum_proc = new Win32::API( 'psapi.dll', 'EnumProcesses', [ P, N, +P ], I ) or die "Cannot Enum Processes: $!\n";

    Of course, the last proposal is seen to be as a joke, because real fun will start as soon as you lazily name a file handle P and try to read/write with it.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

      Of course, the last proposal is seen to be as a joke, because real fun will start as soon as you lazily name a file handle P and try to read/write with it.

      Is that a problem ?? Subs (via use constant) and filehandles have different slots in the symboltable AFAIK.

      Since P, N and I do not need to be interpolated I prefer using ' instead of " with reference to your second alternative.

      Nice post though++

Re: use strict and Win32::API
by Enlil (Parson) on Feb 14, 2003 at 09:41 UTC
    you should be able to just put quotes around those characters without losing anything (and gaining the ability to use strict). that is:
    my $enum_proc = new Win32::API( 'psapi.dll', 'EnumProcesses', [P,N,P +], I ) || die "Cannot Enum Processes: $!\n";
    to
    my $enum_proc = new Win32::API( 'psapi.dll', 'EnumProcesses', ["P"," +N","P"], "I" ) || die "Cannot Enum Processes: $!\n";

    -enlil

Re: use strict and Win32::API
by Cabrion (Friar) on Feb 14, 2003 at 11:32 UTC
    The other answers are better in this case.

    You can also turn strict off in blocks of code using  no strict; too. Just food for thought.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found