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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:23:24+00:00 2026-05-30T22:23:24+00:00

Is using my %data; below okay or should I switch to my $data={}; ?

  • 0

Is using my %data; below okay or should I switch to my $data={}; ?

#!/usr/bin/perl -wT
use strict;
use JSON;

sub makeJson {
        my %data;

        $data{kib} = 1;
        $data{games} = {
                0 => [],
                1 => [qw(a b c d e)],
        };

        return \%data;
}

my $x = makeJson();
print encode_json($x) . "\n";

I’m confused because –

If the makeJson() is called several times – and the my %data; is allocated at stack and then that address is being returned – wouldn’t that be a memory leak or maybe not a “leak”, but a “problem”? Because there would be memory allocated at the stack, which is still referenced and thus can not be deallocated.

And other way around: if I have a subroutine returning a hash reference, which is better to use my %data; (faster, because preallocated by compiler?) or my $data={}; ?

  • 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-30T22:23:25+00:00Added an answer on May 30, 2026 at 10:23 pm

    Perl is not C.

    That’s the actual answer to your question, but I’ll expand a bit.

    Perl is not C in that Perl does not have automatic variables. It has lexical and dynamic variables and that’s it.

    Perl is not C in that Perl does memory management so you (mostly) don’t have to. Absent circular references, memory leaks don’t occur.

    Perl is not C in that the cost of being an interpreted language dominates most performance calculations, and so micro-optimizing hashes versus hashrefs is almost always irrelevant.

    Use whichever construct more naturally expresses your intention. Neither leaks memory more than the other; Amdahl can worry about the tiny performance differences. Perl is not C.

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

Sidebar

Related Questions

using the below code for decoding json $categories = json_decode($data); $categories = $categories->data; where
I'm using json data below for flexigrid, but it doesn't work. json.txt {'page':1, 'total':1,
I want to validate below data using regex and python. Below is the dump
I'm trying to add new output column using synchronous custom data flow component(below is
I'm trying to figure out how to replace binary data using Java. below is
I am adding data to a dataset using the below code cmdExcel.CommandText = SELECT
Below is the code that I'm using in ms access to list the data
I am using the json parser from json.org to handle data from my application.
Hi i using this mysql code to generate this data below: code: SELECT L.LEAVETYPE,
I'm struggling to parse the below data using PHP. An API returns it, and

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.