supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:
Hi PerlMonks,
I am interested in uploading a text file which has been just created
on desktop to save results. For this I have written a script m1.pl given below.
The cmd is showing error "Use of uninitialized value $new". I am at my
wit's end to solve this problem. Please help me.
Here goes the script m1.pl:
#!/usr/bin/perl
use warnings;
##################################
# Output to a text file:
##################################
$output1="j.txt";
open (my $fh1,">",$output1) or die"Cannot open file
'$output1'.\n";
##################################
$a="John is"; $b="a good boy";
$join=$a.$b; # Concatenate
print "\n Complete sentence:\n
$join\n\n";
print $fh1 "\n Complete sentence:\n
$join\n\n";
#######################################
# To input textfile j.txt from desktop:
#######################################
open FILE,"j.txt" or die "Can't open file:$!";
while (<FILE>) {
$new .=$_;}
close FILE;
print "\n Uploaded file content:\n
$new\n\n";
exit;
#########################
Incorrect results of cmd:
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\x>cd desktop
C:\Users\x\Desktop>m1.pl
Complete sentence:
John isa good boy
Use of uninitialized value $new in concatenation (.) or string at C:\U
+sers\x\Desktop\m1.pl line 30.
Uploaded file content:
Re: How can one input a textfile from desktop just created?
by NetWallah (Canon) on Mar 08, 2014 at 05:02 UTC
|
Your specific problem : "Use of uninitialized value $new" can be resolved by declaring
my $new = "";
BEFORE the loop where you concatenate into $new.
You would have discovered the missing declaration if you had "use strict";
THe alternative to concatanation is to the "slurp" the file - plenty of references at this site on how that is done.
What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
-Larry Wall, 1992
| [reply] [d/l] |
|
| [reply] |
Re: How can one input a textfile from desktop just created?
by Athanasius (Archbishop) on Mar 08, 2014 at 04:46 UTC
|
You need to close $fh1 before re-opening the file “j.txt” for reading. Or, alternatively, open the file just once, for both writing and reading:
...
open (my $fh1, "+>", $output1) or die "Cannot open file '$output1': $!
+";
...
seek $fh1, 0, 0;
while (<$fh1>) {
...
(But — why don’t you use strict; ??)
Update: To clarify: the problem arises because the filehandle FILE is positioned at EOF (end-of-file), so the first call to <FILE> returns undef and the body of the while loop is never entered. Calling close $fh1; before re-opening the file ensures that FILE is initially positioned at the beginning of the file rather than the end. Calling seek $fh1, 0, 0; achieves the same result without the overhead of re-opening the file.
Hope that helps,
| [reply] [d/l] [select] |
|
Hi Athanasius,
Thanks for your suggestions. I have got desired result after using seek $fh1, 0, 0; as suggested by you.
I avoid using "use strict;" in the script because "my" has to placed before every scalar and array variable. Moreover, I have noticed that "use strict;" sometimes does not give me the correct result from my script and the cmd shows a large number of warnings. Without the "use strict;" line in my script, I often get the desired result. That is why I avoid "use strict;" although I know that it detects the flaws in a script.
I have to learn more about "use strict;".
With regards,
| [reply] |
|
Hi supriyoch_2008,
A Perl programmer developing a script without use strict is a bit like a trapeze artist practising a new trick without a safety net. In other words, it’s a Bad Idea (unless you enjoy all-night debugging sessions). But it’s your call.
However, I think you may have some misconceptions about what strict does:
I have noticed that "use strict;" sometimes does not give me the correct result from my script and the cmd shows a large number of warnings. Without the "use strict;" line in my script, I often get the desired result.
This is simply not possible! First, use strict never issues warnings, it generates errors which cause the script to abort immediately they occur. Second, if the script runs without errors, then the presence of use strict does not change the way it runs in any way.
use strict is actually three pragmata rolled into one:
-
use strict 'refs';
This prevents you from using symbolic references, and since you probably don’t use them anyway, it won’t have much impact on your code.
-
use strict 'subs';
Consider the difference between these snippets:
18:18 >perl -E "sub foo { return 'bar'; } $y = foo; say $y;"
bar
18:19 >perl -E "$y = foo; sub foo { return 'bar'; } say $y;"
foo
18:20 >
In the second, “foo” is treated as a string, not a subroutine name, so the output is not what you wanted. use strict 'subs' prevents this by forcing you to either declare your subroutines before calling them, or call them explicitly with parentheses. This is a Good Thing.
-
use strict 'vars';
This is the one that makes you declare each variable with my (or our) the first time it is used. More typing? Sure, but think how much debugging time it will save you down the track when you have a variable named opensesame which you later write as openseseme by mistake!
See strict and then recite The strictures, according to Seuss as needed. :-)
Hope that helps,
| [reply] [d/l] [select] |
|
I avoid using "use strict;" in the script because "my" has to placed before every scalar and array variable. Moreover, I have noticed that "use strict;" sometimes does not give me the correct result from my script and the cmd shows a large number of warnings. Without the "use strict;" line in my script, I often get the desired result. That is why I avoid "use strict;" although I know that it detects the flaws in a script.
This is a tragic mistake. Any sane Perl programmer is always using use strict; for any program which is more than two lines. And your post is just a perfect example of it: with the use strict; pragma turned on, you would have known of your problem at compile time, even before your program started to run. And having to use my is really not a nuisance, but an excellent opportunity: to start with, the compiler will tell you about many of your mistakes (including simple typos) which might otherwise take you hours to track down. When you will know more Perl, you will also appreciate the capability of mastering the scope of a variable. And, no, using strictures will not change the result of your program, it will abort your program if it does not respect rules that you should really follow. Very seasoned programmers sometimes (rarely) turn off one of the strictures for a very limited part of the program to enable the use of a very special construct (magics), but it is usually (hopefully) when they really know what they are doing. Don't try to do it until you are really an expert. For the time being, always enable strictures, you will save a considerable amount od debugging time.
| [reply] [d/l] [select] |
Re: How can one input a textfile from desktop just created?
by ww (Archbishop) on Mar 08, 2014 at 12:34 UTC
|
| [reply] [d/l] |
|
|