Saturday, October 16, 2010

The cdrom checker perl code.......

Perl program strikes again....

Considering  a situation in which you wanted to know about the contents of the CDROM or DVD. You wanted to list all the files in the removable media. For this necessity you can hold the perl program iin tight..

The following perl code will scans the device file and if any cdrom or dvd is in the drive it will print the files in it. All we wanted to run the perl code in a terminal..

--------------------------------------------------------------------------
system("ls /media > mediaoutput");
if(open(MY,"mediaoutput"))
{
  $line=<MY>;
  while($line ne "")
   {
     if($line == "cdrom")
      {
        system ( "ls /media/cdrom > lsout");
       }
       $line=<MY>;
    }
}
if (open(LS,"lsout"))
{
$lin = <LS>;
while($lin ne "")
   {
      print $lin;
      $lin=<LS>;
    }
}
------------------------------------------------------------------------
It can make use of UNIX system call  to work as if we are on terminal. The above code will first of all call the ls command over the medial file and stores the ls output to a file. The it  will scan for the file called cdrom. If found it will again run ls command over the cdrom to print out the list of files to another output file. The last IF loop is for printing the contents of the file output due to the command execution  of system call over the cdrom. If there is no any  devices are connected to the computer nothing  will be happened. The notion $line=<MY> is the method of giving the input file line by line

No comments:

Post a Comment