srikdatta has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm a Perl newbie, trying to implement it in one of my projects and I have "Use of uninitialized value $_ in pattern match (m//)" error while executing the following code:

#!/tool/pandora64/bin/perl5.8.8 -w use strict; use warnings; my @verilogfiles = glob '*.verilog'; my ($vf, $of, $i, $j); print "Define the path for sdf_annotate.v: "; my $path = <STDIN>; chomp($path); #For verilog files foreach my $b (@verilogfiles) { open $vf, '>>', $b or die "Could not open file '@verilogfiles' + $!"; if($_ =~ /module entity\;/) { print $vf "\`include \"$path\"\n"; } } close ($vf);
Output is:

Define the path for sdf_annotate.v: rwfg

Use of uninitialized value $_ in pattern match (m//) at opvrmod.pl line 38, <STDIN> line 1. Use of uninitialized value $_ in pattern match (m//) at opvrmod.pl line 38, <STDIN> line 1. Use of uninitialized value $_ in pattern match (m//) at opvrmod.pl line 38, <STDIN> line 1. Use of uninitialized value $_ in pattern match (m//) at opvrmod.pl line 38, <STDIN> line 1. Use of uninitialized value $_ in pattern match (m//) at opvrmod.pl line 38, <STDIN> line 1. Use of uninitialized value $_ in pattern match (m//) at opvrmod.pl line 38, <STDIN> line 1.

I tried the solution suggested in this website. But none worked. Any help?

Replies are listed 'Best First'.
Re: Use of uninitialized value $_ in pattern match (m//)
by roboticus (Chancellor) on Oct 31, 2017 at 19:24 UTC

    srikdatta:

    You were close, but you're getting the error because you haven't done anything to put a value in $_. It appears like you're wanting to scan through a file, so you'd do something like this:

    #!/tool/pandora64/bin/perl5.8.8 -w use strict; use warnings; my @verilogfiles = glob '*.verilog'; my ($vf, $of, $i, $j); print "Define the path for sdf_annotate.v: "; my $path = <STDIN>; chomp($path); #For verilog files foreach my $b (@verilogfiles) { open $vf, '>>', $b or die "Could not open file '@verilogfiles' +$!"; while (<$vf>) { if($_ =~ /module entity\;/) { print $vf "\`include \"$path\"\n"; } } } close ($vf);

    The while (<$vf>) { loop will load a line from $vf into $_ and execute the loop body, and repeat until there are no more lines in the file.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Use of uninitialized value $_ in pattern match (m//)
by 1nickt (Canon) on Oct 31, 2017 at 19:37 UTC

    Hi,

    open $vf, '>>', $b
    This opens in the file in append mode, so when you do
    while (<$vf>) { if($_ =~ /module entity\;/)
    (after adding the while (<$FH>) { ... } construct as suggested by syphilis), there are no lines read; the loop is never entered.

    See open.

    You should open your file in read/write mode, if the intent of your program is indeed as it appears.

    open $vf, '+<', $b or die $!;

    (Note: I can't make sense of what you are trying to accomplish. You read in a path from the user and then repeatedly print that path to a verilog file, once for each existing line in the file that matches a certain string. Is that really your intent?)

    Hope this helps!

    update: added link to doc and example


    The way forward always starts with a minimal test.