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

  • Home
  • SEARCH
  • 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 3225356
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:19:47+00:00 2026-05-17T16:19:47+00:00

I am testing a function that returns an array. The array could be different

  • 0

I am testing a function that returns an array. The array could be different depending on the environment but it will always have at least one constant value (the one that I want to test).

As I am using Perl 5.12, I can use the smartmatch operator to find if the element is in the array:

ok($known_value ~~ @returned, 'testing method abc')

But I like the enhanced output of is and like with the “found” and “expected” parts. So I tried this:

cmp_ok($known_value, '~~', @returned, 'testing method abc')

This does not work because it seems that cmp_ok expects a scalar in both parts of the comparison:

not ok 1 - testing method abc
#   Failed test 'testing method abc'
#   at abc.t line 53.
#     'stable_value'
#         ~~
#     '2'

The array in the “expected” slot is evaluated in scalar context and converted to 2.

I can work around this with a hack using like and stringifying the array, but having a test where you can use the smartmatch operator as a comparison method (like when) would be nice. Is there a way to do this with Test::More or some other module?

At the moment I am using:

ok($known_value ~~ @returned, 'testing method abc')
  or diag (
      "ERROR:\n".
      "Found: ". Dumper @returned."\n".
      "Expected at least one element equal to '$known_value'"
  )

Is this the best that I can do?

  • 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-17T16:19:48+00:00Added an answer on May 17, 2026 at 4:19 pm

    You can’t use @returned because of how Perl passes arguments to subroutines. (Arrays are flattened into the argument list and lose their identity.) Pass an array reference instead:

    cmp_ok($known_value, '~~', \@returned, 'testing method abc')
    

    The smart match operator is smart enough to do the right thing. From perlsyn:

    Note that the smart match implicitly
    dereferences any non-blessed hash or
    array ref, so the “Hash” and “Array”
    entries apply in those cases.

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

Sidebar

Related Questions

I'm testing the VB function below that I got from a Google search. I
I am testing a function called extractions that operates over any list. extractions ::
For testing purposes I have to generate a file of a certain size (to
Unit testing sounds great to me, but I'm not sure I should spend any
for testing purposes i need an recursive directory with some files, that comes to
I have the following code where I declare a PHP array variable and inside
I have a HTML page that contains a flash file and some a JavaScript
I have a weird condition that only appears on gcc -O0 -g output, not
Testing: return request.getCookies() == null; is not an appropriate way test. Is there another
Unit testing and ASP.NET web applications are an ambiguous point in my group. More

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.