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

Is there any context in which a string containing ONLY whitespace tests false?

Here's the situation:
The HTML output page of a script prints, "Hello, $name!" to greet the visitor.
First the script imports the user's info file that contains their name, among other things, via require.
If the user file does not contain a $name variable, as in the case of first-time visitors, & the output page will say, "Hello, Guest!".
require $user_file_path; $name = $nick_name || "$salutation $last_name"; $name ||= 'Guest';
The problem is in the case that there's no $nick_name, $salutation, or $last_name variables.
So $name gets assigned " " in the second line, and line 3 can't change it to 'Guest' because " " tests true.
The output page ends up reading, "Hello, !"

I know I could use a regexp or any number of other ways to get rid of that pesky whitespace string, but I'm a stubborn guy who doesn't want to add an extra line of code in there.

What I want is a module or something that I can import which will make it so any string containing only whitespace tests false. I've never written a module and I don't even know if what I want is possible to do, so I need some friendly guideance.

Many thanks!

Replies are listed 'Best First'.
Re: Truth & Whitespace
by broquaint (Abbot) on May 02, 2003 at 17:08 UTC
    A module may be overkill when you can just use an operator
    $name = ( defined $nickname ) ? $nickname : ( defined $salutation and defined $last_name ) ? "$saltutation $last_name" : 'Guest';
    See. ?: (trinary conditional operator) in perlop.
    HTH

    _________
    broquaint

      I'd much rather write that as
      $name = defined $nickname ? $nickname : defined $salutation && defined $last_name ? "$salutation $last_name" : 'Guest';
      In my opinion it's much more pretty readable (and pretty) ;)


      MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
      I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
      ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Truth & Whitespace
by chromatic (Archbishop) on May 02, 2003 at 17:18 UTC

    What would happen if I were to enter my name in your system as:

    chromatic"; exit; "
Re: Truth & Whitespace
by artist (Parson) on May 02, 2003 at 17:16 UTC
    but I'm a stubborn guy who doesn't want to add an extra line of code in there.

    Why is that? If that gets your work done, there shouldn't be any problem apart from curosity and finding alternative methods. It would help to seperate the message from the 'name'.

    $salutation = 'Hello'; $name = $nick_name || $last_name || 'Guest'; $msg = "$salutation $name!"; print $msg;

    artist

Re: Truth & Whitespace
by perlplexer (Hermit) on May 02, 2003 at 17:11 UTC
    You would rather load a module instead of adding an extra line of code? Interesting...
    If you just want to keep it to one line though, you could always do this:
    $name = $name=~tr/ //c ? $name : 'Guest';
    --perlplexer
      The reason I would rather load a module is that I already have a couple sites with about 5000 scripts (slight exageration).
      They all require a single .pl file that loads standard code and creates standard variables.
      So loading a module in this standard.pl file would be much easier than searching 5000 scripts for uses of $a ||= $b statements and replacing all that plus an extra line of code.
        I would fix all 5000 scripts. You have a logic flaw and trying to work around it will:
        1. Take at least as long, and probably longer than just fixing it
        2. Will never reduce the number of problems to zero.
        3. Is plain old stubborn when flexible is called for.
        Your problem has to do with the fact that you're not handling the string right. You should be figuring out the name, then adding the salutation. Don't add the salutation until you know the name! Sounds pretty cut'n'dry to me ...

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Truth & Whitespace
by hardburn (Abbot) on May 02, 2003 at 17:09 UTC

    I don't think anything currently exists to do what you want. But you probably could have added that line of code and tested it in the time it took you to write the above post (:

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated