Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8027411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:48:14+00:00 2026-06-04T23:48:14+00:00

I am just looking for some advice on best practice. What is the best

  • 0

I am just looking for some advice on best practice. What is the best way to check for strings that are empty or include nothing but space character in them?

I generally use regex like this $string =~/^\s*$/ to see if the variable is blank. Is there a more accepted way or this is as good as any other? Is it better to use regex or to chomp and then check for empty string?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:48:16+00:00Added an answer on June 4, 2026 at 11:48 pm

    Personally, I generally invert the sense, and search for a single non-whitespace character. So I’d say something like:

    next unless $string =~ /\S/;
    

    As ikegami mentioned, \s (and \S) can be a bit idiosyncratic about exactly what characters are considered whitespace, and you can use \p{Whitespace} (or in my case \P{Whitespace}) for more consistent results.

    Out of curiosity, I benchmarked not /\S/ versus /^\s*\z/. On an 80 character string (with Perl 5.14.2), I found not /\S/ was about 20% faster for strings of spaces, and /^\s*\z/ was about 30% faster for strings of non-spaces. It’s unlikely that checking for blank lines is the bottleneck in your program, but it certainly doesn’t hurt to use a simpler regexp.

    use Benchmark 'cmpthese';
    
    our $text = ' ' x 80;
    #our $text = 'abcd' x 20;
    
    cmpthese(-3, {
        'pos'  => '$text =~ /^\s*\z/',
        'neg'  => 'not $text =~ /\S/',
        'negU' => 'not $text =~ /\P{Whitespace}/',
    });
    

    Example results for spaces:

              Rate  pos negU  neg
    pos  3562012/s   --  -2% -15%
    negU 3651619/s   3%   -- -13%
    neg  4194275/s  18%  15%   --
    

    Example results for non-spaces:

              Rate negU  neg  pos
    negU 4951234/s   --  -3% -24%
    neg  5112271/s   3%   -- -22%
    pos  6555251/s  32%  28%   --
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just looking for some advice. What is the best way to present a full
I'm looking for some advice on best practice when writing a web app that
I'm looking for some best practice advice here. I have a library that I'd
Just looking for some advice on the best to approach a profile picture system
I'm looking for some advice/opinions on the best way to approach creating a sort-of-dynamic
I am looking for some advice as to the best way to generate a
Really quick here... I think I have the answer, but just looking for some
Maybe a browser plugin? Just looking for a dirty, quick way to test some
I'm looking for a way to group some ui elements together just like the
I'm looking for some advice on the best approach to synchronising access to properties

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.