Gossamer Forum
Home : General : Perl Programming :

module question?

Quote Reply
module question?
Sometimes I think I know a bit of perl, other times I feel like I know nothing, this is one of my know nothing times Blush so please excuse me..

Will using
Code:
use CGI qw(:standard);
instead of
Code:
use CGI;
make any difference to the use of this module within a script?

The reason I ask is my script already uses
Code:
use CGI;
but I have just been reading up on cookies at perldoc.com and it says use
Code:
use CGI qw(:standard);
to make use of cookies.

Would that be in place of mine or would it need to be added like so
Code:
use CGI qw(:standard);
use CGI;

thanks guys..

chmod
Quote Reply
Re: [chmod] module question? In reply to
I believe, using the qw() part, just limits it to parts of the Module. That way, theoretically it should speed up the script.

I'm not definate on that though Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [chmod] module question? In reply to
It also depends on if you're using the OO interface to the module. If you are then, simply use CGI; at the top of your script, create a new CGI object and you can import functions as needed.

- wil
Quote Reply
Re: [Wil] module question? In reply to
If you are using CGI with OO code you don't need to import much Laugh, for example without OO code you'd need to import header methods or :standard to be able to do:

print header;

but with OO code nothing needs to be imported, you just do:

$obj->header;
Quote Reply
Re: [Paul] module question? In reply to
Yes, exactly, the functions get imported as needed.

- wil
Quote Reply
Re: [Wil] module question? In reply to
They aren't imported, you can access them without importing Laugh

Importing isn't the same and $obj->method
Quote Reply
Re: [Paul] module question? In reply to
thanks guys,
so if my thinking is correct when I add the "standard" part to the module call, only that part of the CGI module will be imported, but if I leave it at simply use CGI; then all parts get imported and are available.

chmod
Quote Reply
Re: [chmod] module question? In reply to
No, in order to import everything you need:

use CGI qw/:all/;
Quote Reply
Re: [Paul] module question? In reply to
confused, should I use both?

use CGI;
use CGI qw/:standard/;

or will just using,

use CGI;

enable me to set and retrieve a cookie?

thanks

chmod
Quote Reply
Re: [chmod] module question? In reply to
use CGI;

will do fine if you're using the OO interface of CGI.pm. What's the code you have to retrieve your cookie?

- wil
Quote Reply
Re: [chmod] module question? In reply to
Code:
confused, should I use both?

use CGI;
use CGI qw/:standard/;

or will just using,

use CGI;

enable me to set and retrieve a cookie?

You don't need both, you use the most appropriate one for the type of code you are writing, for example all my scripts are now OO code (object oriented) so I just use:

use CGI;

....but when I'm playing about answering forum posts I use:

use CGI qw/:standard/;

...on most occasions. I could limit it even more but :standard is ok in general.

So for setting a cookie non-OO style:

Code:
use CGI qw/:cgi/;

my $cookie = cookie( -name => 'Foo', -value => 'Bar', -path => '/', -expires => '+1d' );

print header( -cookie => $cookie );

OO style:

Code:
use CGI;

my $IN = new CGI;
my $cookie = $IN->cookie( -name => 'Foo', -value => 'Bar', -path => '/', -expires => '+1d' );

print $IN->header( -cookie => $cookie );
Quote Reply
Re: [Paul] module question? In reply to
thanks Paul, appreciated.

not so confused now, I`ll give the OO a try.

chmod
Quote Reply
Re: [chmod] module question? In reply to
The reason for the difference is that a CGI object is already letting perl know where to look for the methods you call, eg:

my $IN = new CGI;

$IN now stores a blessed reference to the CGI module, basically, so whenever you do something like $IN->cookie perl knows you are calling the cookie method in the CGI module.

The reason you need to explicitly import with non-OO code is because perl doesn't have an object to work off so:

cookie()

...could either be in your script or in any of the modules your script is using and so by importing into your name space perl now generally knows that cookie() is from the cgi module. Thats why using @EXPORT_OK is recommended over @EXPORT because with export your scripts namespace becomes polluted because sub-routines are imported into your script without you specifiying and so you could end up duplicating subroutines and arrays and other perl functions.

An a slightly un-related note, if you name a sub-routine with the name as an existing perl function for example, sub time { ...your script will work as perl will use your routine over the default function however to call the default function you will then need:

CORE::time

Anyway I'm rambling and that is just a very brief explanation but hopefully it will help a bit ;)

Last edited by:

Paul: Aug 27, 2002, 8:10 AM
Quote Reply
Re: [Paul] module question? In reply to
believe me it does, thanks.

chmod
Quote Reply
Re: [chmod] module question? In reply to
   Chmod: Another thing you may wish to know is that use CGI /:standard/ is a way of using the CGI libraries as a method and not a function. This requires more memroy at run/compile/interpret time so if you're using mod_perl or any *like* libraries and memory is a major concern then you may wish to use the use CGI form with OO because it tends to use less space for calls and storage.
Quote Reply
Re: [s0crates] module question? In reply to
>>
Another thing you may wish to know is that use CGI /:standard/ is a way of using the CGI libraries as a method and not a function.
<<

Isn't it the other way around?

Using a CGI object uses methods and importing uses functions.
Quote Reply
Re: [Paul] module question? In reply to
   Thanks Paul, I did mess the order of those 2 up. I knew I'd mess something up in there Crazy

s0crates
-- If practice didn't make perfect, what would it do? --
Quote Reply
Re: [s0crates] module question? In reply to
>>
-- If practice didn't make perfect, what would it do? --
<<

Give you experience :)