G'day harangzsolt33,

"I would like to point out a distinction."

You have failed to do that. The 108 lines of code that you've posted contain no references to functions, subroutines, or any other code.

Calling a variable $CODE_REF does not make it a coderef. $CODE_REF is not any kind of reference; it's just a string:

$ perl -MO=Deparse -e 'my $CODE_REF = \"if (\$c == 64) { \$MODE = prin +t \"\\n\"; }" . " else { print chr(\$MODE = \$MODE & 1 ? 32 : 35) x \ +$c; } ";' my $CODE_REF = 'SCALAR(0xa00038be0) else { print chr($MODE = $MODE & 1 + ? 32 : 35) x $c; } '; -e syntax OK

You can explicitly check it yourself:

$ perl -e 'my $CODE_REF = \"if (\$c == 64) { \$MODE = print \"\\n\"; } +" . " else { print chr(\$MODE = \$MODE & 1 ? 32 : 35) x \$c; } "; my +$ref = ref $code_ref; print $ref ? $ref : "not a ref";' not a ref

Or let Perl do it for you:

$ perl -Mstrict -e 'my $CODE_REF = \"if (\$c == 64) { \$MODE = print \ +"\\n\"; }" . " else { print chr(\$MODE = \$MODE & 1 ? 32 : 35) x \$c; + } "; $CODE_REF->();' Can't use string ("SCALAR(0xa00038c70) else { print"...) as a subrouti +ne ref while "strict refs" in use at -e line 1.
"Is there any other way to do it?"

You can modify a caller's variables by passing references to those variables. Here's a very simple example:

#!/usr/bin/env perl use strict; use warnings; my ($x, $y) = qw{X Y}; print "BEFORE:\n"; print "\$x[$x] \$y[$y]\n"; mod_callers_vars(\$x, \$y); print "AFTER:\n"; print "\$x[$x] \$y[$y]\n"; sub mod_callers_vars { my ($x_ref, $y_ref) = @_; $$x_ref = 'A'; $$y_ref = 'B'; return; }

Output:

BEFORE: $x[X] $y[Y] AFTER: $x[A] $y[B]

I see LanX has shown you how to use closures (which could be appropriate for more complex scenarios).

In "Re^4: Aren't there code refs as well as function refs?", you wrote:

"... you decide to call each function with 30 or so arguments."

That would be a very poor decision. Instead, use a single hashref argument.

— Ken


In reply to Re^3: Aren't there code refs as well as function refs? by kcott
in thread Aren't there code refs as well as function refs? by dd-b

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.