Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl code using CUrl

Status
Not open for further replies.

Panavia

Technical User
Nov 14, 2010
15
DE
Hi,

I have a peace of perl code which is using curl to download GRIB files.
Coming from a Visual Basic and Java backgoud I have just limited knowledge about what exactly is going on in the code below.
Especially the curl usage is very cryptographic for me...
I would highly apprechiate if someone provides an explaination what is going on in which line.

Perl:
$range="";
$lastfrom='';
$lastto=-100;
while (<STDIN>) {
  chomp;
  $from='';
  /:range=([0-9]*)/ && do {$from=$1};
  $to='';
  /:range=[0-9]*-([0-9]*)/ && do {$to=$1};

  if ($lastto+1 == $from) {
    $lastto = $to;
  }
  elsif ($lastto ne $to) {
    if ($lastfrom ne '') {
       if ($range eq '') { $range="$lastfrom-$lastto"; }
       else { $range="$range,$lastfrom-$lastto"; }
    }
    $lastfrom=$from;
    $lastto=$to;
  }
}
if ($lastfrom ne '') {
  if ($range eq '') { $range="$lastfrom-$lastto"; }
  else { $range="$range,$lastfrom-$lastto"; }
}

unlink $file;
if ($range ne "") {
   $err=system("$curl -f -v -s -r \"$range\" $url -o $file.tmp");
   $err = $err >> 8;
   if ($err != 0) {
      print STDERR "error in getting file $err\n";
      sleep(20);
      exit $err;
   }
   if (! rename "$file.tmp",  "$file") {
      sleep(30);
   }
}
else {
  sleep(10);
  print STDERR "No download!\n";
  sleep(30);
  exit 8;
}

Thanks!
 
While it may not be helpful to point out, I must say that this code is very sloppy. It doesn't even appear to have use strict and use warnings turned on as none of the variables are declared with my. This does not make me optimistic toward the effectiveness and bug-free nature of this code.

However, what you've shown us is fairly straight forward. It builds a range variable based off input from the command line, and then calls an amorphous program in the $curl variable. This could be the unix utility, but honestly we have no way of knowing for sure what that is or what it's doing. What you can do though is rewrite that code is that it is mirrored to the shell so that you can see exactly what is being executed.

Code:
if ($range ne "") {
   my $command = "$curl -f -v -s -r \"$range\" $url -o $file.tmp";
   print "$command\n"; # For Debugging
   $err=system($command);
   $err = $err >> 8;

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top