Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: defining variables using my - subroutines

by daxim (Chaplain)
on Sep 05, 2012 at 13:51 UTC ( #991862=note: print w/replies, xml ) Need Help??


in reply to defining variables using my - subroutines

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.
  • Comment on Re: defining variables using my - subroutines

Replies are listed 'Best First'.
Re^2: defining variables using my - subroutines
by Freezer (Sexton) on Sep 05, 2012 at 14:13 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991862]
help
Chatterbox?
[Corion]: Discipulus: I'm slowly migrating my code to require 5.8.x ;) Most of my code works on 5.6, but Filter::signatures requires 5.10 I think
[Corion]: 5.14 is also a good target, widely available and fairly stable. Also, over five years old, which doesn't push people into a needless upgrade cycle
[Discipulus]: yes I saw your efforts Corion but the pain is worth?
[Corion]: Discipulus: IMO it's not that much pain, but most of the systems I use have a Perl 5.14 or newer on them nowadays
[marto]: the question is more, is Padre still the way new users should be directed?
[Discipulus]: ah ah I have all 5.26 atm but I play alone..
[Corion]: Except this RHEL box, which comes with Perl 5.10.1 , but there I installed my own 5.20.3 for the real Perl programs we run ;)
[Corion]: marto: I used Padre for some time but then switched to Notepad++, since all the funky features I wanted to put into Padre couldn't be made to work, and/or I lost interest :)
[Discipulus]: indeed marto I dunno. I tested once and many years ago. But I'm not the programmer yardstick. I try to install Padre via cpan now just to add a line about it in the issue. Strawberryperl all life long!
[marto]: yeah, when I worked on Windows I did use Np++ all the time

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2018-06-25 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (126 votes). Check out past polls.

    Notices?