in reply to passing reference to subroutine

I think you've removed too much context for any answer to actually solve the problem you haven't described. However I'll address the question as posed just in case it does have some bearing on your actual problem.

The sub call passes a reference to an array into the sub - so far so good. Note though that @struct outside the sub has nothing to do with @struct inside the sub: It would be clearer to choose a different name for one of the arrays.

In the sub $ref gets a copy of the passed in array reference. A copy of the elements in the referenced array is then made and placed into the sub's @struct - a copy note!

undef @struct; (better written as @struct = (); btw) clears out the array containing a copy of the original array.

The important thing to realise here is that nothing done in the sub affects the original array because all the work is done on a copy of the contents of the original array.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: passing reference to subroutine
by BrowserUk (Patriarch) on Oct 28, 2011 at 08:18 UTC
    undef @struct; (better written as @struct = (); btw)

    Not if the goal is to free up space rather than just empty it for reuse:

    @a = 1..1e6;; print total_size \@a;; 32000176 @a = ();; print total_size \@a;; 8000176 undef @a;; print total_size \@a;; 176

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: passing reference to subroutine
by asthaonard (Acolyte) on Oct 28, 2011 at 08:31 UTC

    i see... but how would i avoid that copying? How should i dereference my variable correctly?

      What do you really want to do? The dereference is as simple as undef @$ref; (see below for the entire updated sub), but I have the strong feeling that you are asking the wrong question and providing the wrong sample code. It seems extraordinary that you would call a sub just to clear an array!

      sub undef_ref { my $ref = shift; undef @$ref; print '=' x 20; print "\n"; print Dumper (@$ref); }
      True laziness is hard work

        Well, I'm trying to modify an array in a subroutine. I have chose to undef my array because... well... i thought it would be good example. I was wrong :)

        Your code works as expected.

        Thank you very much

      It's worth the time to have a look at perlreftut -- the simple reference tutorial :)