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

hi,

here is my code
sub showAvman(){ $dbh = DBI->connect ("dbi:Pg:dbname=NPMS"); $res=$dbh->prepare("select \"name\", \"address\", \"type\", \"pollint\", \"protocol\" from \"avman\" order by \"name\""); $res->execute; format STDOUT_TOP= Name IP Address Type Pollint Pr +otocol . format STDOUT= @<<<<<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<< @ +<<<<<<<<<<<< $row->{name}, $row->{address},$row->{type} ,$row->{pollint},$r +ow->{protocol} . while ($row=$res->fetchrow_hashref()){ Name IP Address Type Pollint Pr +otocol . format STDOUT= @<<<<<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<< @ +<<<<<<<<<<<< $row->{name}, $row->{address},$row->{type} ,$row->{pollint},$r +ow->{protocol} . while ($row=$res->fetchrow_hashref()){ if ($row->{type} eq "SNMP"){ @_=split(/:/,$row->{protocol}); $row->{protocol} = "Interface $_[1]"; } \"address\", \"type\", \"pollint\", \"protocol\" from \"avman\" order by \"name\""); $res->execute; format STDOUT_TOP= Name IP Address Type Pollint Pr +otocol . format STDOUT= @<<<<<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<< @ +<<<<<<<<<<<< $row->{name}, $row->{address},$row->{type} ,$row->{pollint},$r +ow->{protocol} . while ($row=$res->fetchrow_hashref()){ if ($row->{type} eq "SNMP"){ @_=split(/:/,$row->{protocol}); $row->{protocol} = "Interface $_[1]"; } write STDOUT; } $res->finish; $dbh->disconnect; } sub showEvents(){ $var=NPMS::getGlobals(); $maxEvent=$var->{maxevent}; $dbh = DBI->connect ("dbi:Pg:dbname=NPMS"); $res=$dbh->prepare("select t.\"status\", to_char(l.\"time\",\'Dy DD Mon YYYY HH12:MI am\') as t +ime, l.\"extra_text\", t.\"desc\", l.\"userid\", l.\"device\", l.\"number\" from \"events\" t, \"event_log\" l where t.\"id\" = l.\"id\" and l.\"ack\" = \'0\' order by \"number\" DESC limit $maxEvent"); if (!defined $res){ die "Cannot prepare statement\n"; } $res->execute; format STDOUT_TOP= STATUS USERID TIME DEVICE DESC . format STDOUT= @<<<<<<<<<<<< @<<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<< +<<<<< $row->{status},$row->{userid},$row->{device},$row->{desc} . while ($row=$res->fetchrow_hashref()){ write STDOUT; # print "......$row->{status}......$row->{userid}......$row->{de +vice}......$row->{desc}\n"; } $res->finish; $dbh->disconnect; }
now i have got two different sub routines each of which is supposed to churn out some data back to the screen and format it too.

but i cant format STDOUT twice in the same perl script.

what is the way out? like i wan ever sub routine to print out formatted output to the screen. but the error i am getting is that STDOUT and STDOUT_TOP is formatted again at lines 168 and 175.

how do i circumvent this problem?

Readmore tags added and Formatting fixed by holli

Replies are listed 'Best First'.
Re: formatting STDOUT more than once
by artist (Parson) on Jul 04, 2005 at 04:54 UTC
    Look for Format Variables in perldoc perlform

    An excerpt : These variables are set on a per-filehandle basis, so you'll need to select() into a different one to affect them:

    --Artist

      i read the article on this link
      http://url.to.pirated.site.removed/orelly/perl/prog3/ch07_01.htm
      but didnt make much sense to me.
      can u explain with the help of little code.

      Janitored by davido: Obliterated link to pirated content per consideration.

Re: formatting STDOUT more than once
by tlm (Prior) on Jul 04, 2005 at 14:33 UTC

    I preface this by noting that I know very little about formats, so what follows could be completely wrong.

    AFAICT, the question you ask about should be the least of your worries. The code you posted doesn't even compile. (It looks like one big cut-and-paste charlie foxtrot.) It has numerous other problems, IMO, but I'll confine myself to your question.

    I've edited your code minimally to do what I think you want it to do (i.e. I've retained stuff in the original, such as the abuse of @_, that I'd never do in my own code). Even if I didn't get it right, it should give you some idea of what you should do. The code is untested.

    the lowliest monk