Data-Printer icon indicating copy to clipboard operation
Data-Printer copied to clipboard

[wishlist] Using default variable when calling p() without parameters.

Open bessarabov opened this issue 12 years ago • 2 comments

Here is my .dataprinter:

{
    use_prototypes => 0,
}

And here is the test script:

#!/usr/bin/perl

use DDP;

foreach (1..2) {
    p;  
    p $_; 
}

It outputs:

undef
1
undef
2

but I'm expected it to output:

1
1
2
2

What you do think? Is this behaviour good and should it be implemented? Or this is the expected behaviour that should be left as it is? Or this is a bug?

By the way, If I run this scrip without custom .dataprinter I'll get error:

Not enough arguments for Data::Printer::p at a.pl line 6, near "p;"
Execution of a.pl aborted due to compilation errors.

bessarabov avatar Feb 27 '13 16:02 bessarabov

I don't think p() should be called without arguments (which is why it complains about it without the 'use_prototypes => 0' in your .dataprinter), so I'd say it's a bug that it lets you do it!

That said, I looks very reasonable to me that if you say p() without arguments you are interested in $_, feels perlish. I'm gonna think about it for a bit and see if I can come up with a decent solution that works with and without function prototypes. Tomorrow. It's almost 4am here now :D

garu avatar Mar 02 '13 06:03 garu

Alright, prototypes are hitting me hard here. According to perlsub, adding a "_" to the prototype list before the ; should do the trick, but right now I'm only able to get the non-prototyped version to see the caller's $_ variable, and even thought it's a cool feature to have, I don't think it's sane to have a feature like that only working if the user has a specific setup, and I can't get rid of the prototypes altogether without losing lots of syntax sugar too, like non-backslashed variables and inline options without braces.

What do you think? Can you make the (\[@$%&];%) prototype saner and considering $_? Patches are welcome!

Otherwise, the best I can do is turn it into a feature you get only without prototypes, but that would really hurt consistency, so I'm not very confident about it.

garu avatar Mar 03 '13 07:03 garu