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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:16:34+00:00 2026-06-01T00:16:34+00:00

Is it possible to in some way indicate if an array in Perl is

  • 0

Is it possible to in some way indicate if an array in Perl is undefined or null?

I find myself coming across situations where I would like to be able to differentiate between an empty array and one that hasn’t been populated yet (or failed to populate for some reason).

So for instance, with an array reference I can do this:

my $apples;
$apples = get_apples();

if(defined $apples){

    if(scalar @$apples == 0){
        # We successfully got the list of apples, but there were none
    }

    }
    else{

        # There was a problem getting the list of apples

}

My only complaint about this is that "my $apples" doesn’t tell you that $apples is intended to be an array reference, so @apples would be more specific.

It doesn’t look there is a way to do something with an array explicitly. Is that the case? Will another variable always be required to indicate if the array was successfully populated?

The following could never be tested for a successful return of apples, right? Or am I missing something neat?

my @apples;
(@apples) = get_apples();

I know that get_apples could both return a success and a list to populate the array with, but I’m curious if there is a way to indicate a null or undefined value with just an array.

  • 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-01T00:16:35+00:00Added an answer on June 1, 2026 at 12:16 am

    In Perl there is no difference between an empty array and an uninitialized array.

    $ perl -MDevel::Peek -e 'print Dump(\@a)'
    SV = RV(0x20033b00) at 0x20033af0
      REFCNT = 1
      FLAGS = (TEMP,ROK)
      RV = 0x20091830
      SV = PVAV(0x200350c0) at 0x20091830
        REFCNT = 2
        FLAGS = ()
        ARRAY = 0x0
        FILL = -1
        MAX = -1
        ARYLEN = 0x0
        FLAGS = (REAL)
    
    $ perl -MDevel::Peek -e '@a=(); print Dump(\@a)'
    SV = RV(0x20033b00) at 0x20033af0
      REFCNT = 1
      FLAGS = (TEMP,ROK)
      RV = 0x20091818
      SV = PVAV(0x200350c0) at 0x20091818
        REFCNT = 2
        FLAGS = ()
        ARRAY = 0x0
        FILL = -1
        MAX = -1
        ARYLEN = 0x0
        FLAGS = (REAL)
    

    Your only hope may be to inspect the MAX attribute of the internal AV object to see whether an array used to contain any data:

    use B;
    @b = ();
    @c = (1..100); @c = ();
    print B::svref_2object(\@b)->MAX;      # -1
    print B::svref_2object(\@c)->MAX;      # 99
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to handle this event in some way? What happens in terms
I don't think this is possible, but I'll ask anyway... Is there some way
Possible Duplicate: Best way to stop SQL Injection in PHP I have seen some
Folks, I really like the -Wshadow option since it helps to spot some possible
I would like to put JDK tools.jar as compile dependency. I found some examples
I would like to indicate the currently chosen page in a shared menu component
I'm trying find a way to have PHP to indicate to the browser that
I would like to build a website that has some elements of a social
Is it possible to in some way tell the maven deploy:file goal to deploy
Is it possible make some handler that will do something when user shutdown computer

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.