From: [email protected]
[mailto:[email protected]] On Behalf Of
Perl Help
Sent: 31 March 2009 23:33
To: Sisyphus
Cc: [email protected]
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);
> }
> ############3 END ################
>
> I tried to delete the handle to the console but then I do not get any
output at all.
> Also, in actual world my sub ErrorMsg is defined in a .pm file and
this function is called by multiple scripts > and I can not get handle
to win32 console in script and pass it to the module due to some project
restrictions. > ALso, this error happens because I am trying to call my
$console = Win32::Console->new(STD_OUTPUT_HANDLE)
> twice in the same script. Is there a way I can still call it twice in
the same script?
>
> Any help is much appreciated.
It certainly looks like it causes problems if you create a second
instance of Win32::Console. If there is nothing in the documentation
about this being a 'feature', it might be worth raising it with the
module's maintainers.
As you say your ErrorMsg sub is in a separate module, it will be fairly
easy to keep the Win32::Console in a package scope variable that is only
initialised once. For example:
use strict;
use warnings;
package MyErrorModule;
use base qw{Exporter};
use Win32::Console;
use Carp;
our @EXPORT = qw{ErrorMsg};
our $console;
sub ErrorMsg {
my $error = shift;
$console = Win32::Console->new(STD_OUTPUT_HANDLE)
unless defined $console;
defined $console
or croak "Failed to get console: $!\n";
my $CurrentConsoleColor = $console->Attr;
defined $CurrentConsoleColor
or croak "Failed to get current console colour: $!\n";
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);
}
1;
HTH
--
Brian Raven
This e-mail may contain confidential and/or privileged information. If you are
not the intended recipient or have received this e-mail in error, please advise
the sender immediately by reply e-mail and delete this message and any
attachments without retaining a copy.
Any unauthorised copying, disclosure or distribution of the material in this
e-mail is strictly forbidden.
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs