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 921657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:54:09+00:00 2026-05-15T18:54:09+00:00

Is it safe/good practice in Perl to use an empty string as false in

  • 0

Is it safe/good practice in Perl to use an empty string as false in boolean comparisons?

ex:

my $s = '';

if($s) {
 print 'true';
} else {
 print 'false';
}

or would the length function be a better way to go:

my $s = '';

if(length($s) > 0) {
 print 'true';
} else {
 print 'false';
}
  • 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-05-15T18:54:09+00:00Added an answer on May 15, 2026 at 6:54 pm

    When testing for true/false in an expression, you should think carefully about what you actually want to test.

    If you have a string and you need to check if it has any value at all, then test for that:

    if (defined $string) { ... }
    

    If you are passing back a “boolean” value from a function, or testing the result of some internal operation, then perform a boolean check:

    if ($value) { ... }
    

    In Perl, “truth” depends on the manner in which it is evaluated. If you simply say if ($value), then the only string values that evaluate to false are the empty string '', and a literal zero '0'. (Also, any undefined value is false, so if $value has never been assigned, that would be false too.)

    There are other values which may evaluate to false in a numerical context, for example 0E0, 0.0000, and the string '0 but true'. So the question “what is truth” really depends on the context, which you the programmer need to be aware of.

    Internally, Perl uses '' for false and 1 for truth (for example as the result of any boolean expression such as !$value). A common technique for “canonicalizing” any value into one of these two values is !!$value — it inverts a value twice, giving you either '' or 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be a good way to writein C# a .NET3.5 Thread Safe DataSource
I wanted to know if this was thread safe/ good practice. My IOC is
For many reasons, it is not good practice to use threads inside a servlet.
Is it a good practice/safe to temporarily store the HttpServletRequest and the HttpServletResponse as
I'm using WinApi. Is SendMessage/PostMessage a good, thread safe method of communicating with the
How safe is it to use Silverlight in production for a graphic form? Is
Is the following code safe? boost::any any_value; { std::string s = HelloWorld; any_value =
What is a good practice to save username and password on device? I have
I just want to know is it safe/ good approach to call return inside
Does anyone know a good safe way to redirect the output of a printf-style

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.