in reply to Global variable unexpectedly modified when passed by reference

For those of you who might be schooled in other programming languages, it might be useful to you for me to point out that Perl really doesn’t have the notion of “[passing parameters by] reference.”   Instead, it has a generally-applicable notion of:   references.

“A reference” is, first of all, sort-of “a data type.”   In other words, “any variable’s value” could be (say ...) integer, string, floating-point, file-handle, or ... “reference.”   Every “thing” in the Perl world has a “reference count” associated with it, and the Perl runtime system somehow keeps everything straight, so that nothing gets de-allocated too soon.

Nevertheless, this creates a situation that catches many programmers who are accustomed to “strongly-typed,” “compiler-oriented” language systems somewhat off-guard:   it’s all very dynamic.   When a variable contains “a reference,” anything that you do to it is automatically and transparently conveyed to act upon its “target.”   But, that very same variable could at some time cease to contain a “reference” value and, instead, contain an ordinary scalar value, in which case it would behave altogether differently than before.   The runtime behavior of the variable is not bound-to nor determined-by its compile-time (syntactic ...) characteristics.   Instead, that behavior is determined by its (perhaps, very immediate) history and provenance.

“With great power comes great responsibility ...”

Replies are listed 'Best First'.
Re^2: Global variable unexpectedly modified when passed by reference
by Anonymous Monk on Dec 08, 2014 at 21:37 UTC
    When a variable contains "a reference," anything that you do to it is automatically and transparently conveyed to act upon its "target." But, that very same variable could at some time cease to contain a "reference" value and, instead, contain an ordinary scalar value, in which case it would behave altogether differently than before.

    From perlref:

    References are easy to use in Perl. There is just one overriding principle: in general, Perl does no implicit referencing or dereferencing. When a scalar is holding a reference, it always behaves as a simple scalar. It doesn't magically start being an array or hash or subroutine; you have to tell it explicitly to do so, by dereferencing it.

    You are once again completely, utterly wrong. Dereferencing is a general concept that applies to many programming languages.

    Your posting history has revealed your business model:

    Step 1: Market yourself as someone who fixes "legacy software projects that have wandered far off-course", i.e. cleaning up others' messes

    Step 2: Troll programming websites and (a) spread misinformation to those seeking help and (b) cause other professional Perl devs to waste their time proofreading and correcting you instead of helping people

    Step 3: Profit!

    Your LinkedIn profile claims you were an instructor at Scottsdale Community College. I can only assume you used the same approach there, training the next generation of bad programmers. Well, I guess us average Perl devs might be thankful to you for screwing the curve and making us look better. Or maybe you already recognized that and are getting paid for this service by other developers - that'd be quite a racket!

      Fellow monks, as the author of the parent node* I kindly request you approve its reaping. Most of it is obviously just a rant which I no longer think needs to remain on permanent record (although I do hope it will remain in sundialsvc4's mind as a motivation to post better content). I've re-posted the purely technical content of the node below, I think it communicates all that needs to be said.

      * If there are doubts, perhaps one of the gods could confirm using the server logs.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: Global variable unexpectedly modified when passed by reference
by Anonymous Monk on Dec 09, 2014 at 01:21 UTC
    Perl really doesn't have the notion of "(passing parameters) by reference."

    As LanX already pointed out about half an hour before your post, all sub calls are pass-by-reference, since @_ contains aliases to the actual parameters.

Re^2: Global variable unexpectedly modified when passed by reference
by marto (Cardinal) on Dec 09, 2014 at 10:50 UTC

    "With great power comes great responsibility ..."

    Yet you take no responsibility for spamming this site with incorrect information. Someone seems to have figured out your motives for continually doing so. The fact that, at time of writing, this node has positive reputation (+1) is embarrassing.

Re^2: Global variable unexpectedly modified when passed by reference
by ikegami (Patriarch) on Dec 10, 2014 at 07:13 UTC

    Perl really doesn’t have the notion of “[passing parameters by] reference.”

    That's not true. In fact, Perl always passes parameters by reference.

    $ perl -E'sub { $_[0] = "abc" }->($x); say $x' abc

    I think you wanted to point out the phrase "passing by reference" has been incorrectly used instead of "passing a reference" throughout this thread, but you took a wrong turn somewhere.

Re^2: Global variable unexpectedly modified when passed by reference
by Anonymous Monk on Dec 10, 2014 at 01:46 UTC
    When a variable contains "a reference," anything that you do to it is automatically and transparently conveyed to act upon its "target." But, that very same variable could at some time cease to contain a "reference" value and, instead, contain an ordinary scalar value, in which case it would behave altogether differently than before.

    From perlref:

    References are easy to use in Perl. There is just one overriding principle: in general, Perl does no implicit referencing or dereferencing. When a scalar is holding a reference, it always behaves as a simple scalar. It doesn't magically start being an array or hash or subroutine; you have to tell it explicitly to do so, by dereferencing it.

    Dereferencing is a general concept that applies to many programming languages.