So, I got a PERL script I am trying to run, it start like this:
use strict;
use a;
use CGI 'param';
use URI::Escape;
use HTML::FromText 'text2html';
use XML::Simple;
use LWP::UserAgent;
use Data::Dumper;
use URI::Escape;
use DBI;
use Tie::DBI;
use Digest::MD5 'md5_hex';
use MIME::Base64;
use Encode;
my $r = shift; $r->content_type("text/html; charset=utf-8"); my $tmp = a::tmp();
When it get’s to the part where content_type() function is called, it fails with this error message:
Can't call method "content_type" on an undefined value at script.pl line 18.
Any ideas? I am kinda PERL newbie.
If
$ris coming from@ARGV, it won’t have acontent_typemethod.You could potentially
bless$rinto some package, but that’s surely not what you’re intending to do, here.I’m guessing that you want to obtain a CGI parameter, probably a
POSTed upload file? So you want$rto be aCGIobject, not a parameter. You’d start withBut, then, I refer you to the very fine manual for
CGI, http://perldoc.perl.org/CGI.html orperldoc CGIfrom the shell.(To expand a bit:)
In Perl, a $scalar var holds “any one thing.” Things coming in from the command-line are generally strings (maybe numbers, on a good day); that’s what
shiftwould get at the top level. (The special variable@ARGVcontains command-line parameters passed in to your program.)“One thing” can also be a reference to an object. In Perl’s Object Oriented model, the methods of a package (“class”) are tied to that reference using
bless. That’s usually handled for you, though; the special subroutine (aka function, method)CGI::newwill create a newCGIobject with some state data (things like form fields’ values), andblessit into theCGIpackage.The
->notation going to a function call will only work if your variable contains ablessed reference. You can “ask” what kind of a reference you have in a variable usingref; you’ll get the name of its package (aka class). ($foo = []; bless $foo => 'Some::Package'; print ref $foo;=>Some::Package)— But, again, for your specific case, check out some of the examples in the
CGImodule’s manual 🙂