Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

using content-type for judgement

by sarvan (Sexton)
on Jun 22, 2011 at 09:39 UTC ( #910873=perlquestion: print w/replies, xml ) Need Help??
sarvan has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,

My perl program gets input as returns a url(its a pdf searcher program). Once i get the url, i used LWP::useragent(head,content_type) to know the type of the content the url holds.

So far, i have got types like application/pdf,application/x-pdf,application/octent-stream,text/html,text/plain.

My question is i want to turn my program to print three kinds of print statements according to this return type of content-type

for e.g: if content-type of the url is "application/pdf" then print "file exist". or if content-type of the url is "text/html" then print "file exist but requires subscription" or simple "no matching found" if there is no url returned..

The problem now is, it only works for this two types of content. but when there are other kinds of contents returned it will not work..

foreach(@urls) { my $ua = LWP::UserAgent->new(); my $url = "$_"; my $response = $ua->head($url); #HTTP::Response type return my $headers = $response->headers(); # HTTP::Headers type return my $content_type =$headers->content_type(); # string print "$content_type\n"; } if($content_type eq 'application/pdf') { print "File Exists:\n $_"; } elsif($content_type eq 'text/html') { print "File Exists but requires subscription to access:\n"; } else{ print "No Matching found"; }

Replies are listed 'Best First'.
Re: using content-type for judgement
by moritz (Cardinal) on Jun 22, 2011 at 09:46 UTC

    Properly indenting your code reveals the problem:

    foreach(@urls) { my $ua = LWP::UserAgent->new(); my $url = "$_"; my $response = $ua->head($url); #HTTP::Response type return my $headers = $response->headers(); # HTTP::Headers type retu +rn my $content_type =$headers->content_type(); # string print "$content_type\n"; } if($content_type eq 'application/pdf') { print "File Exists:\n $_"; } elsif($content_type eq 'text/html') { print "File Exists but requires subscription to access:\n"; } else{ print "No Matching found"; }

    Your if is outside the loop, so the variable $content_type has gone out of scope, it's value is now undef.

    Please use strict; use warnings;, that's a safeguard that catches such errors for you.

    And indent your code properly, one indention level per opening curly brace. See perlstyle. As this example illustrates, it's not only a stylistic thing - it is essential for understanding the code.

    Also please start by learning the basics of Perl. perlintro and the book "Learning Perl" are good places to start.

Re: using content-type for judgement
by planetscape (Chancellor) on Jun 22, 2011 at 11:35 UTC
Re: using content-type for judgement
by Utilitarian (Vicar) on Jun 22, 2011 at 12:26 UTC
    foreach(@urls){ my $ua = LWP::UserAgent->new(); my $url = "$_"; my $response = $ua->head($url); #HTTP::Response type return my $headers = $response->headers(); # HTTP::Headers type return my $content_type =$headers->content_type(); # string print "$content_type\n"; if($content_type eq 'application/pdf'){ print "File Exists:\n $_"; } elsif($content_type eq 'text/html'){ print "File Exists but requires subscription to access:\n"; } elsif ($response->is_success) {{ print "No Idea how to parse $content_type\n""; } else{ print"No Match Found\n"; }
    A definitive list of MIME types is an unrealistic goal, however you should be able to draw up a top 50 and handler applications fairly easily.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://910873]
Approved by Corion
help
Chatterbox?
[james28909]: i guess i should have done it in perl. i bet it woulnt have taken me 5 hours to figure out haha
[choroba]: In the end, I was able to upgrade Perl from 5.8.3 to 5.22 and install Net::SFTP::Foreign , at least for the task involved
[james28909]: what would be the best way to capture that stream with perl? ffmpeg args -rtp rtp://127.0.0.1?
[LanX]: if I was forced to talk about all security risks I encountered oO
[james28909]: if i did that with ffmpeg, i could then listen on the port with perl right?
[LanX]: one of my clients filtered a menu linking to Web pages according to user rights. .. but he didn't secure the access to those unlisted pages

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2017-05-22 21:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?