Perl WorkNote1

===================================================== ==============
Get the current system path in perl

In perl programs, the current system path is sometimes used.

There are two ways to get the current Li Jing in perl:
1. Use the CWD package
     use Cwd;
     print getcwd;

2. Use the environment variable
     print $ENV{'PWD'};

I prefer to use environment variables so that no extra packages are introduced

3. Use the shell command
      print system("pwd");

===================================================== ==============
Open the file and write information into the file, if the file does not exist, it will be generated.
#!/usr/bin/perl -w
open DENO, ">/home/haiouc/dailyDeno";
print DENO ("hello, world!");
close DENO;

Open the specified file and print it out;
my $myfile;
open(myfile,"c://label.txt")||die "Cann't open it !";
while(my @content=)
     {
    print @content;
  }

===================================================== ==============
Write to file

open(filehandle,">pathname")
open(filehandle,">>pathname")

Multiple file handles can be opened simultaneously for read and write operations
open(source,"sourcefile")||die "$!";
open(dest,"destination")||die "$!";
@contents =;
print dest @contents;
close(dest);
close(source);

The above code snippet implements a simple file copy. Actually doing both reading and writing can shorten the routine a bit:
print dest ;
since the print function expects a list as its argument, it is evaluated in list context. When the angle bracket operator is evaluated in list context, the entire file is read and then output to the file handle DEST.
===================================================== ==============
Create a directory with the name after entering the name in the current directory, and set the permissions of the directory;

#!/usr/bin/perl -w
print "Directory to create?";
my $newdir=<STDIN>; ===>> use line input operator <STDIN>
chomp $newdir; ===>> chomp remove newline (if there is a newline at the end of the string, chomp can remove it, which is basically all it can do)    
mkdir($newdir, 0755)||die "Failed to create $newdir:$!";

===================================================== ==============
Get the current system time and output it in the specified format

#!/usr/bin/perl -w
my $date=localtime;
print "$date";       ===>>  Mon Oct  3 13:24:44 2011

============================== 

#!/usr/bin/perl -w
sub GetCurrentTime {
    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $time) = localtime();
    return sprintf( "%2.2d:%2.2d:%2.2d", $hour, $min, $sec); ===>> 12:52:56 Time format can be modified according to your needs
}
$buildTime = GetCurrentTime() ;
print "$buildTime";  
============================================= =======================

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327067040&siteId=291194637