What you call "function refs" are what I would call code refs.

I would like to point out a distinction. In a function ref, you cannot just simply modify a variable of the caller except if it is included in the argument list. In a code ref, you could modify the caller's variables but the only way to do this is with the eval() function. Is there any other way to do it? (In the following code, the PrintPattern() function modifies its first argument. But a nicer solution would be to be able to directly modify the caller function's variable.)

#!/usr/bin/perl use strict; use warnings; my $TRIANGLE = '..Z2.X6.V343.T2132312.RI.P4E4.N8A8.L3636363.J313231323132313.Hc..'; DrawPattern1($TRIANGLE); DrawPattern2($TRIANGLE); DrawPattern3($TRIANGLE); ##################################################################### # This function converts the input pattern from plain text to binary. # Also returns the length of the pattern and a couple of zeros. # Usage: ($PATTERN, $LENGTH, 0, 0) = GetPattern($TEXT) # sub GetPattern { my $PATTERN = defined $_[0] ? $_[0] : ''; # Remove everything except letters and numbers and periods. $PATTERN =~ tr|.0-9A-Za-z||cd; # Replace all numbers and letters with characters 00 - 3E $PATTERN =~ tr|.0-9A-Za-z|\x40\x00-\x3E|; return ($PATTERN, length($PATTERN), 0, 0); } ##################################################################### # This function prints a pattern to stdout. # The pattern should be plain text. Each even numbered letter # specifies the number of spaces to print, and odd numbered # letters specify the number of '#' signs to print. # A period creates a line break. # Usage: DrawPattern1(STRING) # sub DrawPattern1 { my ($PATTERN, $LENGTH, $MODE, $c) = GetPattern($_[0]); for (my $i = 0; $i < $LENGTH; $i++) { $c = ord(substr($PATTERN, $i, 1)); PrintPattern($MODE, $c); } } ##################################################################### # Prints a number of spaces or pound signs to stdout, or a new line. # Changes the value of the first argument. # Usage: PrintPattern(WHAT_TO_PRINT, COUNT) # sub PrintPattern { @_ == 2 or return; my ($MODE, $COUNT) = @_; $COUNT < 64 or return $_[0] = print "\n"; $_[0] = $MODE = $MODE & 1 ? 32 : 35; print chr($MODE) x $COUNT; } ##################################################################### # This function does exactly the same thing as DrawPattern1() # Usage: DrawPattern2(STRING) # sub DrawPattern2 { my ($PATTERN, $LENGTH, $MODE, $c) = GetPattern($_[0]); for (my $i = 0; $i < $LENGTH; $i++) { $c = ord(substr($PATTERN, $i, 1)); if ($c == 64) { $MODE = print "\n"; next; } print chr($MODE = $MODE & 1 ? 32 : 35) x $c; } } ##################################################################### # This function does exactly the same thing as DrawPattern1() # Usage: DrawPattern3(STRING) # sub DrawPattern3 { my ($PATTERN, $LENGTH, $MODE, $c) = GetPattern($_[0]); # This is nice but won't work: my $code_ref = \"if (\$c == 64) { \$MODE = print \"\\n\"; next; }" . " print chr(\$MODE = \$MODE & 1 ? 32 : 35) x \$c; "; # This will work: my $CODE_REF = \"if (\$c == 64) { \$MODE = print \"\\n\"; }" . " else { print chr(\$MODE = \$MODE & 1 ? 32 : 35) x \$c; } "; for (my $i = 0; $i < $LENGTH; $i++) { $c = ord(substr($PATTERN, $i, 1)); eval($$CODE_REF); # This is, of course, not a very good idea, because # repeatedly using eval() slows down the program. } } #####################################################################

In reply to Re^2: Aren't there code refs as well as function refs? by harangzsolt33
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.