mmazlan67 has asked for the wisdom of the Perl Monks concerning the following question:
Hi, i am working on a simple perl script to grab 3 variables based on a textfile. I am using regex. I tried to catch the word after the match string for the first variable, it works fine. But when i tried on the second variables, it appears error: Use of uninitialized value in print at enhancement.pl line 31, <$readhandle> line 4. I tried to put no warnings 'uninitialized';, but still the second output did not appear as expected. From the output file, only the name which is blocka appear whereas the path did not. Below are my code and the text file used.
#! /tools/perl/5.8.8/linux/bin/perl use strict; use warnings; print "Enter the name of the file to read: "; my $filetoread = <STDIN>; chomp($filetoread); print "Enter the name of the file to write: "; my $filetowrite = <STDIN>; chomp($filetowrite); open my $readhandle, "<", $filetoread or die "Unable to read '$filetor +ead'"; open my $writehandle, ">", $filetowrite or die "Unable to write '$file +towrite'"; my $name; my $path; while (<$readhandle>) { if ($_=~ m/^Name=(\S+)/) { $name = $1; print $writehandle $name; } if ($_=~ m/^Path=(\S+)/) { $path = $2; print $writehandle $path; } } close $readhandle; close $writehandle;
This is the textfile used
Name=blocka Path=/main/sub/a/bg/adee/swarfrw/ewqsx/fewaxr/akeil/awedr/date/file
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: error: Use of uninitialized value in print at enhancement.pl line 31, <$readhandle> line 4.
by hippo (Archbishop) on Jul 20, 2017 at 09:05 UTC | |
|
Re: error: Use of uninitialized value in print at enhancement.pl line 31, <$readhandle> line 4.
by thanos1983 (Parson) on Jul 20, 2017 at 11:28 UTC |