Results 1 to 9 of 9
Thread: perl scripting
-
06-26-2005, 07:11 PM #1Newbie
- Join Date
- Jun 2005
- Posts
- 6
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.
-
06-26-2005, 09:26 PM #2Aspiring Evangelist
- Join Date
- May 2002
- Posts
- 441
Re: perl scripting
Originally posted by always
!/usr/bin/perl -w
Originally posted by always
$textfile = <~/bin/textfile>
$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.
-
06-27-2005, 08:17 AM #3Newbie
- Join Date
- Jun 2005
- Posts
- 6
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.
-
06-27-2005, 09:42 AM #4Web Hosting Master
- Join Date
- Apr 2003
- Location
- UK
- Posts
- 2,569
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"; }
-
06-27-2005, 11:24 AM #5Newbie
- Join Date
- Jun 2005
- Posts
- 6
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"; }
-
06-27-2005, 11:35 AM #6Web Hosting Master
- Join Date
- Apr 2003
- Location
- UK
- Posts
- 2,569
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
-
06-27-2005, 11:50 AM #7WHT Addict
- 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";
}
-
06-27-2005, 11:52 AM #8Web Hosting Master
- Join Date
- Apr 2003
- Location
- UK
- Posts
- 2,569
i wondered if someone'd go that route..
-
06-27-2005, 12:00 PM #9WHT Addict
- 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";
}