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

From the main block of my code, I'd like to pass a formating string to a sub to be filled in with it's private (my) variables.

sub test { my %arguements = %{ $_[0] }; my $otherstuff

Replies are listed 'Best First'.
Re: Late evaluation of a string.
by btrott (Parson) on Mar 22, 2000 at 22:52 UTC
    That's not *all* that helpful--perhaps you forgot to post part of this? But, if I'm guessing correctly as to what you're trying to do, perhaps you should investigate eval. You could specify your format in a single-quoted string (containing a double-quoted string), then eval it.

    Perhaps you might also want to look at Text::Template, since it sounds like that might be sort of what you want? I'd recommend this latter approach if it fits w/ what you're trying to do.

Re: Late evaluation of a string.
by jbert (Priest) on Mar 23, 2000 at 16:07 UTC
    If you mean you currently format things with:
    $result = "And the answer is $var1 (or $var2 percent)";
    and you want to pass in a format string, look at the 'sprintf' function instead. You can then do things like:
    printit( "The answer is %s (or %d percent)" ); sub printit { $format = shift @_; # Passed in to function ...set variables... $result = sprintf( $format, $var1, $var2 ); }