http://www.perlmonks.org?node_id=991855

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

Dear Monks,

I wish to generate an array and a variable within one subroutine, return those data holders and use them to pass into a second subroutine. The calling of the subroutines is within a while loop. Where do a declare these variables using my? Outside of the while loop and also outside the subroutines? Inside the while loop? Inside the subroutine that is first called? Or a combination of the above?

Replies are listed 'Best First'.
Re: defining variables using my - subroutines
by Arunbear (Prior) on Sep 05, 2012 at 15:59 UTC
    Declare them nearest to the point where they are used (usually):
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub make_them { my $var = 1; my @array = qw(a b c); return ($var, \@array); } sub use_them { my ($var, $array_ref) = @_; print Dumper($var, $array_ref) . "\n"; } while ( 1 ) { my ($var, $array_ref) = make_them(); use_them($var, $array_ref); last; # demo }
Re: defining variables using my - subroutines
by NetWallah (Canon) on Sep 05, 2012 at 15:52 UTC
    my ($generated_array_ref, $generated_var) = Generator(); Second_sub_receives_array_ref( $generated_array_ref, $generated_var); sub Generator{ my $localvar=33; return ([0..50], # Generated array ref $localvar); } sub Second_sub_receives_array_ref{ my ($rcvd_arrayref, $rcvd_var) = @_; for (@$rcvd_arrayref){ # array element is available as $_ } }
    UNTESTED.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

Re: defining variables using my - subroutines
by daxim (Curate) on Sep 05, 2012 at 13:51 UTC
    The question is nonsensical because subroutines in Perl only take and return lists, not variables. If you define tightly-scoped variables in a subroutine, they are not available outside of it. Instead of paraphrasing your approaches in English prose, show the code/attempts you already have, then we can properly discuss it.
      Surely they take variables. I don't understand when you say that they don't. If I define variables outside of a subroutine, are they available inside the subroutine?
        Under normal usage, they take values not variables. If you run the code
        sub add_one { my $x = shift; $x += 1; return $x; }
        You would not expect the variable you passed in (my $y = add_one($outer_x);) to change value, right? This is a little awkward/contrived, but this is in contrast to more object-oriented frameworks.

        Advanced: Of course Perl actually does pass by reference with lvalues bound to @_. But that's probably the best reason for transferring subroutine inputs to locally-scoped variables.


        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        $scalar = ( 1, 2, 3 );
        $scalar is a variable, it exists past this line, but ( 1, 2, 3 ); is a list of value, it does not exist past that line

        @array = ( 'list item 1', 2, 'another value', 'yet another value ');
        an @array is a variable, it has a name, it exists past the line, it is a bucket, you store scalars inside
Re: defining variables using my - subroutines
by GrandFather (Saint) on Sep 05, 2012 at 23:49 UTC

    You may care to reread Re: Calling a subroutine within a conditional which addresses exactly that issue. If there is stuff in that reply you don't understand, ask.

    In general variables should be declared in the smallest scope that makes sense and most often initialised with their first sensible value when they are declared. Think about which parts of the code need to be able to access a variable and declare it so that only those parts can see it (but don't use global variables).

    True laziness is hard work
Re: defining variables using my - subroutines
by Anonymous Monk on Sep 05, 2012 at 13:48 UTC

    Do you have any code?

      Yes:

      http://www.perlmonks.org/?node_id=991832

        Please spend some effort and reduce your code to the bare minimum that reproduces the problem.

        Is the database access relevant to your problem?

        Are the three different subroutines relevant to your problems?

        You've been advised several times by now not to use prototypes on your subroutines. Are they relevant to your problems?