• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 797
  • Last Modified:

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.
0
deshaw
Asked:
deshaw
1 Solution
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now