Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Just to explain one possibility of what might be going wrong in your original code without strict, so that it doesn't remain a mysterious "leak", because this kind of thing can theoretically bite you even under strict (although it's a lot less likely):

When you don't use strict, all the variables you use without a declaration are automatically "package variables", that is, they are global everywhere in their package, and since most scripts only have one package (the default, main), they're effectively global throughout the entire script. So anywhere you take a reference to an array that is a package variable (\@newArray), that's always going to refer to the same array (Update: I'm simplifying a bit and leaving out some "advanced" stuff like local for the sake of this explanation). So if you make modifications to that array anywhere in the script, they will be visible everywhere else. This may be what is biting you in your original code.

So when you do use strict, you're forced to declare all your variables e.g. with my or our. The latter actually declares package variables, so they'll have the same behavior as I just described. But variables declared with my are "private" to the enclosing block, meaning that code outside the block can't normally see or access them. Every time you call a subroutine that contains a my @newArray declaration, that will create a new memory location for you - this is the important part in this case. Every time you run that sub and do return \@newArray, that's a new array. (More details in Private Variables via my().)

Here's a demo, two functions that re-implement Perl's x operator for lists, one correctly and one incorrectly:

use warnings; use strict; use Data::Dump; sub good { my ($string,$times) = @_; my @array; # lexical variable push @array, $string for 1..$times; return \@array; } sub bad { my ($string,$times) = @_; our @array; # package variable # always the same "@array" here! push @array, $string for 1..$times; return \@array; } my $arr1 = good("foo",2); dd $arr1; # prints ["foo", "foo"] my $arr2 = good("bar",2); dd $arr2; # prints ["bar", "bar"] my $arr3 = bad("quz",2); dd $arr3; # prints ["quz", "quz"] my $arr4 = bad("baz",2); dd $arr4; # prints ["quz", "quz", "baz", "baz"]

Of course, it could also be something a bit simpler, like that you accidentally re-used variable names like @newArray or @arrayToBeManipulated in multiple places in your code.

In reply to Re^3: Leaking Function Return by haukex
in thread Leaking Function Return by cgoolsby

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (9)
    As of 2018-07-16 07:00 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (333 votes). Check out past polls.