in reply to error: Use of uninitialized value in print at enhancement.pl line 31, <$readhandle> line 4.
Hello mmazlan67,
Well fellow monk hippo provided you with the answer to your problem already, but I would like to point out some things to your code just for your knowledge.
Let's start. When you use die always use $! why? (from documentation)
If an uncaught exception results in interpreter exit, the exit code is + determined from the values of $! and $?
Moving on, when reading the file while (<$readhandle>) I would suggest always chomp $_; each line. The most important affect of chomp is that removes the new line characters \n it can produce problems or not desired output. Next step is that when you are using the general variable $_ you do not need to define it, Perl does this for you. For example, you do not need to define it like this chomp $_; you can do it straight like chomp;, same thing applies also on the if ($_=~ m/^Name=(\S+)/) you can do it straight like if (m/^Name=(\S+)/).
Moving on, I noticed on your first if condition you define the regex without a space after the equal sign if ($_=~ m/^Name=(\S+)/) but on your input data file you have a space, so which one is the correct?
Last thing that I noticed is that when you are writing on your file your are overwiriting it, if you want to appent it you need to use >>.
I would also recommend to use the say. It automatically appends at the end of the string a new line character \n so you do not to be bothered to enter it when you are debugging or writing to a file.
Any way based on my recommendations and correct solution provided by the fellow monk hippo here is a working version of your code.
#!/usr/bin/perl use strict; use warnings; use feature 'say'; say "Enter the name of the file to read: "; my $filetoread = <STDIN>; chomp($filetoread); say "Enter the name of the file to write: "; my $filetowrite = <STDIN>; chomp($filetowrite); open my $readhandle, "<", $filetoread or die "Unable to read '$filetoread': $!"; open my $writehandle, ">>", $filetowrite or die "Unable to write '$filetowrite':$!"; while (<$readhandle>){ chomp; if (m/^Name=(\S+)/) { say $writehandle $1; } if (m/^Path=(\S+)/) { say $writehandle $1; } } close $readhandle or warn "Unable to close ".$filetoread.": $!"; close $writehandle or warn "Unable to close ".$filetowrite.": $!"; __END__ $ cat out.txt blocka /main/sub/a/bg/adee/swarfrw/ewqsx/fewaxr/akeil/awedr/date/file
Hope this helps, BR.
|
|---|