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

I can't think of a better title (please feel free to suggest mods). This question is about both logic and style.

I have spent the past few days working with Win32::Daemon. I have been using code (from the docs and examples provided) that looks like so

while( SERVICE_STOPPED != ( $State = Win32::Daemon::State() ) ){ if( SERVICE_STARTING == $State ) {
Q1. What is this use of barewords? I almost feel compelled to single quote all the words in UPPERCASE above. Should this not be 'SERVICE_STOPPED' etc.? And won't this fail under use strict?

Q2. Are the UPPERCASE words above just proxy for numbers? For example, in the situation above, the State() returned is actually a number. Of course, having the -w switch on complains that

Argument "SERVICE_STARTING" isn't numeric in numeric eq (==) at ...

Q3. (this one is the style-related question) I feel the urge to reverse the above tests as in

if( $State == SERVICE_STARTING )
since I am used to testing the value that a variable holds against certain "measures" that are fixed as in
if ($color eq 'red') { print 'angry'; } elsif ($color eq 'green') { print 'money'; } elsif ($color eq 'blue') { print 'sad'; }

I guess this is new idiom for me, and I need to understand what is going on here.

Replies are listed 'Best First'.
Re: Barewords and equality tests
by davido (Cardinal) on Dec 29, 2004 at 22:44 UTC

    SERVICE_STOPPED and SERVICE_STARTING may be constants:

    use strict; use warnings; use constant SERVICE_STARTING => 1; use constant SERVICE_STOPPED => 0;

    Constants in Perl are just a sort of subroutine with special optimizations. Thus, they follow subroutine syntax rules. We don't get concerned when we see 'shift' as a bare word.


    Dave

      Thanks (to all). Time for me to read use constant and use vars, some of the many Perlthings that I have not yet used in my past 3-4 years experience with Perl.

      However...

      We don't get concerned when we see 'shift' as a bare word.
      I guess I didn't get bothered by that because shift is a core function in Perl, and it is used on an array. Only when the array is a special variable (@_) we can eliminate it and just type shift() or even shift. Personally, I like using all the parens and spell things out wherever possible. Makes code easy for me.

      Thanks for the clarifications.

Re: Barewords and equality tests
by bart (Canon) on Dec 29, 2004 at 22:46 UTC
    SERVICE_STOPPED and the others are constants, defined and exported by Win32::Daemon. Not barewords, but functions each returning a constant, numerical value.

    Just try the code snippet:

    use Win32::Daemon; print SERVICE_STOPPED;

    Because SERVICE_STOPPED is a function exported by Win32::Daemon, Perl will not think of it as a file handle, but instead print its value: 1.

Re: Barewords and equality tests (())
by tye (Sage) on Dec 30, 2004 at 01:36 UTC

    Write those instead with trailing parens, SERVICE_STOPPED(), and they look like what they are (constants implemented as function calls) instead of barewords.

    - tye        

Re: Barewords and equality tests
by dave_the_m (Monsignor) on Dec 29, 2004 at 22:44 UTC
    Q1. What is this use of barewords? I almost feel compelled to single quote all the words in UPPERCASE above. Should this not be 'SERVICE_STOPPED' etc.? And won't this fail under use strict?
    It's Perl's rather clumsy attempt to allow constants. SERVICE_STOPPED is actually a zero-argument function that returns a value, and which the perl interpreter optimises away at compile-time. See also 'use constant'.

    Q2. Are the UPPERCASE words above just proxy for numbers?
    Dunno. That depends on the particular module that defines the constant.

    Q3. (this one is the style-related question) I feel the urge to reverse the above tests as in
    if( $State == SERVICE_STARTING )
    Personally I prefer this too.

    Dave.

      But see the meditation "Avoiding the == Blues" (418058) for a different take on bare-words-on-the-right....

      ----
      I Go Back to Sleep, Now.

      OGB