LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-27-2007, 06:47 PM   #16
Four
Member
 
Registered: Aug 2005
Posts: 298

Rep: Reputation: 30

Thanks for the info on error using open and $! I have implemented it in.
I got to my computer to test it turns out that I had a variable misspelled which resulted in emptyness oh and I didn't use curl properly (well I didn't really know what curl was but now I do kinda).

Enjoy script.

Let me know if it works

Code:
use strict;
# this code is not tested I'm at school right now using windows machine 
# with no perl
# read a line (assuming file is opened using open())

# place your url here
# this q{} may help with programming syntax which
# will help prevent the "bareword" problem that was encountered
my $URL = q{http://example.com};

my $i;

my $file;
my $verb=0; # verbious
for($i = 0; $i < @ARGV; $i++){
   if($ARGV[$i] eq "-v"){
       $verb = 1;
   } else {
      $file = $ARGV[$i];
   }
   if($ARGV[$i] eq "--help"){
        print "perl script.pl file [-v]\n-v for verbous\n";
      exit(0);
   }
}
if(!$file){
   print "Please enter a file name\n";
   $file = <>;
}
if(!(-e $file)){
   print "file doesn't exit\n";
}
open (file, "$file") or die ("couldn't open $file\nError: $!");
my $r;
my $line = 1;
while(<file>){
   my $read;
   $read = $_;
   chomp $read;
   # get relevent data
   $read =~ /(.*?),(.*?),(.*?),(.*?)/;
   my ($num, $date, $reason, $enable);
   $num = $1;
   $date = $2;
   $reason = $3;
   $reason =~ s/ /+/g;
   $enable = $4;
   if(!$read){ next; }
   if($enable eq "yes"){
      # put code to do something here if you want
      # I think you mean to have plus signs there too?
      $enable = "ETC+Applies";
   } else {
      $enable = "No+ETC";
   }
   # its case sensitive so the below and above need to match case
   my $cmd;
   $cmd = qq{curl "$URL/index.php?number=$num&date=$date&reason=$reason&enabled=$enable"};
   print "$cmd\n" if($verb);
   $r = system($cmd);
   if($r != 0){
      print "Error: couldn't run curl on line $line\n$read\n";
   }
}
close file;
 
Old 11-28-2007, 01:42 AM   #17
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
morphix,
Are you still interested in a production solution to your stated problem? That is the need you seemed to originally present.

Or, have you become more interested in using said problem as a vehicle for learning some Perl & how to debug same?
 
Old 11-30-2007, 01:27 AM   #18
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
The first line in my Perl progs is:
#!/usr/bin/perl -w
then you can just say
./myperl.pl

The -w switch is equiv to
use warnings;

Always add one of these as well as the
use strict;
you've already got.
To check for syntax errors etc with execution, use

perl -wc myperl.pl
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute a ssh script on Linux server from Windows through a bat script? wanna13e Programming 13 10-23-2009 02:41 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
Help w/ script to read file and parse variables cslink23 Linux - General 18 11-26-2006 02:22 AM
cURL script question verbatim Programming 8 05-18-2005 04:50 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM


All times are GMT -5. The time now is 03:28 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration