A
Andrew
If I start with the following code;
use strict;
use warnings;
my $message = "Hello";
displayPrint();
sub displayPrint {
print "The message is $message.\n";
}
When I run it, I get the following output;
The message is Hello.
I would like to loop through several messages, so I have changed the
code to;
my $message;
my @messages = ("Hello", "Goodbye");
foreach $message (@messages) {
displayPrint();
}
sub displayPrint {
print "My message is $message.\n";
}
Then I get the following error message from the print line;
"Use of uninitialized value in concatenation (.) or string"
Can someone explain this to me ? From my understanding (obviously
wrong), $message is 'declared' at the start of the code so it should
be available for use within displayPrint. If I put an extra print
statement inside the foreach loop but before the branch to
displayPrint, the right value is displayed. Why isnt the value of
$message pass into displayPrint ?
Thanks for any help you can give.
use strict;
use warnings;
my $message = "Hello";
displayPrint();
sub displayPrint {
print "The message is $message.\n";
}
When I run it, I get the following output;
The message is Hello.
I would like to loop through several messages, so I have changed the
code to;
my $message;
my @messages = ("Hello", "Goodbye");
foreach $message (@messages) {
displayPrint();
}
sub displayPrint {
print "My message is $message.\n";
}
Then I get the following error message from the print line;
"Use of uninitialized value in concatenation (.) or string"
Can someone explain this to me ? From my understanding (obviously
wrong), $message is 'declared' at the start of the code so it should
be available for use within displayPrint. If I put an extra print
statement inside the foreach loop but before the branch to
displayPrint, the right value is displayed. Why isnt the value of
$message pass into displayPrint ?
Thanks for any help you can give.