How to perform dynamic formatting with perl during write?

Posted by Bee on Stack Overflow See other posts from Stack Overflow or by Bee
Published on 2011-02-03T07:13:46Z Indexed on 2011/02/03 7:25 UTC
Read the original article Hit count: 126

Filed under:

I have a format which is defined like below:

format STDOUT =
------------------------------------
|Field1      | Field2     | Field3 |
------------------------------------
|@<<<<<<<<<<| @<<<<<<<<<<<| @<<<<< |~~
shift(@list1),shift(@list2),shift(@list3)
------------------------------------
.
write STDOUT;

So the questions are as below:

  1. Is it possible to make the list of values printed dynamic? e.g. If list 1 contains 12 elements, and if $flag1 is defined, then print only elements 0..10 instead of all 12. I tried doing this by passing $flag as a parameter to the sub which generates the report. However, the last defined FORMAT seems to always take precedence and the final write when it happens, applies the last format no matter what the condition is.
  2. Is it possible to also add/hide fields using the same process. e.g. If $flag2 is defined, then add an additional field Field4 to the list?

© Stack Overflow or respective owner

Related posts about perl