Comments (3)
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.
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.
OK thanks and to @bessarabov as well. This great explanation could appear in the POD :-)
from data-printer.
Related Issues (20)
- Introspection into Object::Pad-based objects HOT 5
- double quotes suddenly escaped. HOT 4
- SGR attributes leak from escaped key HOT 1
- Special handling for undef in _check_readonly()
- ContentType filter produces warnings if data has wide characters
- show_unicode is documented as being on by default, but it isn't
- Incorrect output with tied hashes HOT 2
- please tag release 1.1.0 HOT 4
- spews warnings with recent Object::Pad: Object::Pad::MOP is experimental ... HOT 5
- Using DDP in Perl debugger prints data twice HOT 2
- support native booleans in Data::Printer::Filter::Web
- t/011-class.t: 2 test failures in perl-5.39.1 HOT 1
- [wishlist] Light theme(s) HOT 1
- Data::Printer doesn't escape control characters HOT 1
- Add an option to show where DP was invoked HOT 1
- Print core booleans as `true` / `false` HOT 1
- Unknown warnings category 'experimental::builtin' HOT 1
- This module fails to pass tests on Windows due to the use of OS specific hard-coded path separators in two of the Unit Tests
- Support Table based sorting scheme for DBIx::Class::Row
- tagging typed data in JSON: should we?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from data-printer.