in reply to Re^2: ref to read-only alias ... why? (notabug)
in thread ref to read-only alias ... why?

I'd document it pretty much as I already described it. Passing a read-only value or a literal constant to something that makes aliases (for, a sub) may decide to make an alias to the read-only value or may decide to make a copy of it. The decision might even be different in those two case (a literal constant vs. some other read-only scalar). The choice is a matter of optimization and subtle edge cases and Perl code should not depend on either specific behavior. Both behaviors have existed in many different versions of Perl.

No, I don't consider it a bug that some versions of Perl don't die in the face of:

sub add1 { return ++$_[0]; } my $two= add1(1);

Despite the use of "++" over "1+" there being questionable. It does have the interesting and perhaps useful side effect of allowing: add1($count). Yes, it is a contrived example. As is yours.

I don't find it hard to imagine cases where either result would be preferred. I do find it hard to imagine cases where either result is a serious problem that I wouldn't just address with a better interface for the subroutine.

- tye        

Replies are listed 'Best First'.
Re^4: ref to read-only alias ... why? (notabug)
by dk (Chaplain) on Jan 06, 2012 at 17:07 UTC
    Thanks for commenting. Here's a case with a serious problem: a person writes code that doesn't fail, which then gets executed on another installation, and fails. I'd say it's bad enough. I believe I'd file a bug after all (unless you have reasons for me not to).

      Wow. To have code that runs fine for the author and then fails somewhere else constitutes a "serious problem" to you? Cushy world you must've lived in before. Ever looked at "cpan testers" reports? *shrug*

      I wouldn't file a bug. I'd rather just add an eval to the code (actually, I'd probably make a less fragile interface to the sub, as I said).

      - tye        

        > less fragile interface to the sub

        We showed that any call-by-reference can be affected.

        So you are effectively saying that modifying $_[ ] is a fragile interface and bad coding practice?

        I agree it's not a crisis, but this must at least be documented, not only for helping people desperately trying to understand the reason why an imported sub using another imported sub fails out of a sudden, because an alias was passed thru the chain.

        This can't be avoided, even less if people use Data::Alias or Lexical::Alias.


        > I'd rather just add an eval to the code

        And how do you expect eval to help if there is no error thrown?

        Cheers Rolf

        I'd be grateful if you wouldn't be sarcastic towards me, I hate that. I'd continue to collaborate, but I'd think that you're an idiot. Thanks otherwise for expressing your opinion.