467,104 Members | 1,026 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,104 developers. It's quick & easy.

Problem in code

Hi I am very new to PERL i have written code as :
if (open(PROGRAM_1, "C:\program_1")) {
print ( "working");
}
for opening the simple txt file but it is showing error as
"Name "main::PROGRAM_1" used only once: possible typo at Untitled1 line 3.

Please do help me.i am using perlexpress on windows environment.
Sep 4 '08 #1
  • viewed: 1097
Share:
6 Replies
Ganon11
Expert 2GB
Hi I am very new to PERL i have written code as :
if (open(PROGRAM_1, "C:\program_1")) {
print ( "working");
}
for opening the simple txt file but it is showing error as
"Name "main::PROGRAM_1" used only once: possible typo at Untitled1 line 3.

Please do help me.i am using perlexpress on windows environment.
Your method of calling open is deprecated - that is, you should be using a scalar filehandle variable instead of a bareword filehandle. If your program had the strict and warnings pragmas enables (as it always should), you would be getting complaints. In addition, you are not telling open how you want to open the file - for input? output? Finally, the '\' character is the escape character, so the open function thinks you have a special character '\p' in the string. Either backslash your backslash '\\' or use single quotes so that the backslash isn't interpolated.

Your code should look like this:

Expand|Select|Wrap|Line Numbers
  1. if (open my $PROGRAM_1, '<', 'C:\program_1') { # '<' assuming you want to open for input
  2.    print "Working.\n";
  3. }
Sep 4 '08 #2
KevinADC
Expert 2GB
Your method of calling open is deprecated - that is, you should be using a scalar filehandle variable instead of a bareword filehandle. If your program had the strict and warnings pragmas enables (as it always should), you would be getting complaints. In addition, you are not telling open how you want to open the file - for input? output? Finally, the '\' character is the escape character, so the open function thinks you have a special character '\p' in the string. Either backslash your backslash '\\' or use single quotes so that the backslash isn't interpolated.

Your code should look like this:

Expand|Select|Wrap|Line Numbers
  1. if (open my $PROGRAM_1, '<', 'C:\program_1') { # '<' assuming you want to open for input
  2.    print "Working.\n";
  3. }

The default open is read onlyif no file open operator is used. "<" is read only, not input, which would be ">>" or ">". Your code snippet also did not include the file handle in the print line. Here are some more samples:

Expand|Select|Wrap|Line Numbers
  1. # open for reading
  2. open (my $PROGRAM_1, '<', 'C:\program_1') or die "Can't open C:\program_1: $!";
  3. while (<$PROGRAM_1>) {
  4.    print;
  5. }
  6. close $PROGRAM_1;
  7.  
  8. # open for over-write or create
  9. open (my $PROGRAM_1, '>', 'C:\program_1') or die "Can't open C:\program_1: $!";
  10. print $PROGRAM_1 "hello there\n";
  11. close $PROGRAM_1;
  12.  
  13. # open for appending or create
  14. open (my $PROGRAM_1, '>>', 'C:\program_1') or die "Can't open C:\program_1: $!";
  15. print $PROGRAM_1 "hello there\n";
  16. close $PROGRAM_1;    
  17.  
any good tutorial covers file I/O, which is very basic to perl.
Sep 4 '08 #3
KevinADC
Expert 2GB
As a side-note, bareword filehandles are not deprecated, but using a lexical filehandle is safer when you can use it. The older style bareword filehandles are global in context which can lead to problems, the "my" declared lexical filehandles are scoped to the block/file, which is safer, but the perl coder must also be aware of that. Also using the three argument version of open() is better than the older two argument style.
Sep 4 '08 #4
Ganon11
Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. if (open my $PROGRAM_1, '<', 'C:\program_1') { # '<' assuming you want to open for input
  2.    print "Working.\n";
  3. }
By input here, I meant taking data from the file and 'inputting' to the program. I guess I should have been clearer in saying '<' meant reading.
Sep 4 '08 #5
KevinADC
Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. if (open my $PROGRAM_1, '<', 'C:\program_1') { # '<' assuming you want to open for input
  2.    print "Working.\n";
  3. }
By input here, I meant taking data from the file and 'inputting' to the program. I guess I should have been clearer in saying '<' meant reading.

Ahh.... my bad mate.
Sep 4 '08 #6
Ganon11
Expert 2GB
Ahh.... my bad mate.
No, my bad for being ambiguous.
Sep 4 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Kostatus | last post: by
7 posts views Thread by Keith Dewell | last post: by
28 posts views Thread by Jon Davis | last post: by
6 posts views Thread by Ammar | last post: by
2 posts views Thread by Mike Collins | last post: by
6 posts views Thread by TPJ | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.