Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^2: Detect Plugged USB Flash Drive / CD in CDROM Drive

by Perlbotics (Bishop)
on Nov 28, 2008 at 12:08 UTC ( #726564=note: print w/replies, xml ) Need Help??

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; ...

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://726564]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2022-06-29 07:32 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (96 votes). Check out past polls.