Undefined subroutine CGI::Vars

M

Mark

I want to store my request variables as a hash and have the code:

use CGI qw:)standard);
use CGI qw:)cgi-lib);
#during development, put Perl errors to the browser
use CGI::Carp qw(fatalsToBrowser);
use strict;
my $q = new CGI;
my $params = $q->Vars();

but the server is giving me an error message:

Undefined subroutine CGI::Vars

but I'm not sure why.

Thanks
Mark
 
T

Tad McClellan

Mark said:
use CGI qw:)standard);
use CGI qw:)cgi-lib);
[snip]

my $q = new CGI;


Object oriented interfaces do not need to import anything into
your name space, so don't import anything into your namespace. :)


use CGI;
 
K

krakle

Mark said:
I want to store my request variables as a hash and have the code:

use CGI qw:)standard);
use CGI qw:)cgi-lib);
#during development, put Perl errors to the browser
use CGI::Carp qw(fatalsToBrowser);
use strict;
my $q = new CGI;
my $params = $q->Vars();

but the server is giving me an error message:

Undefined subroutine CGI::Vars

I see lot's of problems.. The format your using CGI.pm is OO yet you
call it as function. You are also calling cgi-lib. And you are using a
scalar to stora Vars when it should be a hash to collect all. Try:

#!/usr/bin/perl -Tw
use strict;
use CGI; # Object Oriented Interface

my $q = new CGI;
my %params = $q->Vars; # Now you can use $params{name} for value of
name
 
M

Mark

Mark wrote:

[snip]


Object oriented interfaces do not need to import anything into
your name space, so don't import anything into your namespace. :)

use CGI;


Thanks. I took out the two imports so the code is now:

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use strict;
my $q = new CGI;
my $grandtotal= sprintf("%.2f", ($q->param('grandtotal')));
$params = $q->Vars;

now I get the error.

Execution /orderform.cgi aborted due to compilation errors.

If I replace the last line with

my $params = $q->Vars;

then I get as previously:

Undefined subroutine CGI::Vars

so I am not sure which is in error.

Perhaps my web host does not have the right library installed? They list the
following CGI modules as installed:

CGI::Apache
CGI::Carp
CGI::Cookie
CGI::Fast
CGI::pretty
CGI::push
CGI::Switch
CGI::Util

In order to successfully log orders made from my shopping cart script I have
to be able to use the (unknown amount of) POST variables in hidden form
fields. I can't see an easy way of doing this unless I can assign them to a
hash variable. Any suggestions?

Thanks
Mark
 
T

Tintin

Mark said:
Thanks. I took out the two imports so the code is now:

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use strict;
my $q = new CGI;
my $grandtotal= sprintf("%.2f", ($q->param('grandtotal')));
$params = $q->Vars;

now I get the error.

Execution /orderform.cgi aborted due to compilation errors.

Due to you not declaring $params
If I replace the last line with

my $params = $q->Vars;

then I get as previously:

Undefined subroutine CGI::Vars

so I am not sure which is in error.

You must have a pretty old version of Perl/CGI module installed on your
webserver.
 
K

krakle

Mark said:
Mark wrote:

[snip]


Object oriented interfaces do not need to import anything into
your name space, so don't import anything into your namespace. :)

use CGI;


Thanks. I took out the two imports so the code is now:

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use strict;
my $q = new CGI;
my $grandtotal= sprintf("%.2f", ($q->param('grandtotal')));
$params = $q->Vars;

my %params = $q->Vars;
now I get the error.

Read my other reply to this. Geesh.
 
S

Sherm Pendley

krakle said:
my %params = $q->Vars;

Calling Vars() in scalar context is valid, and returns a tied hash
reference.

Unlike the hash you get when you call Vars() in array context, the hash
reference you get by calling it in scalar context is not read-only. It
can also be used to set parameters for output, which is used in self-
referential CGIs that include forms and/or links back to themselves in
the HTML they output.
Read my other reply to this. Geesh.

Read 'perldoc CGI'. Geesh.

sherm--
 
K

krakle

Sherm Pendley said:
Read 'perldoc CGI'. Geesh.

sherm--

He wanted to know why it didn't work. I showed him a working example.
Which was tested. Being that I use it in most of my scripts... Then he
complained his script still didn't work... I pointed out exactly what
he did wrong... Using OO while CGI is called as function.
 
