| 1 | package HTML::Pen::Utils ; | ||
| 2 | |||
| 3 | require Exporter; | ||
| 4 | |||
| 5 | our @ISA = qw(Exporter); | ||
| 6 | |||
| 7 | # Items to export into callers namespace by default. Note: do not export | ||
| 8 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 9 | # Do not simply export all your public functions/methods/constants. | ||
| 10 | |||
| 11 | # This allows declaration use Contessa::Demo ':all'; | ||
| 12 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 13 | # will save memory. | ||
| 14 | our %EXPORT_TAGS = ( 'all' => | ||
| 15 | [ qw( split memstat mymkdir imagefile randomize ) ] ); | ||
| 16 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 17 | our @EXPORT = qw( ); | ||
| 18 | our $VERSION = '0.01'; | ||
| 19 | |||
| 20 | |||
| 21 | sub mysplit { | ||
| 22 | my $pat = shift ; | ||
| 23 | my $t = shift ; | ||
| 24 | return ( $t ) if ref $t eq 'SCALAR' ; | ||
| 25 | return () if ref $t ; | ||
| 26 | |||
| 27 | my @rv ; | ||
| 28 | while ( $t =~ s/^(.*?)($pat)//s ) { ; | ||
| 29 | my @o = ( $1, $2 ) ; | ||
| 30 | push @rv, $o[0], \( $o[1] ) ; | ||
| 31 | } ; | ||
| 32 | push @rv, $t ; | ||
| 33 | return @rv ; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub split { | ||
| 37 | my $t = shift @_ ; | ||
| 38 | my @rv ; | ||
| 39 | |||
| 40 | while ( @_ ) { | ||
| 41 | if ( @rv ) { | ||
| 42 | my $p = shift @_ ; | ||
| 43 | @rv = map { mysplit( $p, $_ ) } @rv ; | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | @rv = mysplit( shift @_, $t ) ; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | return map { ref $_? $$_: $_ } @rv ; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub memstat { | ||
| 54 | if( ! open( _INFO, "< /proc/$$/statm" ) ) { | ||
| 55 | warn "Couldn't open /proc/$$/statm [$!]" ; | ||
| 56 | return {} ; | ||
| 57 | } | ||
| 58 | |||
| 59 | my @info = split( /\s+/, <_INFO> ) ; | ||
| 60 | close( _INFO ) ; | ||
| 61 | return { size => $info[0] * 4, | ||
| 62 | resident => $info[1] * 4, | ||
| 63 | shared => $info[2] * 4 } ; | ||
| 64 | } | ||
| 65 | |||
| 66 | ## utility | ||
| 67 | sub mymkdir { | ||
| 68 | my $dir = shift ; | ||
| 69 | mkdir $dir unless -d $dir ; | ||
| 70 | return unless scalar @_ ; | ||
| 71 | |||
| 72 | my $next = shift ; | ||
| 73 | return mymkdir( join( '/', $dir, $next ), @_ ) ; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub imagefile { | ||
| 77 | my $fn = shift ; | ||
| 78 | my $buff = shift ; | ||
| 79 | |||
| 80 | $fn =~ s|^/|| ; | ||
| 81 | my @fn = split m|/|, $fn ; | ||
| 82 | pop @fn ; | ||
| 83 | mymkdir( @fn ) ; | ||
| 84 | |||
| 85 | open $f, "> $fn" ; | ||
| 86 | print $f $buff ; | ||
| 87 | close $f ; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub randomize { | ||
| 91 | # srand() ; | ||
| 92 | |||
| 93 | my @p = @_ ; | ||
| 94 | my @rv = () ; | ||
| 95 | push @rv, splice @p, int( rand( scalar @p ) ), 1 while @p ; | ||
| 96 | return @rv ; | ||
| 97 | } | ||
| 98 | |||
| 99 | 1 | ||
| 100 | __END__ |