Light Bringer Light Bringer -4 years ago 106
Perl Question

Perl (use CGI) Can't call method "header" without a package or object reference

Alright, using Perl for the first time in a decade, sort of rusty. Thinking perhaps some updates since then (genius huh?)

Alrighty, This is Perl 5, version 16, subversion 3 x86_64-linux-thread-multi with 29 registered patches on AWS micro.

Perl came with it, I just yum installed perl-CGI perl-Data-Dumper

use CGI; # load cgi routines
my $q = CGI>new; # cgi object
print $q->header(),
$q->start_html("Hello World"),

Seems about a simple and straightforward as it can be, as I build programs bit by bit. But I have got an error at if I run it at the command line (or browser).

Can't call method "header" without a package or object reference at create line 4.

Line 4 is the print $q->header(); "create" is the file name.

Any ideas? Bring me up to date in Perl 2017? My problems, being rusty are always something fabulously simple.

Answer Source

Looks like a typo.

my $q = CGI>new;

should probably be

my $q = CGI->new;

Perl is interpreting your code as:

my $q = 'CGI' > 'new';

So you end up with a false value (probably an empty string) in $q. And you can't call methods on an empty string :-)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download