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

Hi,

I'm currently extending the online help of a software system we have here. It has a normal command line where typing in 'help' spills some general help text (more or less a list of known commands with short descriptions).
Now I'd like to do something like 'help <command>' for a more detailed help. Until now, the implementation is very trivial and in my opinion error prone:

... } elsif($command eq '?' or $command eq 'find') { &message(<<'EOT'); Help menu for the command 'find': SYNOPSIS find <phrase> - find <phrase> in lexicon and output al +l meanings found. DESCRIPTION Both commands are optional. If given a phrase, it is default blahblahblah EOT } elsif($command eq 'add') { &message(<<'EOT'); Help menu for the command 'add' SYNOPSIS add <lex> <entry> - add an entry <entry> to the lexicon <l +ex> DESCRIPTION This adds an entry to the lexicon with the name/type <lex>, <entry> must have proper syntax or it will be rejected. If blahblahblah EOT ...
Now I'd like to place some variables into the here documents. E.g. the commands shouldn't be typed in explicitedly, but just the $command variable should be interpolated. HOwever, it seems to me, that here documents are a very static construct not allowing this "template mechanism".

Am I overseeing something?

Bye
 PetaMem

Replies are listed 'Best First'.
Re: Interpolating in Here Documents?
by Abigail-II (Bishop) on Jul 24, 2002 at 11:12 UTC
    Here documents interpolate like double quoted strings. Except when you are putting single quotes around the delimiter.
    my $text = "Some text"; print <<EOT; This does interpolate. Look: $text. EOT print <<'EOT'; This does not interpolate. Look: $text. EOT __END__ This does interpolate. Look: Some text. This does not interpolate. Look: $text.
    Abigail
Re: Interpolating in Here Documents?
by tadman (Prior) on Jul 24, 2002 at 11:30 UTC
    No, no, no. Not chained ifs. Use a hash, which is less problematic.
    my %help = ( find => <<END, Help for command 'find': Don't forget to set the variable \$foo to the default value of '$foo_default', or space rays will erase data on your computer. END add => <<END, Help for command 'add': If you use this command, your bank balance will increase by the specified amount. Advance two spaces and skip a turn. END ); my %alias = ( plus => 'add', '?' => 'find', ); print $help{$alias{$ARGV[0]} || $ARGV[0]};
    This could be adapted to use your message wrapper function, but I beg you: Please do not use the ampersand! That was killed off when Perl 4 was replaced.

    For a quick introduction to how these documents are quoted, Abigail-II has a good example.
      What is wrong with the ampersand? The ampersand was not "killed", it hasn't even been deprecated! Just because you may leave off the ampersand doesn't mean you have to.

      Some people prefer to use the ampersand, let them. (And yes, I know there's the difference if prototypes are used, or if the call is argumentless and parenless.)

      Abigail

        What I mean is that the requirement to use an ampersand on function calls was removed with the introduction of Perl 5. It may be a minor point, it may not even be officially deprecated, but either way, I still plead my case: Say no to ampersands unless you know what you're doing. Virtually every time I see them, it's a case of someone using them out of ignorance, or because they are still dusting off some Perl 4-type code.

        Sure, some people prefer to use them, just I imagine some people prefer to use prototypes everywhere, despite how they can cause trouble. Perhaps some people even prefer to not use strict or warnings. These people are entitled to their opinions, of course.

        What's wrong with the ampersand? In this example, it is completely useless and serves no purpose.