Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

undef vs 0

by skx (Parson)
on Feb 06, 2003 at 09:38 UTC ( [id://233093]=perlquestion: print w/replies, xml ) Need Help??

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

 Hello Monks,

 I've got a simple problem with a configuration file parser/settings object.

 I've written a simple module which allows access to configuration values, and I find that I'm not able to differentiate between a value that is unset, and a value that is zero easily.

 Consider the following:

my $value = getConfigValue( "use_foo" ) || 1;

 What I want to do is to say read the value, and if it's not defined use '1' as a default. What actually happens is that if the value is set to '0' it gets ignored.

 I see the following as a solution:

my $value = getConfigValue( "use_foo" ); if ( !defined( $value ) ) { $value = 1; }

 but that seems a little messy.

 Short of re-writing the code to be 'getConfigValue( "keyname", "default" )' which would be a pain is there a simple solution?

Steve
---
steve.org.uk

Replies are listed 'Best First'.
Re: undef vs 0
by robartes (Priest) on Feb 06, 2003 at 09:49 UTC
    Use the ?: operator:
    my $value=defined(getConfigValue("use_foo""))?getConfigValue("use_foo" +):1;
    But your solution does basically the same and is not messy IMO. You could rewrite it as such:
    my $value = getConfigValue( "use_foo"); $value=1 unless defined $value;
    Which looks a bit better (and is more Perlish).

    Please not that code provided is untested.

    CU
    Robartes-

Re: undef vs 0
by PodMaster (Abbot) on Feb 06, 2003 at 10:00 UTC
      The real question is why write such a module when there are tons of them?

       A valid question; basically because this is a perl re-implementation of some C code. The config file parser has to be 100% compatible with the old codes behaviour.

       Also it should be possible for the settings in the configuration file to be overridden by the contents of environmental variables, which no existing code I saw supported.

       (The environmental variable overrides is necessary for my testing scripts/testing system - fully automatic regression testing of the project as a whole, and the modules it is implmented via/with).

      Steve
      ---
      steve.org.uk
Re: undef vs 0
by bart (Canon) on Feb 06, 2003 at 11:51 UTC
    That's why perl6 will have the '//' operator. Heck, it could even be that one of the future perl5 versions will have it... Urm... no, perl5.8.0 doesn't have it, yet. You can search through the P5P archives to see any discussions, for example in this message.

    Anyway, a contrived solution could be:

    my($value) = map { defined() ? $_ : 1 } getConfigValue( "use_foo" );
    Perhaps a less vicious example:
    my $value = do { defined(local $_ = getConfigValue( "use_foo" )) ? $_ +: 1 };
    You could always write a sub I suppose — perhaps in an external module, if you use it a lot?
    sub default { my $v = shift; return defined $v ? $v : shift; } my $value = default(getConfigValue( "use_foo" ), 1);
Re: undef vs 0
by ehdonhon (Curate) on Feb 06, 2003 at 11:30 UTC

    In perl6, this will be easy:
    my $value = getConfigValue( "use_foo" ) // 1;

    In perl5, I think robartes has the right idea. Just to expand on it though, suppose you only want to call getConfigValue once for each value. You could do this:
    my $value = defined($_ = getConfigValue( "use_foo" )) ? $_ : 1;

Re: undef vs 0
by pfaut (Priest) on Feb 06, 2003 at 15:54 UTC

    If you wrote the module, why not add a second argument to the getConfigValue() function to act as a default value? Then the check for defined is hidden inside the module and your calling code is as simple as my $value = getConfigValue( "use_foo", 1 );.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

       I think that would probably be the most straightfoward solution.

      Steve
      ---
      steve.org.uk
Re: undef vs 0
by TheDamian (Vicar) on Feb 07, 2003 at 17:17 UTC
    Yet another solution:
    my ($value) = grep defined, getConfigValue( "use_foo" ), 1;
Re: undef vs 0
by PureSignal (Initiate) on Feb 06, 2003 at 18:30 UTC
    this is about the same I guess:

    my $value;
    defined ($value = getConfigValue( "use_foo" )) or $value = 1;
    
Re: undef vs 0
by thinc (Initiate) on Feb 07, 2003 at 06:48 UTC
    try this:
    my $value = getConfigValue( "use_foo" ) ||= 1;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-03-29 15:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found