Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Spotting an empty array as argument

by Chuma (Scribe)
on Mar 26, 2021 at 13:43 UTC ( [id://11130382]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130382]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found