in reply to pass by referene
'100' is a literal. When you take a reference to a literal, it's still a literal. Literals cannot be altered.
Think of it this way: You can take a reference to a number of "things". You can take a reference to an @array, to a %hash, to a $scalar, to a &sub, and even to a literal. If you modify the value of the target of your hash reference, you change the referent hash's value(s). If you modify the value of the target of a scalar ref, you modify the referent scalar container's value. But these are called variables for a reason; they hold variable data. Literals aren't, by their very definition, variable.
An example:
my $string = 'This'; my $number = 100; my $scalar_ref = \$string; # Take a reference to $string. $$scalar_ref = 'That'; # Modify the value of the scalar that # $scalar_ref refers to. print "$$scalar_ref = $string\n"; # That = That because we # changed the value of the # thing that $scalar_ref # points to, which happens to # also be what $string holds. $scalar_ref = \$number; # Change what $scalar_ref points to # doesn't change the referent values. print "$$scalar_ref isn't $string, it's $number\n";
You can modify where a reference points, and can modify the values held in the variable containers referred to by the reference all day long if you want. But if your reference doesn't point to a variable container, but instead points to a literal value, all you can do is modify where it points, not what the underlying value is.
Dave
|
|---|