#!/usr/bin/perl -w use strict; sub mod_perl { { my $static1; print "[", defined($static1) ? $static1 : "undef", "] "; BEGIN { $static1= "Hello" } sub sub1 { my $old= $static1; $static1= shift if @_; return $old; } } BEGIN { my $static2= "Hi"; print "<$static2> "; sub sub2 { my $old= $static2; $static2= shift if @_; return $old; } } print "(",sub1(shift),") "; print "(",sub2(shift),")\n"; } mod_perl(qw( Goodbye Bye )); mod_perl(qw( Morning Morn )); mod_perl(qw( Evening Eve )); mod_perl(); #### Variable "$static1" will not stay shared at static.pl line 9. [Hello] (Hello) (Hi) [undef] (Goodbye) (Bye) [undef] (Morning) (Morn) [undef] (Evening) (Eve) #### sub uniqueNonBlanks { my( $avItems )= @_; my @return; for my $item ( @$avItems ) { my %seen= ( "" => 1 ) BEGIN; push @return, $item unless $seen{$item}++; } return wantarray ? @return : \@return; } #### my @a= uniqueNonBlanks("Why","","I","Why"); my @b= uniqueNonBlanks("Why","You","Be","I"); $"= ","; print "(@a) (@b)\n"; #### sub validateOptions { my( $hvOptions, $hvUserOpts )= @_; for my $key ( keys %$hvOptions ) { $hvOptions->{$key}= delete $hvUserOpts->{$key} if exists $hvUserOpts->{$key}; } if( %$hvUserOpts ) { my $subName= (caller(1))[3]; croak "$subName: Invalid options (", join(",",keys %$hvUserOpts),")"; } return $hvOptions; } BEGIN { my %defaults= ( Access=>READ_ONLY, Delimiter=>"/" ); sub Open { my( $keyName, $hvUserOpts )= @_; my $hvOpts= validOptions( {%defaults}, $hvUserOpts ); # ... } } BEGIN { my %defaults= ( Delimiter=>"/", Translate=>"text" ); sub Read { my( $handle, $hvUserOpts )= @_; my $hvOpts= validOptions( {%defaults}, $hvUserOpts ); # ... } } #### our %Open_defaults= ( Access=>READ_ONLY, Delimiter=>"/" ); sub Open { my( $keyName, $hvUserOpts )= @_; my $hvOpts= validOptions( {%Open_defaults}, $hvUserOpts ); # ... } our %Read_defaults= ( Delimiter=>"/", Translate=>"text" ); sub Read { my( $handle, $hvUserOpts )= @_; my $hvOpts= validOptions( {%Read_defaults}, $hvUserOpts ); # ... } #### sub mod_perl { our $static= "Hi"; sub sub1 { my $old= $static1; $static1= shift if @_; return $old; } # ... } #### sub mod_perl { { my $static1 BEGIN; $static1= "Hi"; sub sub1 { my $old= $static1; $static1= shift if @_; return $old; } } # ... } #### sub mod_perl { sub sub1 { my $static1= "Hi" STATIC; my $old= $static1; $static1= shift if @_; return $old; } # ... }