From: Tal Cohen <[EMAIL PROTECTED]>
   Date: Sun, 18 Sep 2005 12:58:39 -0400

        Yes, I agree. Here is what I am trying to achieve: I have a
        database of command automation templates. The templates include
        placeholders for information such as: remote host id, user name,
        path, etc. I also have an XML file that has environment
        information for hundreds of computer environments. The solution
        that I am trying to hammer out first builds the command,
        replacing the place holders with variable names. It then simply
        eval?s the variable names (from the hash in my example) and then
        re-eval?s the command template.

        I guess I will have to give it some more thought. Maybe I can
        skip the variables altogether and simply regex the values into
        the template. Then I would only need to eval the template. I was
        kind of hopping to references though? :)

   Tal

Is this what you had in mind?

        [EMAIL PROTECTED]> cat templating.pl
        #! /usr/bin/perl -w

        use strict;

        my $template = qq(print "Host name is test1, or so I'm told.\\n";);

        my %varHash;
        $varHash{'test1'} = "\$hostnamevar";

        # substitute template reserved words with suitable perl code.
        my $code = join('', map { exists($varHash{$_}) ? $varHash{$_} : $_;
                              } split(/([\w\d]+)/, $template));

        # use the template.
        use vars qw($hostnamevar);
        $hostnamevar = "Hello World\n";
        eval $code;
        [EMAIL PROTECTED]> ./templating.pl 
        Host name is Hello World
        , or so I'm told.
        [EMAIL PROTECTED]> 

Even if so, I still don't understand why you want to use eval.  If the
"command automation templates" are written in Perl, then presumably they
could be written as keyword-style sub calls.  And if they aren't written
in Perl, then how can eval run them?  Or maybe you were just relying on
the eval to do the substitution?  In that case, just do

        #! /usr/bin/perl -w

        use strict;

        my $template = qq(Host name is test1, or so I'm told.\n);

        my %varHash;
        $varHash{'test1'} = "Hello World\n";

        print(join('', map { exists($varHash{$_}) ? $varHash{$_} : $_;
                              } split(/([\w\d]+)/, $template)));

and you're done.

                                        -- Bob Rogers
                                           http://rgrjr.dyndns.org/
 
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to