S

Sherm Pendley

krakle said:
I pointed out exactly what
he did wrong... Using OO while CGI is called as function.

In the post I replied to, the only thing you "pointed out" was this:
my %params = $q->Vars;

The "error" that you're "correcting" above is not an error, nor does it
have anything to do with methods vs. functions. Calling Vars() in scalar
context is allowed, regardless of whether you call it as a method or as
a function.

The addition of "my" above *is* good advice - which is why I did not
criticize it in my reply.

You also said:
Read my other reply to this. Geesh.

.... and in that other reply:
And you are using a
scalar to stora Vars when it should be a hash to collect all.

That is false, and repetition won't make it true. Using a scalar to get
a hashref is valid, supported, useful, and - most importantly - documented.

Also from your earlier reply:
I see lot's of problems.. The format your using CGI.pm is OO yet you
call it as function.

You're wrong here too. Mark is *not* calling Vars as a function in the
code he posted. He is creating a query object $q, and calling $q->Vars()
as an instance method.

Your advice here is pointless - importing functions into the local name
space won't prevent OO the methods from working. There's no reason to do
so if you intend to use OO methods, that's true, but it won't cause the
type of error he's getting.

So drop the snide attitude and whiny "Geesh" comments please. If you
want people to follow your advice, post some advice worth following.

sherm--
 
K

krakle

Sherm Pendley said:
In the post I replied to, the only thing you "pointed out" was this:

Since when do you reply to a snipet of post without reading the whole
thread?
The "error" that you're "correcting" above is not an error, nor does it
have anything to do with methods vs. functions.

Sure it does. 1 he called CGI.pm as a method. However he is using
OO... I never said it was an error.. I simply stated a snipet that did
infact WORK and accomplish exactly what he was doing...
The addition of "my" above *is* good advice - which is why I did not
criticize it in my reply.

Good advice. Bad advice. No advice. Doesn't it all get criticized on
usenet? What's your point, lad?
You also said:


... and in that other reply:


That is false, and repetition won't make it true. Using a scalar to get
a hashref is valid, supported, useful, and - most importantly - documented.

Which code works? Mine or his? Mind. Does it accomplish his goal? Yes.
Also from your earlier reply:



You're wrong here too. Mark is *not* calling Vars as a function in the
code he posted. He is creating a query object $q, and calling $q->Vars()
as an instance method.

He wanted help, correct? I gave him help and a working example that
accomplishes exactly what he is trying to do. If you can get that much
on usenet you are a lucky duck... Most people just criticize peoples
postings.... *snickers*.. Having fun?
Your advice here is pointless

And I stop your post here. You summed it up... My working code is
worthless to the guy whose code doesn't work. Don't you love usenet
advice? Round 3.
 
S

Sherm Pendley

krakle said:
And I stop your post here. You summed it up...

Yes I did, and I'll do so again. You said that Vars() must be called in
list context, not scalar context. That is false, and the fact that it is
false is clearly documented in 'perldoc CGI' - a document you clearly
are not familiar with.

Further, you claimed that the OP called Vars() as a function, but the
code he posted read 'my $params = $q->Vars()' - clearly you don't know
the difference between a function and a method.

Mark asked for help. The error he received is "Undefined subroutine";
calling Vars() in scalar context will not produce that error, and
neither will calling Vars() as a method when it has also been imported
as a function.

Calling Vars() as a function *will* produce that error, if it has not
been imported as a function. But in the code Mark posted, it *is*
imported; not only that, it's called as a method. Doing both is wrong
only in terms of style - it won't cause an error.

You were wrong on at least one point, and overall your suggestions were
useless. Calling me rude won't change those facts.

sherm--
 
S

Sherm Pendley

Mark said:
use CGI qw:)standard);
use CGI qw:)cgi-lib);
#during development, put Perl errors to the browser
use CGI::Carp qw(fatalsToBrowser);
use strict;
my $q = new CGI;
my $params = $q->Vars();

but the server is giving me an error message:

Undefined subroutine CGI::Vars

but I'm not sure why.

Is that your *real* code?

I suspect that your real code might actually be calling Vars() like this:

my $params = Vars();

In that case, your problem might come from the dual sets of 'use'
statements. A module that has already been 'use'd won't be reloaded if
another 'use' is encountered, so the second - which imports the function
you want to use - wouldn't have any effect.

