Phoenix.pm: Perl internals questions

Peter Jones pedrohonez at yahoo.com
Mon May 10 18:19:17 CDT 1999


--- Shay Harding <sharding at ccbill.com> wrote:
> Just wondering the following:
> 
> Which is faster at execution, uses less memory, best overall in terms
> of
> performance:
> 
> 
> use CGI qw(:standard);
> print header
> print start_html
> etc
> 
> 
> or
> 
> 
> use CGI;
> $cgi = new CGI;
> 
> print $cgi->header
> print $cgi->start_html
> etc;
> 
> ??
> 
> Using the OO implementation seems to be the best choice. Just
> wondering if any one knows which is better
> overall and why?
> 
> 

There is a small overhead when using OO in Perl. I have not found the
need to use the OO side of CGI.pm. I like to use OO, but only when it
makes sense and in this case I do not see the need. I have never needed
to have more then one instance of a CGI object, or have found anything
really useful with using the OO syntax of CGI. Maybe I am wrong but I
think that it just adds to the amount of typing you do and I am lazy.

As far as the "why" goes, Perl has to look up which package the object
belongs to, then look up the method that you are calling and even
follow the the chain of inheritance if neccessary. I can't see this
adding too much time but I do know that it does add some time. As far
as memory, it all depends on the internal reprasentaion of the object.
If it is a hash it is slower and bigger but nicer to work with. An
array seems to be that fastest and smallest but less used. I prefer the
hash myself.

> 
> Since there is a global package 'main' can one script access
> another's
> variables if they know what they are. I haven't tried this yet and am
> just
> wondering if anyone knows.
> 
> Or is it the case that each script has it's own global package
> 'main'?
> 

each running script is being run by it's own Perl interpeter so there
is no way of using another scripts variables with out some magic. You
can use System V Shared Memory if you are on a system that supports it.
I find it easier to use Unix domain sockets and just communicate that
way. You can also try using Threads and make your scripts into one
script if that makes sense for what you are doing.

> 
> 
> 
> Last question...
> 
> 
> Can you create namespaces dynamically like so:
> 
> 
> $var = "A";
> 
> sub create_namespace($){
>   package $_[0];             # or maybe eval ($string = "package
> $_[0]");
> }
> 
> 
> sub write_to_package($$$){
>   my ($package,$var_name,$var_value) = @_;
>   %$package::$var_name = $var_value;
> }
> 
> 

I played around with it and this is the only way that I could get it to
work:

$x = "Apackage";      

eval "package $x; \$a = 1";

package main;

# can't access $x for some reason
$y = "Apackage::a";

print $$y, "\n"; # prints 1

I tried putting the $a after the eval but it did not work.

> 
> Just a few questions I've been thinking of.
> 

Cool!

> 
> Shay
> 
> 
> 


===
Peter J Jones
Surprise, Arizona
=== <((>< ===
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com




More information about the Phoenix-pm mailing list