Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there! I am working on a library of functions that print different HTML tags, given non-HTML text arguments. (Some might think it's kind of stupid since there are already modules that do this, but it's turing out to be a great way for me to get really down and dirty with the Perl -- and I am digging it!) It's working great so far! Except that I think I hit upon a hitch and may have discovered what is either a shortcoming (probably intentional) in Perl or in my implementation (more likely the case.) The issue revolves around passing a subroutine as part of a text string that forms the argument of a call to another subroutine. Here's the code:
# The main subroutine in question sub MessagePrinter { my(@logmessages) = @_; ULStart(); foreach $message (@logmessages) { LI(); Strong($message); } ULEnd(); } ############################################ # Suppporting subroutines sub ULStart { print "<UL>"; } sub LI { my($inputValue) = @_; print "<LI>$inputValue\n"; } sub ULEnd { print "</UL>"; } ######################################
As stated above, everything works great (unless I have inadvertantly introduced some typos.) But... I would be happier if I could issue the call to LI() in the sixth line of PrintMessage(%

Replies are listed 'Best First'.
Re: function(function(noFunction?))
by Anonymous Monk on Feb 09, 2000 at 08:36 UTC
    Yes, btrott, you answered my question exactly!
    (The rest of my explanation... with the specific question that you ended up guessing and then answering, got cut off for some reason.)
    Thanks for the advice about returning the HTML instead of explicitly printing it. That should help get me back on track!
    I am returning to the programing thing after taking a break for several years, and I am definitely feeling a bit rusty.
    One thing I am struggling with is finding the right balance between trying to write my code generically enough so that I can reuse it as much as possible and writing it specifically enough so that it actually does something.
    I am sure I am not alone on that count though.
    Thanks again mate. Hail, Larry!
RE: function(function(noFunction?))
by btrott (Parson) on Feb 09, 2000 at 02:18 UTC
    I'm having a bit of trouble understanding what you're getting at, but I *think* I understand; you have this:
    foreach $message (@logmessages) { LI(); Strong($message); }
    and you want to be able to say:
    foreach $message (@logmessages) { LI(Strong($message)); }
    Is that correct?

    If so, what you need to do is modify your "Strong" sub so that it *returns* the HTML rather than printing it out.

    In other words:

    sub Strong { return "<STRONG>" . $_[0] . "</STRONG>"; }
    Now you can say
    LI(Strong($message));
    and it should work.

    Is that what you were asking? (Of course, if you're going to do this, you *may* want to think about making all of your functions return the HTML rather than printing it, to be consistent.)