So you might try one of two things. Either make sure that your real code
does call Vars() as a method, like the code you posted here does. Or,
import both groups of functions with a single use statement, like this:

use CGI qw:)standard :cgi-lib);

sherm--
 
M

Mark

Sherm Pendley said:
In the post I replied to, the only thing you "pointed out" was this:


The "error" that you're "correcting" above is not an error, nor does it
have anything to do with methods vs. functions. Calling Vars() in scalar
context is allowed, regardless of whether you call it as a method or as
a function.

Thanks for posting your advice which I have taken on board. Much of the
comments are moot now anyway as I emailed my host support who told me they
don't even have CGI::Vars installed anyway and won't do it (cheapskates!).
So all the code posted probably *should* work if that were not the case.
Hence the error message and my earlier suspicions were correct. I've opted
for changing the way I post my variables from the PHP server so I don't need
to use Vars just param().

[snip]
So drop the snide attitude and whiny "Geesh" comments please.

I totally agree with you. I wish some people would try to remember what it
was like when *they* first started learning to code. It's this kind of
attitude that puts people off Usenet and totally negates any helpfulness of
the comments being offered. I tend to avoid people who post comments like
this as they often just need their egos massaged by being able to patronise
newbies. Thankfully there are people like yourself who are genuinely trying
to help people starting out in code and not just show-boating.

Thanks.
Mark
 
S

Sherm Pendley

Perusion said:
The docs say that you should "use CGI qw/:cgi-lib/" if you want
to import Vars as a function

That's true.
-- not if you want to use it as an object method.

That's not. If you call a method, it shouldn't matter if it's imported
or not - importing it is unnecessary, but doing so shouldn't trigger an
"Unknown subroutine" error like the one Mark posted.

On the other paw, unnecessary imports *should* be avoided for the sake
of style and clarity. Think of the poor schmuck who'll have to alter
this code in two years - especially if that poor schmuck might be
yourself. ;-)

sherm--
 
S

Sherm Pendley

Mark said:
comments are moot now anyway as I emailed my host support who told me they
don't even have CGI::Vars installed anyway and won't do it (cheapskates!).

Hmmm... That smells a little fishy. Vars() is part of the standard
CGI.pm module, and that module is standard with Perl 5.8.x and newer.
They might have a very old CGI.pm that doesn't have Vars() - in which
case they'd have to have an old Perl as well.

sherm--
 
M

Mark

They use Perl 5.6.1 so I guess that's why.

Mark

Sherm Pendley said:
(cheapskates!).

Hmmm... That smells a little fishy. Vars() is part of the standard
CGI.pm module, and that module is standard with Perl 5.8.x and newer.
They might have a very old CGI.pm that doesn't have Vars() - in which
case they'd have to have an old Perl as well.

sherm--
 
M

Mark

Sherm Pendley said:
Is that your *real* code?

*Real* code? Are you suggesting I would post *fake* code?! Would I do such a
thing? ;-)
Seriously though, it is just a straight copy and paste.
I suspect that your real code might actually be calling Vars() like this:

my $params = Vars();

In that case, your problem might come from the dual sets of 'use'
statements. A module that has already been 'use'd won't be reloaded if
another 'use' is encountered, so the second - which imports the function
you want to use - wouldn't have any effect.

So you might try one of two things. Either make sure that your real code
does call Vars() as a method, like the code you posted here does. Or,
import both groups of functions with a single use statement, like this:

use CGI qw:)standard :cgi-lib);

What I did do is tried every permutation of code I could think of based on
the various articles I read to see where the error could have lain including
what you have given me above but as every permutation brought up the same
errors I did not know where the error lay. The code I posted happened to be
where I was at at the time of posting. (I also tried both OO and method).Yes
I had read about the above in perldoc too but no wonder I was confused - it
was never going to work anyway (see previous post) CGI::Vars was definitely
not installed on the server.

Anyway no need for more replies to this topic for my benefit at least anyway
as had the Vars function been installed on the server my initial code would
have worked right from the start - the perldoc explained it well enough for
me. It was only through messing with the code to try to find the cause of
the error that led to the confused mess of my previous posts. But, of
course, if you still have matters to settle with the other posters in this
thread I will be happily lurking in the background taking notes... ;-)

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top