in reply to Spotting an empty array as argument

Well, there is of course a way of doing it, but it's not pretty...

sub brutesay{ if(@_>=1){ for(@_){print("$_\n")} }else{ ($package,$filename,$line) = caller; open $fh,$filename; for $i(0..$line-2){<$fh>} if(<$fh>=~/brutesay *(\(\))? *;/){print "$_\n";} } }

Replies are listed 'Best First'.
Re^2: Spotting an empty array as argument
by haukex (Archbishop) on Mar 26, 2021 at 22:14 UTC

    That'll break if you have two brutesays on one line ;-) print and say get special parsing by Perl, and as others have explained, you can't replicate their behavior 100% with Prototypes, everything will just be an approximation. I think the closest you'll get functionally is LanX's $, suggestion.

Re^2: Spotting an empty array as argument
by LanX (Saint) on Mar 26, 2021 at 18:54 UTC
    Well yes, I was reluctant to suggesting parsing the source since it seemed far beyond your scope. (Caller has also some limitations if it comes to the line number)

    IIRC is Carp listing the stack trace with the original arguments if called inside the debugger. That's because DB is caching all source lines internally.

    You could do the same with a "passiv" source filter.

    Since it would only read and not change the source it can't possibly cause any harm.

    Another way is adding __DATA__ at the end of your file, because the DATA filehandle is just reading your source. ( seek and see ;)

    Both possibilities need far less resources and are more reliable.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery