Need help new to perl code!!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gooop
    New Member
    • Nov 2006
    • 6

    Need help new to perl code!!!!

    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\c h1\ 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";]
  • GunnarH
    New Member
    • Nov 2006
    • 83

    #2
    Code:
    C:\home>type test.pl
    my $in = $ARGV[0];
    unless (defined $in) {
            die "Usage: $0 filename";
    }
    print "File: $in\n";
    
    C:\home>perl test.pl myfile.txt
    File: myfile.txt
    
    C:\home>

    Comment

    • gooop
      New Member
      • Nov 2006
      • 6

      #3
      [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\c h1\ 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 $_;
      }]

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        Did you try what GunnarH suggested?

        Comment

        • gooop
          New Member
          • Nov 2006
          • 6

          #5
          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.

          Comment

          • gooop
            New Member
            • Nov 2006
            • 6

            #6
            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.

            Comment

            • GunnarH
              New Member
              • Nov 2006
              • 83

              #7
              Originally posted by gooop
              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?

              Comment

              • gooop
                New Member
                • Nov 2006
                • 6

                #8
                Yes , beleive so i used
                Code:
                 #!/user /bin/perl -p
                As the first line of my code

                Comment

                • GunnarH
                  New Member
                  • Nov 2006
                  • 83

                  #9
                  Originally posted by gooop
                  Originally posted by GunnarH
                  When doing so, did you start the command with perl?
                  Yes , beleive so i used
                  Code:
                   #!/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.
                  Code:
                  perl test.pl myfile.txt
                  ^^^^
                  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.

                  Comment

                  Working...