in reply to Re^4: Aren't there code refs as well as function refs?
in thread Aren't there code refs as well as function refs?
> Imagine that we have a very large sub that does a lot of things and spans hundreds of lines of code. To make that easier to read, you could try to break it up into individual functions. The problem occurs when this large function also has a lot of variables which all need to be seen by the individual sub functions.
Are you aware of the concept of closure variables° in functional programming or instance-variables in OOP?
If yes did you consider using them here?
> A third solution would be to have code refs which are kind of like inline code in C language
These are not code-refs but preprocessor macros.
Actually Perl supports them, but I never saw them used. It might also depend on the existence of an installed C preprocssor.
Another better portable approach are source-filters.
But as I said above, I think there are already better solutions for that. Preprocessor macros are dangerous.
Anyway I'm still unsure in which way all of this was related to the OPs question.
Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery
sub huge_function { my $many, $variables, ...; # do something # do something else }
---->
{ my $many, $variables, ...; # accessible inside all closed over s +ubs here sub previously_huge_function { # set $many ... do_something(); do_something_else(); } sub do_something { my $internal_var; # access $many... } sub do_something_else { my $internal_var; # access $many... } }
|
|---|