Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Global variable unexpectedly modified when passed by reference

by Anonymous Monk
on Dec 10, 2014 at 01:01 UTC ( [id://1109831]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Global variable unexpectedly modified when passed by reference
in thread Global variable unexpectedly modified when passed by reference

Friend, I am n-o-t going to argue with you ... no, not at all.   The snippet as-written intends to say:   “let $i contain a reference to $j.”   Even if you wish to now argue that, “no, no, it doesn’t actually say that, as you surely would have known if you had read <<thus-and-such document>>” ... the fact remains that the language-system “raised no ‘quibble’ at all.”

Honestly, my point was never to argue the point of whether-or-not the Perl language system was “doing the right thing.”   Obviously, it is doing exactly what it was carefully-designed to do.   (And, by the by, I “read the documentation” half-a-decade ago.)   My point is simply that it is most-certainly a behavior that might catch catches a great many people off-guard.   Most languages have utterly no concept of “context,” ‘numeric’ or otherwise.     Most languages deal with the entire issue of “indirection’ at a syntactic, compile-time level ... not as a runtime data-type.   Therefore, if that’s what you’re accustomed to, then you are in for a big surprise.

And let me politely emphasize again:   I am not criticizing Perl, therefore there is no need whatsoever to defend it.   The language is what it is.   It was designed to be what it is.   It is, if I may say, “The Swiss-Army® Knife” because of(!) what it is.   However, it is sharply different from many other languages in this particular respect ... and that, and that alone, is my point here.

  • Comment on Re^3: Global variable unexpectedly modified when passed by reference

Replies are listed 'Best First'.
Re^4: Global variable unexpectedly modified when passed by reference
by BrowserUk (Patriarch) on Dec 10, 2014 at 02:32 UTC

    You know, hidden away in all the pointless hieroglyphics, verbose irrelevancies, overly complicated example, and cutesy catch phrases, there is actually the glimmer of an arguable point:

    the language-system “raised no ‘quibble’ at all.”

    Isolated and translated, if you'd said: "Perl doesn't raise a warning when you use a reference in a numeric context; and that has caught me out.", then you might at least have engendered some reasonable discussion on a potentially valid point.

    And so, just for you, here it is:

    C:\test\perl-5.18.0>.\perl -E"say 0+$_ for \( 1, 'fred', $^O, @a, %h)" Reference used in a numeric context at -e line 1. 10473656 Reference used in a numeric context at -e line 1. 10474016 Reference used in a numeric context at -e line 1. 10845904 Reference used in a numeric context at -e line 1. 10845952 Reference used in a numeric context at -e line 1. 10848256

    Now that needs work. If the reference is held in a variable (as opposed to being use anonymously print \$s + 0;), it'd be nice to list which variable. And the exact text would probably need world+dog approval, along with its warning category; but it could be argued for.

    And the change required to make that possible:

    C:\test\perl-5.18.0>diff -u sv.c sv.c.modified --- sv.c Wed Dec 10 02:32:20 2014 +++ sv.c.modified Wed Dec 10 02:13:15 2014 @@ -2682,6 +2682,7 @@ if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) return sv_2num(tmpsv); } + Perl_warner(aTHX_ packWARN(WARN_NUMERIC), "Reference used in a + numeric context" ); return sv_2mortal(newSVuv(PTR2UV(SvRV(sv)))); }

    That's right. A single added line. (Feel free to take that to p5p and champion it.)

    Of course, that raises the question, why if it is so easy to implement, hasn't it been done before? The most likely answer to which is: because no one has found it to be necessary. Till now...?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^4: Global variable unexpectedly modified when passed by reference
by Anonymous Monk on Dec 10, 2014 at 01:29 UTC

    You're free to suggest a new "reference used in numeric context" warning to P5P, or even write a patch yourself. But you're deflecting from the points made above ("Clearly erroneous", "statement ... behaves altogether differently", etc.).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-23 22:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found