views:

115

answers:

2

Is there a way to underline the text is a perl output script? I have read from several sources but the text in the scripts can't be underlined.

An error outputs: Global symbol "$finalAddition" requires explicit package name at C:\Documents and Settings\PCS\Desktop\Perl Scripts\script.pl line 7.

The Script Codes:

#!/usr/bin/perl

use warnings;
use strict;
use Term::ANSIColor;

$finalAddition = 8;

print "\n\nThe Final Number after addtion would be ".coloured($finalAddition, 'bold 
underline');

Please give some advice on this. Thanks.

+4  A: 

This might be to do with variable scoping and having enabled strict mode, rather than what you are trying to achieve. Does changing adding a "my" to the code change anything?

#!/usr/bin/perl

use warnings;
use strict;
use Term::ANSIColor;

my $finalAddition = 8;

print "\n\nThe Final Number after addition would be " .
      colored($finalAddition, 'bold underline');
William Rose
Part of your answer is right. I was missing the "my" but even after fixing that the under still didn't appear. Refer to my answer below to the question.
JavaNoob
The other part is that [Term::ANSIColor](http://search.cpan.org/perldoc?Term::ANSIColor) was written by an American, and the function is spelled `colored`, not `coloured`. I've fixed it in your answer.
cjm
A: 

After rounds of testing and almost smashing the screen, the answer was pretty simple actually... [EDIT] New and better codes!

#!/usr/bin/perl

use warnings;
use strict;
use Term::ANSIColor;

my $totalinput = $userinput * $userinput2;

my $coloredText = colored($totalinput, 'bold underline blue');

print "\n\nThe final answer to the question is: $coloredText\n\n";

Thanks for the code advices!

JavaNoob
While this works, I think the `colored` function is more readable. Your problem (aside from the missing `my`) was that you used the British spelling instead of the American one.
cjm
Yup I changed my codes to colored($text, 'bold underline blue') which can be made into a variable and be printed easily.
JavaNoob