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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:59:38+00:00 2026-06-07T09:59:38+00:00

I have a situation where I expect variables to be passed to me as

  • 0

I have a situation where I expect variables to be passed to me as strings or numbers.

i.e.

sub foo {
    # These can be either strings or numbers
    my ($bar, $var, $star) = @_;

    # I need to check to see if $bar is the number 0 (zero)
    if ($bar == 0) {
        # Do super magic with it
    }
}

Unfortunately Perl tries to do the super magic on $bar when it contains a string.

How can I tell Perl to do super magic on $bar if and only if it is the number 0 (zero)?

I understand Perl fundamentally interprets based on context, which is the underlying problem here. A possible solution to this problem is to use regex, which is fine, but I wanted to know if there was another more “straight-forward” solution.

Thanks in advance.

  • 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-07T09:59:40+00:00Added an answer on June 7, 2026 at 9:59 am

    I’d personally go with what @Disco3’s comment said.

    if ($bar eq 0) { ... }
    

    This works for $bar = 0, $bar = 'foo' and $bar = 123 giving the expected result.

    Here’s a fun fact, though:

    use Benchmark qw(cmpthese);
    my $bar = '0';
    
    cmpthese(-1, {
      'quoted'    => sub { $bar eq '0'    },
      'unquoted'  => sub { $bar eq 0      },
      'regex'     => sub { $bar =~ m/^0$/ },
    });
    

    Benchmarking these three solutions tells us that the unquoted 0 is the fastest way to do it.

                   Rate    regex   quoted unquoted
    regex     4504851/s       --     -70%     -76%
    quoted   15199885/s     237%       --     -19%
    unquoted 18828298/s     318%      24%       --
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have situation where a user can manipulate a large set of data (presented
I have seen that the situation is very problematic after reading these: IE6/IE7 css
I am often in a situation where I have a method where something can
I have a situation where I expect adding to the thread pool to be
i have situation like this: class IData { virtual void get() = 0; virtual
I have situation, where running a query that filters by an indexed column in
I have situation in which I read a record from a database. And if
I have situation where I need to change the order of the columns/adding new
I have situation like this: user submits form with action='/pay' in '/pay' I have
We have situation where say we have four engineers that are working on software

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.