By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,812 Members | 1,978 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,812 IT Pros & Developers. It's quick & easy.

Problem in code

P: 4
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
Share this Question
Share on Google+
6 Replies


Ganon11
Expert 2.5K+
P: 3,652
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 2.5K+
P: 4,059
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 2.5K+
P: 4,059
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 2.5K+
P: 3,652
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 2.5K+
P: 4,059
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 2.5K+
P: 3,652
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.