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

Discussion in 'Perl Misc' started by H. Wade Minter, Apr 23, 2004.

  1. 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
     
    H. Wade Minter, Apr 23, 2004
    #1
    1. Advertising

  2. In article <pPbic.34812$>,
    "H. Wade Minter" <> wrote:

    > [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

    --
    Pukku
     
    Richard Morse, Apr 23, 2004
    #2
    1. Advertising

  3. Richard Morse <> wrote:

    > 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
     
    H. Wade Minter, Apr 23, 2004
    #3
  4. H. Wade Minter

    Lukas Mai Guest

    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
    --
    use warnings; use strict;
    sub hacker'Perl { "Perl @_,"} sub another'Just
    {print"Just @_ "}
    Just another Perl hacker,
     
    Lukas Mai, Apr 23, 2004
    #4
  5. H. Wade Minter

    Ala Qumsieh Guest

    Lukas Mai wrote:

    > 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
     
    Ala Qumsieh, Apr 24, 2004
    #5
  6. H. Wade Minter

    ko Guest

    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
     
    ko, Apr 24, 2004
    #6
  7. ko <> wrote:
    >> 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
     
    H. Wade Minter, Apr 24, 2004
    #7
  8. H. Wade Minter

    Robin Guest

    >[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
     
    Robin, Apr 25, 2004
    #8
  9. H. Wade Minter

    Robin Guest

    "Scott Bryce" <> wrote in message
    news:...
    > Robin wrote:
    >
    > > 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;

    >
    > 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.

    --
    Regards,
    -Robin
    --
    [ webmaster @ infusedlight.net ]
    www.infusedlight.net
     
    Robin, Apr 25, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gabriel Rossetti
    Replies:
    2
    Views:
    256
    Gabriel Rossetti
    May 1, 2009
  2. Dun Peal
    Replies:
    10
    Views:
    473
    Chris Rebert
    May 3, 2011
  3. Volker Nicolai
    Replies:
    9
    Views:
    1,123
    Fabian Pilkowski
    Jul 4, 2005
  4. Victor Porton
    Replies:
    7
    Views:
    116
    TonyV
    Dec 11, 2007
  5. bernd
    Replies:
    2
    Views:
    689
    bernd
    Jan 26, 2012
Loading...

Share This Page