How can i put cgi instance into sub?
i have main cgi script:
#!/usr/bin/perl -w
use CGI;
use strict;
use makepage;
...
my $cg=new CGI;
...
makepage->printhead($cg);
...
In makepage.pm is:
printhead {
my $cg={};
bless $cg, "CGI";
... #it is nice, e.g. $cg->url is OK, but whenever i try $cg->param,
it is empty
}
Don't anybody know, what to do whit this, many thanx, J. Slaby.
You need to read or re-read perldoc perlsub.
First, you didn't declare the subroutine correctly, in that you forgot the
keyword sub. From your statement that "it is nice", I have to assume you
have actual working code somewhere. Please read the posting guidelines
that are posted to this group once a week. They will tell you to post
complete but short scripts that illustrate your problem, and to copy and
paste your code, rather than retype it. That being said, let's move on.
Within your subroutine, you are declaring a new lexical variable $cg. You
are then making this new lexical variable a CGI object. This object has
absolutely nothing to do with the $cg that you passed into the subroutine.
What you want to do is access the variable that you passed in. This is
known as a subroutine argument. Subroutine arguments are passed in via
the @_ array. So within your function:
sub printhead {
my $newcg = $_[0];
my $text = $newcg->param('textfield'); #for example
#...
}
You could call this $newcg any name you want, including $cg. But it
doesn't have anything to do with the original $cg unless you assign it
back to the variable you passed in, as I did by assigning to $_[0].
Again, please make sure you read
perldoc perlsub
to understand more about how subroutine arguments work.
Hope this helps,
Paul Lalli