Results 1 to 9 of 9

Thread: perl scripting

  1. #1

    perl scripting

    I am trying to get the file using command-line argument such as "$>datafile -s filename "but unfortunately it is not working. The errors i am getting as below and only print out the first word for each line in the data file. Anyone know this easy stuff please help me to correct the code that i code wrong. I am trying to do command line checking on is "-s" specified, and the filename specified. If "-s" was specified, then set a true/false variable. If a filename was specified,then connect file to STDIN. Here is my code and errors. Please help! I searched on the net but no luck to get what i am want. Therefore, I need you guys expert help.

    Thank you and cheer!

    Best
    Always


    #textfile is data file

    !/usr/bin/perl -w
    $textfile = <~/bin/textfile>

    $simple=0;

    if ($ARGV[0] eq "-s")
    {
    $simple=1;
    }

    if ($simple)
    {
    do something with the record.\n";
    }

    if ($ARGV[0])
    {
    open (STDIN, "<$textfile") || die "Cannot open file $textphone,\n$!";
    print "Opened file $textfile ok.\n";
    }

    while ($data =<STDIN>)
    {
    chomp $data;

    print "$data";
    }

    TESTING USING THIS COMMAND textphone -s testing
    /users/bin/textfile: line 1: What: command not found
    /users/bin/textfile: line 2: The: command not found
    /users/bin/textfile: line 4: you: command not found
    /users/bin/textfile: line 5: or: command not found
    /users/bin/textfile: line 6: people: command not found
    /users/bin/textfile: line 7: syntax error near unexpected token `678-1255.'
    /users/bin/textfile: line 7: `(575)456-3425. (459)-788-1054 as a phone number,'

    IF I USE THIS COMMAND ./testing OR testing
    Unquoted string "record" may clash with future reserved word at /users/bin/testing line 19.
    Unquoted string "n" may clash with future reserved word at /users/bin/testing line 19.
    String found where operator expected at /users/bin/testing line 24, near "open (STDIN, ""
    (Might be a runaway multi-line "" string starting on line 19)
    (Missing semicolon on previous line?)
    String found where operator expected at /users/bin/testing line 24, near "$textfile") || die ""
    (Missing operator before ") || die "?)
    Bareword found where operator expected at /users/bin/testing line 24, near "") || die "Cannot"
    (Missing operator before Cannot?)
    String found where operator expected at /users/bin/testing line 25, near "print ""
    (Might be a runaway multi-line "" string starting on line 24)
    (Missing semicolon on previous line?)
    Bareword found where operator expected at /users/bin/testing line 25, near "print "Opened"
    (Do you need to predeclare print?)
    Bareword found where operator expected at /users/bin/testing line 25, near "$textfile ok"
    (Missing operator before ok?)
    Unquoted string "ok" may clash with future reserved word at /users/bin/testing line 25.
    Unquoted string "n" may clash with future reserved word at /users/bin/testing line 25.
    String found where operator expected at /users/bin/testing line 32, near "print ""
    (Might be a runaway multi-line "" string starting on line 25)
    (Missing semicolon on previous line?)
    Scalar found where operator expected at /users/bin/testing line 32, near "print "$data"
    (Do you need to predeclare print?)
    String found where operator expected at /users/bin/testing line 32, at end of line
    (Missing semicolon on previous line?)
    syntax error at /users/bin/testing line 19, near "do something with the "
    Can't find string terminator '"' anywhere before EOF at /users/bin/testing line 32.
    Last edited by always; 06-26-2005 at 07:17 PM.

  2. #2
    Join Date
    May 2002
    Posts
    441

    Re: perl scripting

    Originally posted by always

    !/usr/bin/perl -w
    You might try changing that to #!/usr/bin/perl -w

    Originally posted by always

    $textfile = <~/bin/textfile>
    Is this supposed to be the path to a file named textfile? Either way you're missing a semi-colon there. It might work better like:

    $textfile = '/path/to/textfile';

    If you're running the script from the command line you might want to try:

    perl -c yourscriptname

    That will let you know if there's any syntax errors.

  3. #3

    Re: Re: perl scripting

    Thank you for your respone. The thing that you pointed out were not the issue, just the body code itself have problem and I don't know how to make it works. The -s is make-up it could be -a, -whatever, it just for ourself convinence when testing.


    Originally posted by deadserious

    You might try changing that to #!/usr/bin/perl -w


    Is this supposed to be the path to a file named textfile? Either way you're missing a semi-colon there. It might work better like:

    $textfile = '/path/to/textfile';

    If you're running the script from the command line you might want to try:

    perl -c yourscriptname

    That will let you know if there's any syntax errors.

  4. #4
    Join Date
    Apr 2003
    Location
    UK
    Posts
    2,560
    Code:
    #!/usr/bin/perl -w
    
    use strict;
    
    my $textfile = "/tmp/textfile"
    
    my $simple=0;
    
    if ($ARGV[0] eq "-s")
    {
      $simple=1;
    }
    
    if ($simple)
    {
      print "do something with the record.\n";
    }
    
    if ($ARGV[0])
    {
      open FILE, "$textfile" || die "Cannot open file $textphone,\n$!";
      print "Opened file $textfile ok.\n";
    }
    
    while (my $data = <FILE>)
    {
      chomp $data;
    
      print "$data";
    }

  5. #5
    THE ERROR I AM GETTING FOR THIS IS "Use of uninitialized value in string eq at /users/bin/testing1

    if ($ARGV[0] eq "-s")
    {
    $simple=1;
    }


    Originally posted by Slidey
    Code:
    #!/usr/bin/perl -w
    
    use strict;
    
    my $textfile = "/tmp/textfile"
    
    my $simple=0;
    
    if ($ARGV[0] eq "-s")
    {
      $simple=1;
    }
    
    if ($simple)
    {
      print "do something with the record.\n";
    }
    
    if ($ARGV[0])
    {
      open FILE, "$textfile" || die "Cannot open file $textphone,\n$!";
      print "Opened file $textfile ok.\n";
    }
    
    while (my $data = <FILE>)
    {
      chomp $data;
    
      print "$data";
    }

  6. #6
    Join Date
    Apr 2003
    Location
    UK
    Posts
    2,560
    you should do a check to see if $ARGV[0] exists first

    something like:

    if($ARGV[0] && ($ARGV[0] eq "-s")) {



    your problem existed because if there is no argv[0] it had nothing to compare to "-s" but had to do your comparison

  7. #7
    Join Date
    Dec 2003
    Location
    Earth
    Posts
    144
    Code:
    #!/usr/bin/perl -w
    #
    use warnings;
    use strict;
    
    
    use Getopt::Std;
    
    
    getopt('s');
    
    our($opt_s);
    
    if ($opt_s){
        open FILE, "$opt_s" || die "Cannot open file $opt_s,\n$!";
        print "Opened file $opt_s ok.\n";
    
        while (<FILE>){
            chomp;
            print "$_\n";
        }
    }
    else{
        print "No options given\n";
        print "Usage: ./script.pl -s <filename>\n";
    }
    if($php !== $javascript){
    echo "Good it's not supposed to be";
    }

  8. #8
    Join Date
    Apr 2003
    Location
    UK
    Posts
    2,560
    i wondered if someone'd go that route..

  9. #9
    Join Date
    Dec 2003
    Location
    Earth
    Posts
    144
    Hehe.

    Originally posted by Slidey
    i wondered if someone'd go that route..
    if($php !== $javascript){
    echo "Good it's not supposed to be";
    }

  10. Newsletters

    Subscribe Now & Get The WHT Quick Start Guide!

Related Posts from theWHIR.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •