in reply to Re: Bit operation
in thread Bit operation

You can't change the value of $a by using it as a function parameter in MyGetFunc.

Sure you can, perl is perl :)

sub lunch { $_[0]++ } my $foo = 1; warn $foo; lunch($foo); warn $foo; __END__ 1 at - line 3. 2 at - line 5.
search for alias in perlsub

Replies are listed 'Best First'.
Re^3: Bit operation
by ack (Deacon) on Jan 12, 2011 at 16:21 UTC

    I assume what raybies was referring to was that unless $a is within a scope that encloses the subroutine (which it is in your case but may not necessarily be in the OP's case) then it *can* be modified without having to return it.

    However, in my experience, having been bitten way too many times by not paying attention to scopes and mis-using "global variables", in my humble opinion, it is much, much better to be careful and discipline oneself to think of it more along the lines as raybies posed it by keeping things more encapsulated and planning for the variable to be local to the subroutine and then explicitly returning values that you want to change...or to at least be clearer, through the use of references as some of the other responders suggested, that you are doing the change.

    But that is just my opinion and certainly your comment is, to the best of my knowledge, right.

    ack Albuquerque, NM

      You seem to be referring to accessing and changing global variables from inside a subroutine. But notice that Anonymous Monk's example doesn't use global variables. It uses the the fact that perl actually uses call-by-reference when calling a subroutine. The variable @_ contains references to the variables that were used in the subroutine invocation. That is why it is so important to use something like my ($bla,$x,@f)= @_; in subroutines. Because otherwise you are in danger of changing values at a distance.