Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: RFC: Inline::Blocks or inline as a keyword?

by Eily (Monsignor)
on Aug 01, 2018 at 15:58 UTC ( #1219659=note: print w/replies, xml ) Need Help??


in reply to Re^2: RFC: Inline::Blocks or inline as a keyword?
in thread RFC: Inline::Blocks or inline as a keyword?

Seriously, why should source filtering be bad for perl, but good for C?
In both cases the compiler might report an error where the mistake was inserted, rather than where it actually is. Also in perl, source filters screw up the line numbering (insert two lines in the code, and the line numbers for every compilation message, or warn or die afterwards will be off by two). A call to warn on the last line of your test will indicate an issue on line 35 of a 26 lines file...

The #line directive can solve most of those issues though, so if you turn

sub capitalize_next { my ($thing) = @_; uc inline increase($thing); } sub increase { my ($foo) = @_; ++$foo; }
into
sub capitalize_next { my ($thing) = @_; uc do { local @_ = ($thing); #line 6 my ($foo) = @_; ++$foo; #line 3 }; } sub increase { my ($foo) = @_; ++$foo; }
Mistakes in the parameter list (eg increase(£thing)) will be marked as coming from line 3 (the line of the call), and mistakes from the function definition (eg ++£foo) will be reported as coming from inside the function definition.

local @_ makes it possible to access $_[0] and other values inside @_, while making line numbering easier (if ($foo) = @_ is replaced by ($foo) = $thing, the LHS comes from the function body but the RHS from the call, so where do you report an erreor?), but it probably slows things down... And since $foo = shift; uses @_ inside a function, but @ARGV elsewhere, it might introduce some interresting bugs.

Also note that the C preprocessor will only expand macros in code, not in comments nor in strings. Try this in your example:

sub capitalize_next { my ($thing) = @_; # uc inline increase($thing); }
and you'll get some confusing errors. It's pretty easy to correct that one, but things might get difficult with strings and pod.

Replies are listed 'Best First'.
Re^4: RFC: Inline::Blocks or inline as a keyword?
by shmem (Chancellor) on Aug 01, 2018 at 23:51 UTC
    In both cases the compiler might report an error where the mistake was inserted, rather than where it actually is.

    Well, after the file is filtered and text inserted via inline directive expansion, the erroneous code is duplicated and defacto is actually also at the new location. That's how inlining works. Anybody using a source filtering module should know what they are doing. The confusing line numbers issue can be mitigated by running -MO::Deparse,-l on the faulty file, and the line numbers reported by the compiler will match those of the output.

    This is precisely why I included the debug switch into the module configuration, which causes the output of the code after mangling. But yes, there are much more CAVEATS than those currently mentioned in that section.

    Thanks for your input!

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2020-03-30 08:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    To "Disagree to disagree" means to:









    Results (175 votes). Check out past polls.

    Notices?