Modifying a variable inside a sub 
Author Message
 Modifying a variable inside a sub

I want to modify modify a variable inside a sub. E.g.

$apple = 'red'
print "$apple\n";
emptyIt(\%apple);
print "$aplle\n";

sub emptyIt() {
    $h = shift;
    $h = 'blue';

Quote:
}

First 'red' is printed, and SUPPOSINGLY 'blue' is gonna be printed
but 'red' is printed for the second time. How do I change $apple
to 'blue'.

Thanks

/lim/

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.



Sun, 23 Dec 2001 03:00:00 GMT  
 Modifying a variable inside a sub

Quote:

>I want to modify modify a variable inside a sub. E.g.

>$apple = 'red'
>print "$apple\n";
>emptyIt(\%apple);

This IS NOT how to reference a scalar. This creates a reference to a
hash. Which doesn't exist anyway. Use perl -w for goodness sake. RTFF.

Quote:
>print "$aplle\n";

>sub emptyIt() {
>    $h = shift;
>    $h = 'blue';

$h is supposed to be a reference. In order to assign a value to the
variable it refers to you have to dereference it first.

Quote:
>}

phys-nhhdasrvr:nhhdaba> cat test.pl
#!/usr/local/bin/perl -w # <- check it out

use strict; # <- check this out too

my $apple = 'red';

print ( "$apple\n" );
emptyIt ( \$apple ); # create a reference to the scalar
print ( "$apple\n" );

sub emptyIt {

        my $ref = shift;
        ${ $ref } = 'blue'; # dereference and assign

Quote:
}

phys-nhhdasrvr:nhhdaba> test.pl
red
blue

Hope this helps

Gary
--
Gary O'Keefe

You know the score - my current employer has nothing to do with what I post



Sun, 23 Dec 2001 03:00:00 GMT  
 Modifying a variable inside a sub

Quote:
>$apple = 'red'
>print "$apple\n";
>emptyIt(\%apple);
>print "$aplle\n";

>sub emptyIt() {
>    $h = shift;
>    $h = 'blue';
>}

there are so many things wrong with this code

$apple = 'red'  # missing semi-colon
print "$apple\n";     # one of three correct lines
emptyIt(\%apple);       # check the perlref and perldata manuals you're
                        # passing a reference to a new hash called %apple
print "$aplle\n";     # misspelling apple

sub emptyIt() { # second of the three correct lines
    $h = shift; # third
    $h = 'blue';        # check perlred manual - it really is stated very
                        # plainly in there

Quote:
}                       # no points for this line - too easy
>First 'red' is printed, and SUPPOSINGLY 'blue' is gonna be printed

there's only one person in this newsgroup who would suppose that

--
 jason - remove all hyphens for email reply -



Sun, 23 Dec 2001 03:00:00 GMT  
 Modifying a variable inside a sub

[snip]

! sub emptyIt() {       # second of the three correct lines
!     $h = shift;       # third

I would hardly call defining a sub with an empty prototype
correct when the subroutine obviously expects an argument.

regards
andrew



Sun, 23 Dec 2001 03:00:00 GMT  
 Modifying a variable inside a sub

Quote:

> #!/usr/local/bin/perl -w # <- check it out

> use strict; # <- check this out too

> my $apple = 'red';

> print ( "$apple\n" );
> emptyIt ( \$apple ); # create a reference to the scalar
> print ( "$apple\n" );

> sub emptyIt {

>   my $ref = shift;
>   ${ $ref } = 'blue'; # dereference and assign

> }

Or, skip the references entirely.

#!/usr/local/bin/perl -w

use strict; #

my $apple = 'red';

print ( "$apple\n" );
emptyIt ( $apple );
print ( "$apple\n" );

sub emptyIt {
    $_[0] = 'blue';

Quote:
}


subroutine call.

--
 _ / '  _      /       - aka -

    /                                http://www.tiac.net/users/chipmunk/
        "It's funny 'cause it's true ... and vice versa."



Mon, 24 Dec 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Bug -- variable corruption inside sub/foreach

2. Outer scope of a sub inside a sub

3. Error : calling sub routine inside another sub routine

4. anonymous sub inside anonymous sub?

5. Evaluating a Variable INSIDE a variable?

6. variable inside variable

7. scalar variable inside of a scalar variable

8. lvalue sub doesn't work inside perl debugger

9. Passing file descriptor to sub inside a package?

10. oo programming and sub routines inside module

11. Getting the name of a function (sub) inside it

12. using Find::file inside a sub-routine ..?

 

 
Powered by phpBB® Forum Software