Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

defining variables using my - subroutines

by Freezer (Sexton)
on Sep 05, 2012 at 13:41 UTC ( #991855=perlquestion: print w/replies, xml ) Need Help??
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 (Parson) 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 (Abbot) 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 $_ } }

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

Re: defining variables using my - subroutines
by daxim (Chaplain) 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 (Sage) 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?


        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?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991855]
Approved by NetWallah
[Discipulus]: now i must come back to my house/hoven monks: dont do interesting discussion in my absence!
[erix]: or apparently, now it does :)
[GotToBTru]: heh
erix prods ambrus to (re-)start cbstream...
[GotToBTru]: I've got a program with an if ... or ... statement with 80 clauses
[stevieb]: 80 clauses? dispatch table?
[GotToBTru]: no, lookup
[stevieb]: no, I mean have you considered using a dispatch table. an 80 condition if/else is huge :)
[GotToBTru]: that is how this should have been done, no question

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2017-06-22 13:00 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (519 votes). Check out past polls.