----- Original Message ----- 
From: "Perl Help" <[email protected]>
To: "Sisyphus" <[email protected]>
Cc: <[email protected]>
Sent: Wednesday, April 01, 2009 9:32 AM
Subject: Re: Reset in ANSIColor sets the background to black


> Hi Rob,
>
> I am facing another problem with this code. When I call the function twice
> in the same script, it throws me the following error:
>
> Error Msg: Test[This is in Red color as expected for the first function
> call]
> Use of uninitialized value in bitwise and (&) at test.pl line 12.
>
> My Script is :
>
> #########33 START #########3
> use strict;
> use warnings;
> use Win32::Console;
>
> ErrorMsg('Test');
> ErrorMsg('Test');
>
> sub ErrorMsg {
>   my $error = shift;
>   my $console = Win32::Console->new(STD_OUTPUT_HANDLE);
>   my $CurrentConsoleColor = $console->Attr;
>   my $BackgroundColor = $CurrentConsoleColor &
> (BACKGROUND_RED|BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_INTENSITY);
>   # This sets the text color on DOS in red with intensity
>   $console->Attr(FOREGROUND_RED|FOREGROUND_INTENSITY|$BackgroundColor);
>   print "\nError Msg: $error \n";
>   $console->Attr($CurrentConsoleColor);
> }

Hi Jai,
One solution is to create and assign to $console outside of the subroutine 
(and before the subroutine is called). This works fine for me:

#################################
use strict;
use warnings;
use Win32::Console;

my $console = Win32::Console->new(STD_OUTPUT_HANDLE);

ErrorMsg('Test');
ErrorMsg('Test again');
ErrorMsg(' ... and again');

sub ErrorMsg {
   my $error = shift;
   my $CurrentConsoleColor = $console->Attr;
   my $BackgroundColor = $CurrentConsoleColor &
(BACKGROUND_RED|BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_INTENSITY);
   # This sets the text color on DOS in red with intensity
   $console->Attr(FOREGROUND_RED|FOREGROUND_INTENSITY|$BackgroundColor);
   print "\nError Msg: $error \n";
   $console->Attr($CurrentConsoleColor);
}
#################################

Cheers,
Rob 

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to