Skip Menu |

This queue is for tickets about the Win32-DriveInfo CPAN distribution.

Report information
The Basics
Id: 40579
Status: open
Priority: 0/
Queue: Win32-DriveInfo

People
Owner: Nobody in particular
Requestors: cmanley [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 0.06
Fixed in: (no value)



Subject: Please return hashrefs and undefs instead of large arrays.
In methods (or currently functions) such as VolumeInfo, you can't at a later stage add extra information without breaking everyones' code. It's better practice to return objects or hashrefs instead of large arrays. That way it's easier for users to work with and you can eventually add to the result hashrefs without breaking code. For example if you later decide to add this kind of info too: http://www.codeproject.com/KB/cs/hard_disk_serialno.aspx Currently VolumeInfo returns a large array on success and a 1 element array (containing undef) on failuer. Both arrays since they contain at least one element evaluate to true. That's another odd thing about the result. See attachment for example.
Subject: t.pl
#!/usr/bin/perl -w use strict; use Data::Dumper qw(Dumper); # tab width = 4 { package Win32::DriveInfoNice; use Data::Dumper qw(Dumper); use base qw(Win32::DriveInfo); # Overrides parent method in order to returns a hash of information on success, else undef on error. sub VolumeInfo { my $proto = @_ && UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__; my $drive = shift; # This doesn't work because parent class isn't designed to be used in object oriented context... #my @info = $proto->SUPER::VolumeInfo($drive); my @info = Win32::DriveInfo::VolumeInfo($drive); # Return undef on error. unless(@info >= 4) { return undef; } # Coerce array result into a hashref. my @names = qw( VolumeName VolumeSerialNumber MaximumComponentLength FileSystemName ); my %result; foreach my $name (@names) { $result{$name} = shift(@info); } $result{'attr'} = \@info; # Inject more useful information (again can't use $proto because of non-OO design of parent class). my $type = $result{'DriveType'} = Win32::DriveInfo::DriveType($drive); my %types = ( 0 => 'unknown', # the drive type cannot be determined. 1 => 'unknown', # the root directory does not exist. 2 => 'removable', # the drive can be removed from the drive (removable). 3 => 'fixed', # the disk cannot be removed from the drive (fixed). 4 => 'network', # the drive is a remote (network) drive. 5 => 'optical', # the drive is a CD-ROM drive. 6 => 'ram', # the drive is a RAM disk. ); $result{'DriveTypeName'} = $types{$type} || 'unknown'; return \%result; } } package main; my @drives = Win32::DriveInfo::DrivesInUse(); print 'Drives in use: ' . join(', ', @drives) . "\n"; foreach my $drive (@drives) { print "Dump of VolumeInfo about drive $drive: "; print Dumper(Win32::DriveInfoNice->VolumeInfo($drive)); print 'Drive type: ' . Win32::DriveInfo::DriveType($drive) . "\n"; print "\n"; }