perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stathy Touloumis" <stathy.toulou...@edventions.com>
Subject RE: [OT] Dynamically changing current package
Date Fri, 02 Feb 2001 17:09:38 GMT
Probably just never assumed to be used this way.  Do not see to much
documentation on using the Symbol table directly.  Here is something strange
as well.  Try copying a package and then creating an object from it . . .

Really funky

Think I might post this to the p5p list

-----------------------------------------------------
#!/usr/bin/perl -w
#

## ASSIGN TEST TO TEST2
*{ 'Test2::' } = *{ 'Test::' };

## LETS CREATE SOME OBJECTS
my $obj = Test->new;
print 'TEST PACKAGE OBJECT : ', ref($obj), "\n\n";

my $obj2 = Test2->new;
print 'TEST2 PACKAGE OBJECT : ', ref($obj2), "\n\n";

## FYI

print '- ' x 10, 'TEST', ' -' x 10, "\n";
print map { "$_ - $Test::{$_}\n" } keys %Test:: ;

print '- ' x 10, 'TEST2', ' -' x 10, "\n";
print map { "$_ - $Test2::{$_}\n" } keys %Test:: ;

BEGIN {
	package Test;

	sub new {
		my $class =shift;

		print "SETTING OBJECT TO CLASS : $class\n";

		bless my $obj = {}, $class;
	}

}
-------------------------------------------------------

> > > NewPackage will then contain all things in TestPackage....
> >>
> >> Then you can redefined everything in TestPackage without affecting
> >> anything in NewPackage.
> >
> >Actually it will, if you were to delete NewPackage, TestPackage would not
> >contain anything.  Modifying 'thingys' in TestPackage would modify
> >NewPackage.
>
> Actually I investigated this a little more and discovered some
> strange things...
>
> If you check out the results, copying the package at the package
> level cause scalars to be redefined and subroutines to created as
> references.  Where as copying the the package at the glob level
> caused the the subroutines to be redefined and the scalars to be
> references...
>
> I wonder if this is a problem with perl?
>
> Robert Landrum
> --- results ---
>
> # perl n.pl 1
> NewPackage::x was defined
> NewPackage::june was defined
>
> $NewPackage::x was set to 100
> $Test::x now contains 100
>
> Now Deleting Test package
> Test::x was deleted from the STASH
> Test::june was deleted from the STASH
>
> $NewPackage::x was set to 100
> $Test::x now contains 100
>
> Now Listing NewPackage
> NewPackage::x
> NewPackage::june
>
> Now Listing Test
>
> # perl n.pl
> NewPackage:: was defined as *{Test::}
>
> $NewPackage::x was set to 100
> $Test::x now contains 1
>
> Now Deleting Test package
> Test::x was deleted from the STASH
> Test::june was deleted from the STASH
>
> $NewPackage::x was set to 100
> $Test::x now contains 1
>
> Now Listing NewPackage
>
> Now Listing Test
>
>
>
> --- Contents of n.pl ---
> #!/usr/bin/perl
> package Test;
>
> our $x = 1;
> sub june {
>
> }
>
> package main;
>
> if($ARGV[0]) {
>      for my $name (keys %{Test::}) {
>          *{"NewPackage::".$name} = *{"Test::".$name};
>          print "NewPackage::".$name." was defined\n";
>      }
>      print "\n";
> } else {
>      *{NewPackage::} = *{Test::};
>      print "NewPackage:: was defined as \*{Test::}\n";
>      print "\n";
> }
> $NewPackage::x = 100;
>
> print "\$NewPackage::x was set to $NewPackage::x\n";
> print "\$Test::x now contains $Test::x\n";
> print "\n";
> print "Now Deleting Test package\n";
> for my $name (keys %{Test::}) {
>      delete $Test::{$name};
>      print "Test::".$name." was deleted from the STASH\n";
> }
> print "\n";
>
> print "\$NewPackage::x was set to $NewPackage::x\n";
> print "\$Test::x now contains $Test::x\n";
> print "\n";
> print "Now Listing NewPackage\n";
> for my $name (keys %{NewPackage::}) {
>      print "NewPackage::".$name."\n";
> }
> print "\n";
> print "Now Listing Test\n";
> for my $name (keys %{Test::}) {
>      print "Test::".$name."\n";
> }
> print "\n";
>


Mime
View raw message