How to declare Perl format for dynamically changing in variable length

Hi Experts,

Currently we are writing Perl script where we need to use Perl format for displaying output in particular format. We don't want to use sprintf as it is errorprone and want to use only Perl format. We are facing below problem:

Suppose I want to define my Perl format then I am using below code:

my ( $len1, $len2, $len3, $len4 ) = (0,0,0,0);
my ( $var1, $var2, $var3, $var4 ) = ( );
#We have to declare above variables before declaring format
my $srcheader = "format SRCHEADER = \n" .
            '@' . '>' x $len1 . " " . '@' . '<' x $len2 . ": " . '@' . '<' x $len3 . '@' . '<' x $len4 . "\n" .
            '$var1,                  $var2,                      $var3,              $var4' . "\n" .
            ".\n";

Where we $len1 to $len4 to be dynamically change in program so that when we execute below code, we should get desire output:

      $len1 = 45 if ( $ALLSTATS );
      $len2 = length($config);
      $len3 = length($SRC_COUNT{$config});
      my $srcinfo = " ";
      $srcinfo = "(Source : $SYNC_SRC)" unless ( $OLD_LOGS );
      $len4 = length($srcinfo);
      print "( $len1, $len2, $len3, $len4 )\n";
      $var1 = "Configuration";
      $var2 = $config;
      $var3 = $SRC_COUNT{$config};
      $var4 = $srcinfo;
      
                     eval $srcheader;
                     die $@ if $@;
                      select(STDOUT);
                      $~ = $srcheader;
                      write();

Here problem is : though we have updated $len1 to $len4 values it is taking the first defined values (0,0,0,0) but it is working fine for $var1 to $var4. Could any one tell what is the problem.

Thanks.
LVL 1
deshawAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
sub srcheader{
print "format SRCHEADER = \n".
            '@' . '>' x $len1 . " " . '@' . '<' x $len2 . ": " . '@' . '<' x $len3 . '@' . '<' x $len4 . \
"\n" .
            '$var1,                  $var2,                      $var3,              $var4' . "\n" .
            ".\n";
eval "format SRCHEADER = \n".
            '@' . '>' x $len1 . " " . '@' . '<' x $len2 . ": " . '@' . '<' x $len3 . '@' . '<' x $len4 . \
"\n" .
            '$var1,                  $var2,                      $var3,              $var4' . "\n" .
            ".\n";
}
srcheader();
select(STDOUT);
$~ = "SRCHEADER";
write();
0
deshawAuthor Commented:
Thanks for the reply but that wont work for me  as I have to call SRCHEADER many times and both $len and $var variables are dynamic. I came out with below solution:

        $len1 = 26;
        $len1 = 45 if ( $ALLSTATS );
        $len2 = length($config);
        $len3 = length($SRC_COUNT{$config});
        my $srcinfo = " ";
        $srcinfo = "(Source : $SYNC_SRC)" unless ( $OLD_LOGS );
        $len4 = length($srcinfo);
        my $SRCHEADER = "SRCHEADER" . $i++;
        my $srcheader = "format $SRCHEADER = \n\n" .
            '@' . '>' x $len1 . " " . '@' . '<' x $len2 . ": " . '@' . '<' x $len3 . '@' . '<' x $len4 . "\n" .
            '"Configuration",         $config,                   $SRC_COUNT{$config}, $srcinfo' . "\n\n" .
            ".\n";
        eval $srcheader;
        My_Die( __LINE__, "ERROR:", $@ ) if $@;
        mywrite_stdout_log($SRCHEADER, @FHS);

I have used $i variable that will be increamented and appended to format name so that there wont be any problems.

Thanks.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.