Baphi has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, and happy newyear. I just wrote my own installation program for my program which gets installed from a CD to the C: drive, everything works great on my computer but since I must specify the drive from which to move the files, I need to figure out what the letter of the CD drive of the computer that the installation comes in contact with so I can set it to a variable, my questions to you are :

1: how can I find the CD drive letter in perl

2: how can I use system(), and from that put the result in a variable. for example system(dir); is it posible to set the result into your perl script?

Replies are listed 'Best First'.
Re: find CD drive letter
by ikegami (Patriarch) on Jan 03, 2010 at 04:37 UTC

    First, the question makes no sense. It presumes there's one CD drive, no more no less. That's often not the case, especially since many USB keys appear as CD drives to the computer.

    It seems that the real question is that you want to locate the installer, and from there the files to install. You can find the path to the installer via $0

    use Cwd qw( realpath ); use File::Basename qw( dirname ); my $path_to_installer = dirname(realpath($0));

    As for your second question, look into qx, documented in perlop

Re: find CD drive letter
by Jenda (Abbot) on Jan 03, 2010 at 11:23 UTC
    use Win32::FileOp qw(Substed); sub findCD { for ('A'..'Z') { return $_.':' if (Substed($_))[1] =~ /^CdRom/ }; return }; print findCD();

    This finds the first CD drive on your PC. Whether that's of any use, I'm not sure. The program may be installed from a different place. And if the installation is already running, you can find out the path to the executable. See for example FindBin.

    For the second question ... if by "result" you mean the stuff the program prints then you are looking for "backticks": $output = `the_program and parameters`;. See "Quote-Like Operators" in perlop.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Re: find CD drive letter
by ww (Archbishop) on Jan 03, 2010 at 04:05 UTC
    This is pretty much nonsense.

    For the perl part of the question, how do you know the computer on which your program is being installed will even have Perl?

    However, assuming it does, you can "put the result in a variable. for example system(dir);" with a simple assignment, like this:

    perl -e "my $dir=system(dir);print $dir

    update2: Wrong! See BrowserUK's below for clarification.

    But that's not going to get you the drive letter of the CD; it's going to get you the directory of the Drive:\path\from\which\you\run\the\script.

    So, it makes more sense (IMO), to use autorun and/or direct the user installing the program to provide the CD's drive letter.

    Update: Above lists contents (-d && -D) on W2k (plus the errno, as jwkrahn points out); on *nix, perl -e 'my $dir=system(ls);print $dir;' does likewise. Add "\n" at the end if you want the errno on a line above the next prompt.

    Again, WRONG; as in update2. The only thing assigned to $dir is the errno, "0" in these cases; the print $dir does NOT do what I asserted.

      my $dir=system(dir);

      system returns an error number, not a text string from the command run.

        As stated update: following is accurate as to output, but NOT as to the process. See jwkrahn's (above) and BrowserUK's (below).
        on w2k w/ perl, v5.8.8 built for MSWin32-x86-multi-thread (with 33 registered patches) ... Binary build 819 [267479] provided by ActiveState....

        C:\Documents and Settings\ww>perl -e "my $dir=system(dir);print $dir Volume in drive C has no label. Volume Serial Number is 704A-B003 Directory of C:\Documents and Settings\ww 01/03/2010 06:29a <DIR> . 01/03/2010 06:29a <DIR> .. 07/16/2007 06:03a 107 .bps 12/29/2009 07:46p <DIR> .gimp-2.6 08/13/2007 11:40a <DIR> .housecall6.6 12/23/2009 10:55a 1,480 .recently-used.xbel 11/14/2009 10:44a <DIR> .thumbnails ... #more files and dirs 02/16/2008 11:37a <DIR> Start Menu 06/29/2007 10:15p 145 t.net-1.bps 06/29/2007 10:13p 145 t.net.bps 20 File(s) 8,026,977 bytes 10 Dir(s) 6,957,117,440 bytes free 0 C:\Documents and Settings\ww>