in reply to Re: my and scope
in thread my, scope, and references

Hi Rolf,

Thanks, that helps. I re-read Chapter 8 of the Camel book after my code worked, and I came across the following on page 243:

Suppose, for example, that you create a hard reference to a lexically scoped array named @array. This hard reference, and the referent it refers to, will continue to exist even after @array goes out of scope. A referent is only destroyed when all the references to it are eliminated.

So everyone else thinks it's good style?

I appreciate all the quick responses.

Chris

Replies are listed 'Best First'.
Re^3: my and scope
by davido (Cardinal) on Mar 28, 2013 at 22:06 UTC

    Being able to return a reference safely is fundamental to Perl, and doesn't raise any style-police eyebrows. Consider DBI, where database rows are fetched as an array-ref or hash-ref. Consider JSON, where the thawed JSON data is returned as a reference to a Perl data structure.

    Since you have a C++ background, you might appreciate that Perl references are somewhat analogous to C++11's std::shared_ptr; usually safe to return, because they are referenced counted, and safe to let fall out of scope, because the reference counting also handles destruction.


    Dave

Re^3: my and scope
by LanX (Saint) on Mar 28, 2013 at 22:14 UTC
    > So everyone else thinks it's good style?

    This is pluralistic Perl and not a "benevolent dictatorship"!

    (You will always find someone arguing, because of "side effects" or whatever. ;-)

    But honestly as Davido explained, it's fundamental, if you skip these feature it's not Perl anymore.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Indeed, it is fundamental. Were it not for returning references safely, we wouldn't have closures, and we wouldn't have blessed reference based objects.


      Dave