Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

using content-type for judgement

by sarvan (Sexton)
on Jun 22, 2011 at 09:39 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://910873]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-19 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found