Connecting Tech Pros Worldwide Help | Site Map

Need help new to perl code!!!!

Newbie
 
Join Date: Nov 2006
Posts: 6
#1: Nov 27 '06
I am new to perl. I am having trouble with this code line I wrote. On the third line I keep getting this message when I go to run this. This is just the first part of the code. Please any advice would help.I am using TextPad, and window xp pro os.

Usage:C:\Perl\ch1\ filename at C:\Perl\ch1\ line 4, <> line 1.
Press any key to continue . . .

[my $in = $ARGV[0];
unless (defined $in) {
die "Usage: $0 filename";]
Member
 
Join Date: Nov 2006
Posts: 83
#2: Nov 27 '06

re: Need help new to perl code!!!!


Expand|Select|Wrap|Line Numbers
  1. C:\home>type test.pl
  2. my $in = $ARGV[0];
  3. unless (defined $in) {
  4.         die "Usage: $0 filename";
  5. }
  6. print "File: $in\n";
  7.  
  8. C:\home>perl test.pl myfile.txt
  9. File: myfile.txt
  10.  
  11. C:\home>
Newbie
 
Join Date: Nov 2006
Posts: 6
#3: Nov 28 '06

re: Need help new to perl code!!!!


[quote=gooop]I am new to perl. I am having trouble with this code line I wrote. On thef fourth line I keep getting this message when I go to run this. . Please any advice would help.I am using TextPad, and window xp pro os.This program is suppose to make a modified copy of a text file. In the copy ,every string Fred should be replaced with Larry. I want to have the input filename at the command line. But everytime I go to run this I get the error message below on the 4th line of my code. What is wrong with Usage , am I missing a syntax or something.

Usage:C:\Perl\ch1\ filename at C:\Perl\ch1\ line 4, <> line 1.
Press any key to continue . . .

[#!usr/bin/perl -p
my $in = $ARGV[0];
unless (defined $in) {
die "Usage: $0 filename";
}
print "File: $in\n";
my $out = $in;
$out =~ s/(\.\w+)?$/.out/;
unless (open IN, "<$in") {
die "Can't open '$in': $!";
}
unless (open OUT, ">$out") {
die "Can't write '$out': $!";
}
while (<IN>) {
s/Fred/Larry/gi;
print OUT $_;
}]
miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#4: Nov 28 '06

re: Need help new to perl code!!!!


Did you try what GunnarH suggested?
Newbie
 
Join Date: Nov 2006
Posts: 6
#5: Nov 29 '06

re: Need help new to perl code!!!!


Quote:

Originally Posted by miller

Did you try what GunnarH suggested?

Yes I did try this but I still get the error message about line 4. What is wrong with Usage: The only reason I re-posted it was so that I included my entire code because what he wrote on the line after line 4 was the same as mine. The second posting was also more clear on what I am trying to do, that is why I posted it twice.I am very new to programming and I very new to forums.
Newbie
 
Join Date: Nov 2006
Posts: 6
#6: Nov 29 '06

re: Need help new to perl code!!!!


Quote:

Originally Posted by gooop

Yes I did try this but I still get the error message about line 4. What is wrong with Usage: The only reason I re-posted it was so that I included my entire code because what he wrote on the line after line 4 was the same as mine. The second posting was also more clear on what I am trying to do, that is why I posted it twice.I am very new to programming and I very new to forums.


I used the use diagnostics; in my code and it it says uncaught exception by the user, same line 4.
Member
 
Join Date: Nov 2006
Posts: 83
#7: Nov 29 '06

re: Need help new to perl code!!!!


Quote:

Originally Posted by gooop

Quote:

Originally Posted by miller

Did you try what GunnarH suggested?

Yes I did try this but I still get the error message about line 4.

When doing so, did you start the command with perl?
Newbie
 
Join Date: Nov 2006
Posts: 6
#8: Dec 3 '06

re: Need help new to perl code!!!!


Yes , beleive so i used
Expand|Select|Wrap|Line Numbers
  1.  #!/user /bin/perl -p 
As the first line of my code
Member
 
Join Date: Nov 2006
Posts: 83
#9: Dec 4 '06

re: Need help new to perl code!!!!


Quote:

Originally Posted by gooop

Quote:

Originally Posted by GunnarH

When doing so, did you start the command with perl?

Yes , beleive so i used
Expand|Select|Wrap|Line Numbers
  1.  #!/user /bin/perl -p 
As the first line of my code

I wasn't talking about the shebang line, I was talking about the command, i.e. what you are typing on the command line when invoking the script.
Expand|Select|Wrap|Line Numbers
  1. perl test.pl myfile.txt
  2. ^^^^
Did you start that typing with the word "perl"?

As regards the shebang line, I'd recommend that you drop it. Besides the switches, it's redundant on Windows, and I'm not sure what that -p switch does.
Reply