Git Product home page Git Product logo

Comments (3)

bessarabov avatar bessarabov commented on September 28, 2024

I'm not sure that this answers your question, but in case you use DDP without prototypes it works as Data::Dumper:

bessarabov@5:~$ perl -E 'use DDP; %h = (); p $h{"one"}; p %h;'
undef
{
    one   undef
}
bessarabov@5:~$ perl -E 'use DDP { use_prototypes => 0 }; %h = (); p $h{"one"}; p %h;'
undef
undef
bessarabov@5:~$

from data-printer.

garu avatar garu commented on September 28, 2024

Hi @grtodd! This is yet another unfortunate side-effect of using prototypes. The way they work, to allow people to provide a single argument to p() and np(), we must use backslashed prototypes (\$) and start with a reference to your variable - this way we don't have to care whether the developer did p(@foo), p($foo), p(%foo), etc. This means when you wrote:

p $h{"one"}

You were actually sending us \$h{"one"}, which unfortunately will always create the key. You can check that behaviour yourself by running:

perl -E '\$x{bla}; say keys %x'

I went to p5p and they said it is expected behavior on some left hand side. Below are the relevant bits of that discussion:

< Zefram> couldn't do much else.  without the hash element being created, what would the reference returned by \ be referring to?
< TonyC> maybe a PVLV, but changing it might be a back-compat issue
< TonyC> garu: what you're seeing isn't autovivication - that refers to initializing an undef variable as a reference when you use it as one
< Zefram> fundamentally, \ is treating its operand as an lvalue, same as if it were on the lhs of an assignment
< Zefram> it's not well documented which operations will eagerly create the hash element like that.  foo($x{bla}) *also* treats $x{bla} as an lvalue, but doesn't eagerly create it.  it
                passes a PVLV to foo(), which can then create the hash element by assigning to $_[0]
< Zefram> this is a hedge because function arguments are usually taken by value but there's no type information to say which arguments specifically are taken by reference and so need the lvalue treatment
< garu> I see. Is there any consistency?
< garu> and, if not, should we consider forcing one? Even if, as TonyC mentioned, it might create back-compat issues?
< Zefram> it's consistent between perl versions, and is so long standing that it would be very troublesome to change
< Zefram> making all lvalue contexts behave the same would probably not be worthwhile

So it's as @bessarabov said. The only way to prevent this is to not use prototypes at all 😕 You can achieve that by several ways, my favourite being adding "use_prototypes => 0" on your .dataprinter file and forgetting about it.

from data-printer.

grtodd avatar grtodd commented on September 28, 2024

OK thanks and to @bessarabov as well. This great explanation could appear in the POD :-)

from data-printer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.