# Perl module variables



## Alt (Jun 24, 2010)

Hi i have a problem with module variables. Im trying to find all scalar/array/hash variables in a module, then undef them to destroy all objects, but i want to save subs and other module things (so Symbol::delete_package is not thing i want to do).
Got symbol table this way

```
perl -MFcntl -e 'foreach (keys(%{'main::Fcntl::'})) { print $_," - ", ${'main::Fcntl::'}{$_}, "\n"; }'
```
This gives me otput like
	
	



```
...
S_IFENFMT - *Fcntl::S_IFENFMT
O_TEXT - *Fcntl::O_TEXT
...
```
Now how can i check type of element e.g. Fcntl::S_IFENFMT ? Seems ref() not working on them
Maybe there is other methods to return module to "just-loaded" state?


----------



## ProFTP (Jun 26, 2010)

To be honest I do not understand very well what to do ...

but about:

1) 
Class::Accessor - Automated accessor generation
http://search.cpan.org/~kasei/Class-Accessor-0.34/lib/Class/Accessor.pm


```
$Ñ->bal_lba = 1
etc
```


2) http://www.opennet.ru/base/dev/global_export_perl.txt.html

3) 

... or use inheritance



			
				Alt said:
			
		

> Now how can i check type of element e.g. Fcntl::S_IFENFMT ? Seems ref() not working on them



http://search.cpan.org/~gbarr/Scalar-List-Utils-1.23/lib/Scalar/Util.pm


```
reftype EXPR

    If EXPR evaluates to a reference the type of the variable referenced is returned. Otherwise undef is returned.

        $type = reftype "string";           # undef
        $type = reftype \$var;              # SCALAR
        $type = reftype [];                 # ARRAY

        $obj  = bless {}, "Foo";
        $type = reftype $obj;               # HASH
```

http://www.perlmonks.org/index.pl?node_id=127079

4)
more can be found at Moose MooseX

http://search.cpan.org/search?query=Moose&mode=all
http://search.cpan.org/search?query=MooseX&mode=all

5) moose: meta programming, etc

http://search.cpan.org/search?query=moose+meta&mode=all


```
package MyClass;

    use Moose;
    use MooseX::MethodAttributes;

    sub foo : Bar Baz('corge') { ... }

    my $attrs = MyClass->meta->get_method('foo')->attributes; # ["Bar", "Baz('corge')"]
```


httpd URL patch 
(MVC Catalyst)


```
package MyApp::Controller::Foo;
  sub bar : Local   {} # site.ru/foo/bar/*
  sub baz : Global {} # site.ru/baz/*
  sub some : Path('one/two') {} # site.ru/foo/one/two/*
  sub some2 : LocalRegex('^widget(\d+)$') {} # site.ru/foo/widget123 
  sub bar2 : Local :Args(0)  {} #  site.ru/foo/bar2
```

6) MVC Catalyst http://search.cpan.org/~bobtfish/Catalyst-Runtime-5.80024/lib/Catalyst.pm

7) http://search.cpan.org/~teverett/Class-Prototyped-1.11/lib/Class/Prototyped.pm



> DESCRIPTION
> 
> This package provides for efficient and simple prototype-based programming in Perl. You can provide different subroutines for each object, and also have objects inherit their behavior and state from another object.
> 
> The structure of an object is inspected and modified through mirrors, which are created by calling reflect on an object or class that inherits from Class:rototyped.


----------



## Alt (Jun 26, 2010)

Sry but its not what i searching for =(

In other words my goal is: I have a package name in '$package' variable. I just want to undef all variables in this package (from another package). Names of variables are unknown at build time (package loads dynamically)


----------



## ProFTP (Jun 26, 2010)

http://forum.vingrad.ru/index.php?showtopic=258431&view=findpost&p=1863172

?


```
perldoc Data::Dumper


  DB<17> p Dumper $_
$VAR1 = bless( {
                 'Qualifiers_' => bless( {
                                           'Count' => 4
                                         }, 'Win32::OLE' ),
                 'Properties_' => bless( {
                                           'Count' => 45
                                         }, 'Win32::OLE' ),
                 'Methods_' => bless( {
                                        'Count' => 6
                                      }, 'Win32::OLE' ),
                 'Derivation_' => [
                                    'CIM_Process',
                                    'CIM_LogicalElement',
                                    'CIM_ManagedSystemElement'
                                  ],
                 'Path_' => bless( {
                                     'Path' => '\\\\ARTO-LAPTOP\\root\\cimv2:Win32_Process.Handle="0"',
                                     'RelPath' => 'Win32_Process.Handle="0"',
                                     'Server' => 'ARTO-LAPTOP',
                                     'Namespace' => 'root\\cimv2',
                                     'ParentNamespace' => 'root',
                                     'DisplayName' => 'WINMGMTS:{authenticationLevel=pktPrivacy,impersonationLevel=impersonate}!\\\\ARTO-LAPTOP\\root\cimv2:Win
32_Process.Handle="0"',
                                     'Class' => 'Win32_Process',
                                     'IsClass' => 0,
                                     'IsSingleton' => 0,
                                     'Keys' => bless( {
                                                        'Count' => 1
                                                      }, 'Win32::OLE' ),
                                     'Security_' => bless( {
                                                             'ImpersonationLevel' => 3,
                                                             'AuthenticationLevel' => 6,
                                                             'Privileges' => bless( {

      'Count' => 0

    }, 'Win32::OLE' )
                                                           }, 'Win32::OLE' ),
                                     'Locale' => '',
                                     'Authority' => ''
                                   }, 'Win32::OLE' ),
                 'Security_' => bless( {
                                         'ImpersonationLevel' => 3,
                                         'AuthenticationLevel' => 6,
                                         'Privileges' => bless( {
                                                                  'Count' => 0
                                                                }, 'Win32::OLE')
                                       }, 'Win32::OLE' ),
                 'SystemProperties_' => bless( {
                                                 'Count' => 10
                                               }, 'Win32::OLE' )
               }, 'Win32::OLE' );
```


----------



## Alt (Jun 26, 2010)

Nonono i want to clean package namespace, not an object or class
This guy wants to work with blessed object - its elemtary thing cus he can do `foreach (grep {/element/} keys %$class)`. In opposite i want to walk package's variables not object variables


UPD: Partially solved. To wipe module vars its possible to use (dont know how 'clean' it is, but works)

```
my $pkg = 'modulename';
foreach (keys(%{'main::'.$pkg.'::'})) {
    ${${'main::'.$pkg.'::'}{$_}}=undef if defined ${${'main::'.$pkg.'::'}{$_}};
    @{${'main::'.$pkg.'::'}{$_}}=undef if defined @{${'main::'.$pkg.'::'}{$_}};
    %{${'main::'.$pkg.'::'}{$_}}=undef if defined %{${'main::'.$pkg.'::'}{$_}};
}
```


----------

