Question

I'm trying to execute this simple code but it's already give me the following error :

Use of uninitialized value $string in string at permutation.pl line 11.

#!usr/bin/perl

use strict ;
use warnings ;



sub message {
my ($string) = @_ ;
print "$string" ;
    }
message() ;
Was it helpful?

Solution

You didn't pass any arguments to message, so its @_ is empty, so you assigned undef to $string, which results in that warning when $string is stringified.

Fix:

message("Hello, world\n");

OTHER TIPS

Make sure that you pass non undef value to the function (passing no parameter at all also falls into this category), or make default value for input parameter. //= checks if variable is undef and if it is, sets right value to it (an empty string).

#!usr/bin/perl

use strict;
use warnings;


sub message {
    my ($string) = @_ ;
    $string //= "";
    print "$string" ;
}
message() ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top