http://qs321.pair.com?node_id=726564


in reply to Re: Detect Plugged USB Flash Drive / CD in CDROM Drive
in thread Detect Plugged USB Flash Drive / CD in CDROM Drive

Your snipped might work for your PC only and you'll only see the last /dev/sr device - which might not be a CD or DVD at all. The following suggestion is only a tiny improvement:

use strict; # returns list of CD/DVD mountpoints or empty list sub scan_cddvd { my @drives = (); my $mtabfile = "/etc/mtab"; open my $mtfh, "<", $mtabfile or die "cannot open $mtabfile - $!"; while( <$mtfh> ){ my ($dev, $mountpoint, $fstype, $options) = split /\s+/; next unless $fstype =~ /udf|iso\d+/ && $options =~ /ro\b/; #ro: the /ro/-test above should skip CDRW too $mountpoint =~ s/\\(0\d+)/chr(oct($1))/eg; # resolve oct chars push @drives, $mountpoint; } close $mtfh; return @drives; } print "o <", join(">\no <", scan_cddvd), ">\n";
Things to note: uses strict; avoids "not found" as a special failure-indicator; returns list of mountpoints; expands all octal escapes; less HW-dependent (e.g. IDE, SCSI, SATA, ...), e.g. not restricted to /dev/sdb; tries a little harder to find a CD or DVD (iso/udf,ro), uses 3 param open and die; no globs (MYINPUTFILE); implicit use of $_; will fail for derviant mtab-formats; ...