Proper way to use an imported constant under 'use strict'?

H

H. Wade Minter

I'm taking time and cleaning up my Perl/Tk application to run under
"use strict", which has been fun and productive, and also revealed some
bad code that I've rewritten. So, cool.

I have a question on one thing, though, and didn't see an answer by
Googling. My code is designed on Linux but also runs on Windows via
checks of $^O. So, to import Windows-specific modules, I do this:

if ( "$^O" eq "MSWin32" )
{
$rcfile = "C:\\mrvoice.cfg";

BEGIN
{
if ( $^O eq "MSWin32" )
{
require LWP::UserAgent;
LWP::UserAgent->import();
require HTTP::Request;
HTTP::Request->import();
require Win32::process;
Win32::process->import();
require Tk::Radiobutton;
Tk::Radiobutton->import();
require Win32::FileOp;
Win32::FileOp->import();
require Audio::WMA;
Audio::WMA->import();
}
}
}

Then, later in the code, I use an imported constant like:

if ( "$^O" eq "MSWin32" )
{

# Start the MP3 player on a Windows system
my $object;
Win32::process::Create( $object, $config{'mp3player'}, '', 1,
NORMAL_PRIORITY_CLASS, "." );
$mp3_pid = $object->GetProcessID();
sleep(1);
}

However, when strict subs are enabled, I get an error about barewords:

[minter@localhost mrvoice]$ ./mrvoice.pl
Bareword "NORMAL_PRIORITY_CLASS" not allowed while "strict subs" in use
at ./mrvoice.pl line 3513.
Execution of ./mrvoice.pl aborted due to compilation errors.

My question is - what's the proper way to use this constant when strictures
are enabled?

Thanks,
Wade
 
R

Richard Morse

H. Wade Minter said:
[minter@localhost mrvoice]$ ./mrvoice.pl
Bareword "NORMAL_PRIORITY_CLASS" not allowed while "strict subs" in use
at ./mrvoice.pl line 3513.
Execution of ./mrvoice.pl aborted due to compilation errors.

My question is - what's the proper way to use this constant when strictures
are enabled?

Completely untested, as I don't right now have access to a windows box,
but perhaps accessing it as Win32::process::NORMAL_PRIORITY_CLASS?

Ricky
 
H

H. Wade Minter

Richard Morse said:
Completely untested, as I don't right now have access to a windows box,
but perhaps accessing it as Win32::process::NORMAL_PRIORITY_CLASS?

Thanks for the suggestion, but it doesn't seem to work:

my $object;
Win32::process::Create( $object, $config{'mp3player'}, '', 1,
Win32::process::NORMAL_PRIORITY_CLASS, "." );
$mp3_pid = $object->GetProcessID();
sleep(1);

[minter@localhost mrvoice]$ ./mrvoice.pl
Bareword "Win32::process::NORMAL_PRIORITY_CLASS" not allowed while "strict
subs" in use at ./mrvoice.pl line 3516.
Execution of ./mrvoice.pl aborted due to compilation errors.

--Wade
 
L

Lukas Mai

H. Wade Minter schrob:
[...]
Then, later in the code, I use an imported constant like:
if ( "$^O" eq "MSWin32" )
{
# Start the MP3 player on a Windows system
my $object;
Win32::process::Create( $object, $config{'mp3player'}, '', 1,
NORMAL_PRIORITY_CLASS, "." );
$mp3_pid = $object->GetProcessID();
sleep(1);
}
However, when strict subs are enabled, I get an error about barewords: [...]
My question is - what's the proper way to use this constant when strictures
are enabled?

Constants are really just inlined subroutines, so writing
NORMAL_PRIORITY_CLASS() should work.

HTH, Lukas
 
A

Ala Qumsieh

Lukas said:
H. Wade Minter schrob:
[...]

Then, later in the code, I use an imported constant like:

if ( "$^O" eq "MSWin32" )
{

# Start the MP3 player on a Windows system
my $object;
Win32::process::Create( $object, $config{'mp3player'}, '', 1,
NORMAL_PRIORITY_CLASS, "." );
$mp3_pid = $object->GetProcessID();
sleep(1);
}

However, when strict subs are enabled, I get an error about barewords:
[...]

My question is - what's the proper way to use this constant when strictures
are enabled?


Constants are really just inlined subroutines, so writing
NORMAL_PRIORITY_CLASS() should work.

I would disable strict within the body of the if() statement:

if ($^O eq 'MSWin32') {
no strict 'subs';
....
}

--Ala
 
K

ko

H. Wade Minter wrote:

[snip]
I have a question on one thing, though, and didn't see an answer by
Googling. My code is designed on Linux but also runs on Windows via
checks of $^O. So, to import Windows-specific modules, I do this:

if ( "$^O" eq "MSWin32" )

[snip code]
However, when strict subs are enabled, I get an error about barewords:

[minter@localhost mrvoice]$ ./mrvoice.pl
--^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------

That doesn't look like a M$ shell, cygwin? It wasn't specified whether
or not the appication runs if 'use strict' is commented out.

If you *are* running under cygwin, none of the modules will have been
included because $^O eq 'cygwin', and you'll get the error that you
quoted below:
Bareword "NORMAL_PRIORITY_CLASS" not allowed while "strict subs" in use
at ./mrvoice.pl line 3513.
Execution of ./mrvoice.pl aborted due to compilation errors.

My question is - what's the proper way to use this constant when strictures
are enabled?

Thanks,
Wade

HTH - keith
 
H

H. Wade Minter

ko said:
if ( "$^O" eq "MSWin32" )

[snip code]
However, when strict subs are enabled, I get an error about barewords:

[minter@localhost mrvoice]$ ./mrvoice.pl
--^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------

That doesn't look like a M$ shell, cygwin? It wasn't specified whether
or not the appication runs if 'use strict' is commented out.

If you *are* running under cygwin, none of the modules will have been
included because $^O eq 'cygwin', and you'll get the error that you
quoted below:

No, I was running that test under Linux, but the strict subs pragma was
catching the constant there, too. The two solutions that worked were
to either disable strict subs in that block or qualify the constant
with NORMAL_PRIORITY_CLASS() parens. It's all good now!

--Wade
 
R

Robin

[minter@localhost mrvoice]$ ./mrvoice.pl
Bareword "NORMAL_PRIORITY_CLASS" not allowed while "strict subs" in >use
at ./mrvoice.pl line 3513.
Execution of ./mrvoice.pl aborted due to compilation errors.
My question is - what's the proper way to use this constant when strictures
are enabled?


I went through this exact same thing myself. All you have to do is define
all your variables with my or local and then call your subs like this(); or
this ("something") instead of like this;
-Robin
 
R

Robin

Scott Bryce said:
Are you aware that your answer has nothing whatsoever to do with the OPs
question?

I've been writing Perl scripts for over a year, and I wouldn't pretend
to know the answer to the OPs question.

now I am...sorry H Wade to confuse.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top