Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Pointers and References

by tybalt89 (Prior)
on Nov 23, 2020 at 02:33 UTC ( #11124043=note: print w/replies, xml ) Need Help??

in reply to Pointers and References

A couple lines fixed...

#!/usr/bin/perl use strict; # use warnings; use v5.10; my $variable = 22; my $pointer = \$variable; say "The address of \$varible, which contains the value $variable,"; say "is $pointer"; $$pointer = 25; say "Look at that! \$variable now equals $variable"; sub sum_and_diff { my $a = shift @_; my $b = shift @_; # my $res = \(shift @_); # why does the "\" work here? # it didn't my $res = shift @_; my $sum = $a + $b; $$res = $a - $b; return $sum; } my $b = 2; my $diff; # this is line 27 my $pointer_to_diff = \$diff; say "the sum of 5 and $b is ", &sum_and_diff(5, $b, $pointer_to_diff); #say "and the difference is ", $pointer_to_diff; say "and the difference is ", $$pointer_to_diff; say "the sum of 9 and $b is ", &sum_and_diff(9, $b, \$diff); say "and the difference is ", $diff; # this is line 34


The address of $varible, which contains the value 22, is SCALAR(0x55d283691438) Look at that! $variable now equals 25 the sum of 5 and 2 is 7 and the difference is 3 the sum of 9 and 2 is 11 and the difference is 7

Replies are listed 'Best First'.
Re^2: Pointers and References
by Leudwinus (Beadle) on Nov 25, 2020 at 17:39 UTC
    Thank you for that!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124043]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2021-09-17 10:24 GMT
Find Nodes?
    Voting Booth?

    No recent polls found