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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:16:49+00:00 2026-06-15T15:16:49+00:00

Given Example Code: foo(bar=>test); foo(bar=>[test]); sub foo { my $args = {@_}; say ref($args->{bar});

  • 0

Given Example Code:

foo(bar=>"test");
foo(bar=>["test"]);

sub foo {
   my $args = {@_};

   say ref($args->{bar});
   say ref(\$args->{bar});
}

Outputs:

{expected blank}
SCALAR
ARRAY
REF


What I would like to test for is the best way to check if what is passed is a scalar or an array. Something like:

given( ref($args->{bar}) ){
   when "SCALAR" { }
   when "ARRAY"  { }
}

I could concatenate the two ref types and do a regex-when, but that’s inefficient. I could also test it like the following, but not sure if that’s preferred:

if    ( ref(\$args->{bar}) eq "SCALAR" ) { ... }
elsif ( ref( $args->{bar}) eq "ARRAY"  ) { ... }
else  { return; }
  • 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-15T15:16:50+00:00Added an answer on June 15, 2026 at 3:16 pm

    You’re not trying to differentiate between a scalar and an array. You get a scalar in both cases. You’re trying to differentiate between a non-reference and a reference to an array.

    if (!ref($x) || ref($x) eq 'ARRAY') {
       # Non-ref or ref to array.
       ...
    }
    

    or

    if (!ref($x)) {
       # Non-ref
       ...
    }
    elsif (ref($x) eq 'ARRAY') {
       # Ref to array.
       ...
    }
    

    or

    for (ref($x)) {
       if (!$_) {
          # Non-ref
          ...
       }
       elsif ($_ eq 'ARRAY') {
          # Ref to array.
          ...
       }
    }
    

    or

    my $ref_type = ref($x);
    if (!$ref_type) {
       # Non-ref
       ...
    }
    elsif ($ref_type eq 'ARRAY') {
       # Ref to array.
       ...
    }
    

    or (assuming those are the only two types of values allowed)

    if (ref($x)) {
       # Ref to array.
       ...
    } else {
       # Non-ref
       ...
    }
    

    (Note that Scalar::Util’s reftype actually gets the ref type. ref can return a class name instead of a reference type.)

    Note that differentiating values based on storage type is a poor design in Perl. It necessarily buggy, since it breaks overloaded objects.

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

Sidebar

Related Questions

For example, my goal is to test the code given here: PHP script that
Given the following example: public class Foo { public void myMethod(Bar arg) { SomeOtherClass.baz(arg);
Let's say I have an arbitrary array of keys: $keys = array('foo', 'bar', 'baaz');
Given the example source code below, is it possible for someone to see the
Given the next code example, I'm unable to free the parameter const char* expression
This is an example of my code: var bar = function() { this.baz =
Given the following example classes in my.package ... public class Foo { public void
Consider an object chain like: foo.bar.baz.bing In this particular instance, I am given foo
I have a simple model object with one array property. Example: {name: 'Foo', tags
Consider the following Java code: public class Test { private Foo< String, String >

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.