How can I detect the operating system in Perl?

后端 未结 11 1471
我在风中等你
我在风中等你 2020-12-04 16:41

I have Perl on Mac, Windows and Ubuntu. How can I tell from within the script which one is which? Thanks in advance.

Edit: I was asked what I am doi

11条回答
  •  一向
    一向 (楼主)
    2020-12-04 16:58

    #Assign the $home_directory variable the path of the user's home directory
    my $home_directory = ($^O eq /Win/) ? $ENV{HOMEPATH} : $ENV{HOME};
    #Then you can read/write to files in the home directory
    open(FILE, ">$home_directory/my_tmp_file");
    print FILE "This is a test\n";
    close FILE;
    #And/or read the contents of the file
    open(FILE, "<$home_directory/my_tmp_file");
    while (){
        print $_;
    }
    close FILE;
    

提交回复
热议问题