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

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991862]
LanX my favourite don't tell me the proof of concept is good enough for production anecdote
[ambrus]: ah, it's one of thos
[Eily]: what, there's a difference between proof of concept and production?
[LanX]: 20 years ago traders were complaining about the latency of the trading system...
[ambrus]: I'm currently in the process of rewriting my proof of concept programs. They sort of developped organically as I was experimenting, so now I've got an ugly mess of multiple programs and one-liners held together by nothing. I'll have to rewrite them to som
[ambrus]: ething that's both cleanly organized and mostly automated.
LanX in train, bad connection
[Corion]: ambrus: Yeah - we're in that situation too, except that there is no time to do the reorganizing :-/
[LanX]: ... so my boss started a project with the newest sun servers and invited the traders to come on weekend to test it... and they were so pleased, that they forced him to keep it in production...
[ambrus]: Corion: sure, this is the long-term plan. The short term is that I have to run this ungodly mess to get results from the new input data today.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (16)
As of 2017-03-29 11:49 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (350 votes). Check out past polls.