views:

282

answers:

4

Hello,

I need some help understanding the following Perl code snippet. I have the following two questions.

1. What does local *PIPER mean? Even though I've done some Perl programming before the local * syntax is new to me. Is it a pointer?

2. What is the purpose of

curl http://www.somesite.net/cgi-bin/updateuser.cgi? -d "userid=$userid&password=$password\" -s |"; ?

Thank You :)

local *PIPER;

$http_query = "curl http://www.somesite.net/cgi-bin/updateuser.cgi? -d \"userid=$userid&password=$password\" -s |";

open(PIPER,$http_query) or die "sorry";

while(<PIPER>)
{
   $rets = $_;

}

close(PIPER);

return $rets;
+10  A: 

1) "*PIPER" is a typeglob. It is "$PIPER", "@PIPER", and "%PIPER" (and I then some) all in one. They are declaring all *PIPER names local to the code snippet you have.

2) That's a shell command. It ends with a | which means that that command is being run, and it's output is being piped in as the input for the filehandle PIPER. The program then reads this line-by-line with while(<PIPER>), but you already know that.

I don't know much about curl, but I know it's a command-line program for doing stuff on the internet. Just a random stab, your code appears to be accessing a website's CGI script and sending it some information.

Chris Lutz
For a documentation a typeglobs look at http://www.perl.com/doc/manual/html/pod/perldata.html
Peter
Thanks Chris, your explanation really helped :)
In the days before 'use FileHandle;' or related modules, this was the standard way to provide yourself with a local 'file handle'. These days, you don't normally need that notation - but STDERR, STDOUT, STDIN are relics of that bygone era.
Jonathan Leffler
+3  A: 

local *PIPER; is declaring the PIPER file handle to be local. Since file handles don't have their own type symbol, they need to be caught by typeglobs in order to be declared local.

curl is similar to wget; it is used to transfer a URL. See man curl for more details, but the -d switch (data) passes the following string as data in a POST operation, and the -s switch (silent) suppresses error output and a progress meter.

ruds
+1  A: 

2) This actual invocation of curl sends an HTTP POST request with the text \"userid=$userid&password=$password\" to http://www.somesite.net/cgi-bin/updateuser.cgi? and outputs the response from the server (to the pipe). The -s flag strips the output from anything except what is coming from the server (such as error messages or progress meters).

Sebastian Ganslandt
+6  A: 

As others have noted, *PIPER is a typeglob so you can dynamically scope the PIPER filehandle. That's ancient Perl though. Use a lexical filehandle instead:

sub foo {
    my $http_query = "...";

    open my($piper), $http_query or die "sorry";

    while()
        {
        $rets = $_;
        }

    return $rets;
    }

You don't need to do any of this to send a POST request to a server though. You can do this completely within Perl using LWP.

brian d foy