'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


In reply to Re: pass by referene by davido
in thread pass by referene by mhn388

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.