use constant - checking definedness? 
Author Message
 use constant - checking definedness?

Please respon via email (Cc: to the list is fine) because I do/can/will
not read Usenet on any reliably frequent basis... (if anyone knows of good
software to gateway newsgroups into email, please send me a note offline!)

The subject at hand: use constant

I thought I'd like to try using constants instead of variables.

    use constant DEBUG => 1;

has a nice look to it... and Tom Phoenix thinks they're usfeul ;-)

However, several of my functions take advantage of global variables (such
as $DEBUG). To be robust, they need to determine if the variable has, in
fact, been set (since my functions do not blithely trust what goes on
outside and above their domain).

So how do I determine "Has a constant of this name been defined"?

if I have
    use constant FRED => 1;
then
    if (FRED) {
        ...
    }
does what I expect.  Same with
    use constant FRED => 0;  # or undef

but if FRED just simply isn't set at all, it reverts to a (gack) bareword
string and
    if (FRED) {
is successful (violating the principle of least astonishment  :-(

I can turn on "use strict" but then I get an error about bareword strings.

How (if at all) do I determine if the constant is in use?

Or do I just keep using variables for these situations?

--

Journeyman Sourceror           ZZZzz /,`.-'`'    -.  ;-;;,_
Scripts & Philtres                  |,4-  ) )-,_. ,\ (  `'-'
http://www.*-*-*.com/ ~vlb           '---''(_/--'  `-'\_)



Sun, 22 Jul 2001 03:00:00 GMT  
 use constant - checking definedness?
[cc'ed to author]

Quote:

> So how do I determine "Has a constant of this name been defined"?

if (defined &CONSTANT) {
 ...
Quote:
}



Sun, 22 Jul 2001 03:00:00 GMT  
 use constant - checking definedness?
[posted & mailed]

: Please respon via email (Cc: to the list is fine) because I do/can/will
: not read Usenet on any reliably frequent basis...

        Huh?  Not even to check back on the threads/questions you start?

: (if anyone knows of good software to gateway newsgroups into email, please
: send me a note offline!)

        Followups: poster

: The subject at hand: use constant
: I thought I'd like to try using constants instead of variables.
:
:     use constant DEBUG => 1;
:
: has a nice look to it... and Tom Phoenix thinks they're usfeul ;-)

        Even if they do pull in like a thousand lines of code or so with
        just that one line. :-)

: However, several of my functions take advantage of global variables (such
: as $DEBUG). To be robust, they need to determine if the variable has, in
: fact, been set (since my functions do not blithely trust what goes on
: outside and above their domain).

        That's not likely the best policy.  If your calling spec defines the
        use of globals, then not having them is an error, just as if you
        forgot to pass a required parameter.

: So how do I determine "Has a constant of this name been defined"?

        Black magic:

        if (defined *DEBUG{CODE})

        But don't do that.

: if I have
:     use constant FRED => 1;
: then
:     if (FRED) {
:         ...
:     }
: does what I expect.  Same with
:     use constant FRED => 0;  # or undef
:
: but if FRED just simply isn't set at all, it reverts to a (gack) bareword
: string and
:     if (FRED) {
: is successful (violating the principle of least astonishment  :-(

        Use strict.  FRED should never not be defined in such usage, ever.

: I can turn on "use strict" but then I get an error about bareword strings.

        Yep.  Most consider this a Good Thing.  If FRED is a constant and
        you don't have it at all, your code is in error and should not run.

        If you don't define FRED in C, what do you think the compiler will
        do with this:

        if (FRED) { }

: How (if at all) do I determine if the constant is in use?

        See above, but don't do that.

: Or do I just keep using variables for these situations?

        Either way.  If you are using a constant/global, there is no excuse
        for it not existing if your API calls for it.  That is an error, and
        one that use strict catches at compile time before your program
        blows chunks.

        Use strict and -w on all programs at all times, even quick example
        code.  Does anyone compile C without -Wall?

--

BSD:  A psychoactive drug, popular in the 80s, probably developed at UC
Berkeley or thereabouts.  Similar in many ways to the prescription-only
medication called "System V", but infinitely more useful. (Or, at least,
more fun.)  The full chemical name is "Berkeley Standard Distribution".



Mon, 23 Jul 2001 03:00:00 GMT  
 use constant - checking definedness?

[Mailed and Posted]

Quote:
> So how do I determine "Has a constant of this name been defined"?

This:

        if (defined &FRED) { ... }

will work.  But perhaps you don't like it?



Mon, 23 Jul 2001 03:00:00 GMT  
 use constant - checking definedness?
1999-02-03-22:16:39 Vicki Brown:

Quote:
>I can turn on "use strict" but then I get an error about bareword strings.

I would urgently recommend fixing any errors reported about bareword strings
under "use strict", and running with that at all times. In fact I'd recommend
that all perl programs of any size for any purpose begin with

        #!/where/ever/perl -w
        use strict;

Perl has terrific features for helping protect you from typos that can be hard
to debug; use them at all times.

-Bennett



Mon, 23 Jul 2001 03:00:00 GMT  
 use constant - checking definedness?


Quote:

> : Please respon via email (Cc: to the list is fine) because I do/can/will
> : not read Usenet on any reliably frequent basis...

>  Huh?  Not even to check back on the threads/questions you start?

Nope.  Too much to do.  Usenet requires me to go to it; it will not come
to me. I have little time and less patience for that model.

Under normal circumstances, I wouldn't be back here now, but since this
was posted to the list (and I think it needs a more public response) I am
here.  Usenet _wastes my time_.  If I could have replied to this list via
email, I would have. (If I can post to this list via email, please send
instructions)

Quote:
> : (if anyone knows of good software to gateway newsgroups into email, please
> : send me a note offline!)

>  Followups: poster

No. I want "software to gateway _newsgroups_ into email."  Not to simply
force individual replies to specific messages into email.  I want to Never
Have To Access News Directly Again.

Back to the real topic at hand.

Quote:
>  That's not likely the best policy.  If your calling spec defines the
>  use of globals, then not having them is an error, just as if you
>  forgot to pass a required parameter.

Of course it's an error.  That was the point of my message. I want to
check for the error and handle it.

I hate to say this, but your answer gives the impression that you haven't
had much experience at writing bulletproofed code.  I need to test whether
my environment is as I expect it to be and Do The Right Thing if my
environment are not as desired.

Simply allowing my program to die (or worse, try to run) with a bad
environment, bad parameters, or bad data... is bad coding style.

(Case in point: we've got a program here at work that blithely assumes all
of its input parameters are as they should be.  It jumps to the data
and... seg fault! ... Wrong answer.)

Quote:

>  Use strict.  FRED should never not be defined in such usage, ever.

Again, you miss my point. See next section...

Quote:

> : I can turn on "use strict" but then I get an error about bareword strings.

>  Yep.  Most consider this a Good Thing.  If FRED is a constant and
>  you don't have it at all, your code is in error and should not run.

You have missed my point.  I understand how strict works. I don't want the
program to bomb.   I want to ask if the constant is defined, get an
answer, and act on the answer.

Imagine for a moment a world without the defined() function.  Your only
choice if you want to know if something is defined is to use -w and see if
the program succeeds or dies.

Wrong model.  Not perlish.

Quote:

>  If you don't define FRED in C, what do you think the compiler will
>  do with this:

Um.  We are discussing Perl (at least, I was :-)  C has nothing to do with it.

Quote:
> : How (if at all) do I determine if the constant is in use?

>  See above, but don't do that.

Above... where?

Quote:

> : Or do I just keep using variables for these situations?

>  Either way.  If you are using a constant/global, there is no excuse
>  for it not existing if your API calls for it.

The excuse is that users make mistakes.  The person writing the code that
calls my function will not always be me. Well-designed functions
(well-designed _code_) checks for misuse of the API. End of story.

Quote:
> That is an error, and
>  one that use strict catches at compile time before your program
>  blows chunks.

I'd rather catch it politely and handle it gracefully.  Some people
(myself included) consider
  Bareword "DEBUG" not allowed while "strict subs" in use at foo.pl line 7.
  Execution of foo.pl aborted due to compilation errors.
to, er "blow chunks" :-)

Besides, who are you to second guess my design?  Perhaps if the constant
is not ddefined, my function should do something other than crash and
burn?

- Vicki

--

Journeyman Sourceror           ZZZzz /,`.-'`'    -.  ;-;;,_
Scripts & Philtres                  |,4-  ) )-,_. ,\ (  `'-'
http://www.cfcl.com/~vlb           '---''(_/--'  `-'\_)



Mon, 23 Jul 2001 03:00:00 GMT  
 use constant - checking definedness?
[ Moderators Note: Discussions of mail/news gateways etc. are not in
  the charter of comp.language.perl.moderated.  Followups in this
  thread will only be approved if they remain on-topic, i.e about Perl. ]

[posted & mailed]


        >snip<
: Usenet _wastes my time_.
        >snip<

        If I responded to this, I'd have to mark folloups to alt.flame, and
        down that path lies madness. :-)

: If I could have replied to this list via email, I would have. (If I can
: post to this list via email, please send instructions)

        FYI, news<->email gateways have existed for some time.  5 minutes on
        your favorite search engine should find out all about them.

        >snip<
: No. I want "software to gateway _newsgroups_ into email."  Not to simply
: force individual replies to specific messages into email.  I want to Never
: Have To Access News Directly Again.

        Then look into gateways, or don't ask for help in Usenet.

: Back to the real topic at hand.

        Thank god... :-)

: >  That's not likely the best policy.  If your calling spec defines the
: >  use of globals, then not having them is an error, just as if you
: >  forgot to pass a required parameter.
:
: Of course it's an error.  That was the point of my message. I want to
: check for the error and handle it.

        Why bother when the compiler already will do this for you?

: I hate to say this, but your answer gives the impression that you haven't
: had much experience at writing bulletproofed code.

        You make completely unfounded accusations.

: I need to test whether my environment is as I expect it to be and Do The
: Right Thing if my environment are not as desired.

        From your examples, "Do The Right Thing" should likely be to simply
        refuse to run at all and spew out an error message.  This is exactly
        what use strict and the compiler do for you.  This is a code level
        error and should be handed in a "bulletproof" manor by the one
        handling the code, aka the compiler.

: Simply allowing my program to die (or worse, try to run) with a bad
: environment, bad parameters, or bad data... is bad coding style.

        Agreed.  However, if you don't use strict and -w, then you sir are
        not writing "bulletproof code".

        Under this system, your program will neither "die" or "try to run"
        as perl would never let it get that far in the first place.  How
        much more bulletproof could you get?

        >snip<
: >  Use strict.  FRED should never not be defined in such usage, ever.
:
: Again, you miss my point. See next section...
:
: > : I can turn on "use strict" but then I get an error about bareword strings.
: >
: >  Yep.  Most consider this a Good Thing.  If FRED is a constant and
: >  you don't have it at all, your code is in error and should not run.
:
: You have missed my point.  I understand how strict works. I don't want the
: program to bomb.

        Strict sets compile time boundaries that will simply *prevent* your
        program from "bombing" as it will never even start in the first
        place.

: I want to ask if the constant is defined, get an answer, and act on the
: answer.

        In other words, move the check of a coding bug from a compile time
        error where it should be, to run time.  Not only that, you don't
        want perl to automatically do it for you, you want to hope and pray
        that you manually found every spot this could be a problem and
        manually make the coding tests yourself.

        Are you trying writing another compiler...?

: Imagine for a moment a world without the defined() function.  Your only
: choice if you want to know if something is defined is to use -w and see if
: the program succeeds or dies.
:
: Wrong model.  Not perlish.

        Now you're missing the point.  Under strict, the program will never
        "succeeds or die" as it would never have been allowed to start
        running in the first place.

        Do I need to explain the difference between compile time and run
        time error conditions?  Between compiler and user generated
        exceptions?

: >  If you don't define FRED in C, what do you think the compiler will
: >  do with this:
:
: Um.  We are discussing Perl (at least, I was :-)  C has nothing to do with it.

        We're not really talking Perl specifics here, but general language
        independent coding practices.

: > : How (if at all) do I determine if the constant is in use?
: >
: >  See above, but don't do that.
:
: Above... where?

        if (defined &CONSTANT) {

        or

        if (defined *CONSTANT{CODE}) {
        ## Hmm, would exists() work too I wonder?

        >snip<
: > : Or do I just keep using variables for these situations?
: >
: >  Either way.  If you are using a constant/global, there is no excuse
: >  for it not existing if your API calls for it.
:
: The excuse is that users make mistakes.

        Of course, but a compile time exception isn't good enough to catch
        them?!

: The person writing the code that calls my function will not always be me.
: Well-designed functions (well-designed _code_) checks for misuse of the
: API. End of story.

        But if the compiler can enforce this itself, all the better, no?

: > That is an error, and
: >  one that use strict catches at compile time before your program
: >  blows chunks.
:
: I'd rather catch it politely and handle it gracefully.  Some people
: (myself included) consider
:   Bareword "DEBUG" not allowed while "strict subs" in use at foo.pl line 7.
:   Execution of foo.pl aborted due to compilation errors.
: to, er "blow chunks" :-)

        Is it not better then:

        "Sorry, but at some random part of your code you misused my API.  I
         am now sorry to inform you that I will be handing this error
         condition in a manor that nither you expect, or that is commonly
         used by anyone else anywere.  Furthermore, I forget to include in
         this error message exactly what went wrong, what I plan to do about
         it, what you could do to prevent it in the future, or even for that
         matter where the error even was in your code.  Thank you for your
         time, and have a nice day."

: Besides, who are you to second guess my design?  Perhaps if the constant
: is not ddefined, my function should do something other than crash and
: burn?

        Hmm, then you likely aren't writing "bulletproof code".

--

BSD:  A psychoactive drug, popular in the 80s, probably developed at UC
Berkeley or thereabouts.  Similar in many ways to the prescription-only
medication called "System V", but infinitely more useful. (Or, at least,
more fun.)  The full chemical name is "Berkeley Standard Distribution".



Tue, 24 Jul 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Short circuit predicates based on existence or definedness, not truth

2. Short circuit predicates based on existence or definedness, not truth

3. Perl constants like C constants?

4. Seeking advice on semantics (pros / cons and best ways of) using constants across modules

5. easy question: using constants in perl

6. Using TRUE constant in IF expression??

7. Help: using constants from inherited parent class

8. Be careful about using constants

9. using variable content as constant name

10. Defining constant values in Perl using #DEFINE

11. Checking for open files using fcntl: flock in Solaris

12. Problem checking if a command line option has been specified using Getopt::Long

 

 
Powered by phpBB® Forum Software