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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:37:57+00:00 2026-05-14T04:37:57+00:00

Let’s say I have a subroutine/method that a user can call to test some

  • 0

Let’s say I have a subroutine/method that a user can call to test some data that (as an example) might look like this:

sub test_output {
    my ($self, $test) = @_;
    my $output = $self->long_process_to_get_data();
    if ($output =~ /\Q$test/) {
        $self->assert_something();
    }
    else {
        $self->do_something_else();
    }
}

Normally, $test is a string, which we’re looking for anywhere in the output. This was an interface put together to make calling it very easy. However, we’ve found that sometimes, a straight string is problematic – for example, a large, possibly varying number of spaces…a pattern, if you will. Thus, I’d like to let them pass in a regex as an option. I could just do:

$output =~ $test

if I could assume that it’s always a regex, but ah, but the backwards compatibility! If they pass in a string, it still needs to test it like a raw string.

So in that case, I’ll need to test to see if $test is a regex. Is there any good facility for detecting whether or not a scalar has a compiled regex in it?

  • 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-14T04:37:57+00:00Added an answer on May 14, 2026 at 4:37 am

    As hobbs points out, if you’re sure that you’ll be on 5.10 or later, you can use the built-in check:

     use 5.010;
     use re qw(is_regexp);
     if (is_regexp($pattern)) {
         say "It's a regex";
     } else {
         say "Not a regex";
     }
    

    However, I don’t always have that option. In general, I do this by checking against a prototype value with ref:

     if( ref $scalar eq ref qr// ) { ... }
    

    One of the reasons I started doing it this way was that I could never remember the type name for a regex reference. I can’t even remember it now. It’s not uppercase like the rest of them, either, because it’s really one of the packages implemented in the perl source code (in regcomp.c if you care to see it).

    If you have to do that a lot, you can make that prototype value a constant using your favorite constant creator:

     use constant REGEX_TYPE => ref qr//;
    

    I talk about this at length in Effective Perl Programming as “Item 59: Compare values to prototypes”.

    If you want to try it both ways, you can use a version check on perl:

     if( $] < 5.010 ) { warn "upgrade now!\n"; ... do it my way ... }
     else             { ... use is_regex ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say I have a method in java, which looks up a user in
Let's say I can call a method like this: core::get() . What is the
Let's say you have a method that expects a numerical value as an argument.
Let's say I have a table with a Color column. Color can have various
Let's say that I have a SQLite database that I create in a separate
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have an facebook application running using the JS SDK. First user
Let's say I have multiple requirements for a password. The first is that the
Let's assume that a user votes for some movies in a scale of 1

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.