×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Perl code using CUrl

Perl code using CUrl

Perl code using CUrl

(OP)
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.

CODE --> 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!

RE: Perl code using CUrl

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close