download using wget in perl

3.4k Views Asked by At

New to Perl, hope someone could kindly explain the following questions related to this code:

$url ="some url";
open FH, "wget -q -O- $url|" or die;  
while(<FH>){  
  ...  
}  
  1. what does the the second - in -O- mean?
  2. what does the | in $url | mean?
  3. I tried to do a seek with the FH like this:
 $url ="some url";
 open FH, "wget -q -O- $url|" or die;  
 seek(FH, 14000, 1);  

but if I echo the position with echo tell(FH), I get 0, anyone know why couldn't I seek the FH? Thanks

3

There are 3 best solutions below

1
On BEST ANSWER

The -O- argument tells wget to output to standard output (i.e. your console/terminal), not to a file.

The pipe in the open call is a pipe open. The file handle will be connected to the process's output (in this case, to what wget outputs).

A tell call on this type of filehandle doesn't really make sens. The underlying thing is a stream, not a file. tell can return different things on streams, it depends on the OS.

1
On

The second - means that you want wget to output to the standard output rather than to a file. The | means that you are opening a pipe to the newly created wget process and you can read its output.

Seeking makes sense for files on disk but not for pipes. The output is produced as the program is executed so it's not possible to "jump in the future".

1
On

what does the the second - in -O- mean?

From man wget

The documents will not be written to the appropriate files, but all will be concatenated together and written to file. If - is used as file, documents will be printed to standard output, disabling link conversion. (Use ./- to print to a file literally named -.)


what does the | in $url | mean?

It isn't in $url, it is after it.

From the perldoc for open

if the filename ends with a '|' , the filename is interpreted as a command that pipes output to us