perl_noob_101 has asked for the wisdom of the Perl Monks concerning the following question:


Hey Monks,
I have two .pl files which share two array variables.

1. Is that a good idea?
2. I get the following error while running the code:

"Attempt to free unreferenced scalar: SV 0x187ad44, Perl interpreter: 0x244114 during global destruction. "

which I believe has something to do with destructors since the arrays contains a list of class objects. Does perl behave in the same way as C++, when it comes to destructors?

Original content restored by GrandFather. See Re: Destructors and global variables for updated content

Replies are listed 'Best First'.
Re: Destructors and global variables
by ikegami (Patriarch) on Jun 10, 2011 at 18:21 UTC

    I have two .pl files which share two array variables.

    Variables don't exists in files, .pl or otherwise. Please explain what you mean.

    Are you asking if code from two files can use the same variable? yes.

    That said, I can't figure out why would a program use two .pl files.

    Does perl behave in the same way as C++, when it comes to destructors?

    What are you asking about specifically?

Re: Destructors and global variables
by TomDLux (Vicar) on Jun 10, 2011 at 20:01 UTC

    If all your scripts use the variables $a, $b, $c, ... @a, @b, @c, ... %a, %b, %c, ... well, it's poor style, but perfectly reasonable. Even if two of the scripts are running at the same time, they exist totally separately.

    How about posting your .pl files .. if they're large, simplify them to a subset which preserves the problem.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: Destructors and global variables
by perl_noob_101 (Initiate) on Jun 10, 2011 at 23:13 UTC

    I guess I wasn't clear in my post. Now that I read it again I see my mistake. So let me give a more detailed explanation. What I am trying to do is to parse through an html file and create objects (am using the moose module for class creation).

    if(...something...) { $n_DT[$count] = DT->new; if($line =~ m/>(.*)</) { $n_DT[$count]->{'ID'} = $1; } ... } elsif(..something else)...) { $r_DT[$count_1] = r_DT->new; if($line =~ m/>(.*)</) { $r_DT[$count_1]->{'ID'} = $1; ... }

    The variables n_DT and r_DT are arrays of objects which I want to use/share between the two scripts. I am also saving the two arrays in text files. So I could possibly read the files again to create my list, but I was wondering if there was another, more efficient, way of doing this. Hope this clears my question.

      The variables n_DT and r_DT are arrays of objects which I want to use/share between the two scripts.

      Again with variables in files.

      So I could possibly read the files again to create my list, but I was wondering if there was another, more efficient, way of doing this.

      You want to share between two processes, perhaps?

      Actually, sounds more like passing for one process to another rather than sharing.

      Unless the second process is a fork of the first, you have to somehow convert the array to a string of bytes to pass it from one process to another. You do have choices as to how to convert the array to bytes (Storable, JSON, etc) and how you actually pass it (file, pipe, socket, etc), but efficiency is not going to be the deciding factor.

Re: Destructors and global variables
by Anonymous Monk on Jun 10, 2011 at 17:38 UTC
    I have two .pl files which share two array variables.

    What does that mean?