I've got a class/package called Backup_Drive. It records details of backup files from a WMI Query. I write the following to instantiate it:

$drive = Coles_Backup_Drive->new( \$disk, SPACE_THRESHOLD );

Now if I add a file reference to it calling the add_full_backup method directly: $drive->add_full_backup( $database_name, $file ); it records the file details into the Class's %_last_full_backups hash and @_full_backup_files array and when I leave the method's scope it doesn't lose the detail. I can keep adding file details.

If I add file by add_file method then the $file reference is passed to the add_full_backup method internally but once out of the latter method's scope the array and hash become empty.

I've tried sending the file as a reference of a reference. I can't take away the my clause because then use strict cracks it. I've tried $self->add_full_backup( $database_name, $file ); I don't understand why the hash and array lose their detail after add_full_backup method is called indirectly but keep the details for the life of the class if called directly.

Below is the class code:

package Backup_Drive; use strict; use constant LAST_MODIFIED => 0; use constant FILE_SIZE => 1; use constant TRANSACTION_LOG => 'trn'; use constant FULL_BACKUP => 'bak'; # make database backups private my %_last_full_backups = (); my @_full_backup_files = []; my %_trn_backups =(); my $_space_threshold; sub new{ my ( $class, @arg ) = @_; my $self = bless $arg[0], $class; $_space_threshold = $arg[1]; return $self; } sub add_file{ ### THIS IS THE PROBLEM WHEN PASSING DETAIL FROM THIS METHOD TO add_fu +ll_backup ### # add the file to the relevant file type data my( $self, $database_name, $file ) = @_; if( lc( $file->Extension ) eq FULL_BACKUP ){ add_full_backup( $self, $database_name, $file ); } else { add_trn_log( $self, $database_name, $file ); } print "wait\n"; } sub add_full_backup{ my( $self, $database_name, $file ) = @_; # capture the last backup details if( exists $_last_full_backups{ $database_name } ){ # record the file size only if the database backup is more rec +ent the the one recorded if( $_last_full_backups{ $database_name }->[ LAST_MODIFIED ] < + $file->LastModified ){ # one file I found returned an undefined value for its siz +e. So I defaulted the size to zero. $_last_full_backups{ $database_name } = [ $file->LastModif +ied, defined $file->FileSize ? $file->FileSize : 0 ]; } } else { # add new details $_last_full_backups{ $database_name } = [ $file->LastModified, + defined $file->FileSize ? $file->FileSize : 0 ]; } # add file to file list push ( @_full_backup_files, $file ); print "test\n"; } sub add_trn_log{ my( $self, $database_name, $file ) = @_; if( exists $_trn_backups{ $database_name } ){ $_trn_backups{ $database_name } = [ \$file ]; } else { push( @{ $_trn_backups{ $database_name } }, \$file ); } } sub get_last_backup_modified{ # return the last modified date for the passed in database my( $self, $database_name ) = @_; $_last_full_backups{ $database_name }->[ LAST_MODIFIED ]; } sub get_last_backup_file_size{ # return the last modified date for the passed in database my( $self, $database_name ) = @_; $_last_full_backups{ $database_name }->[ FILE_SIZE ]; } sub need_drive_space{ # return whether the drive needs space to keep it above the space +warning threshold. my $self = shift; if( _database_space_required() > $$self->FreeSpace ){ return 1; } return 0; } sub _database_space_required{ my $self = shift; my $total = 0; my $key; for $key ( keys %_last_full_backups ){ $total += get_last_backup_file_size( $key ); } $total += $_space_threshold; # keep above threshold warning siz +e return $total; } #__END__ 1;

In reply to Is it a reference issue or what? by OzVegan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.