Some common functions written in Perl

Source: Internet
Author: User
Tags flock

######################################## ###################
# Function: Get the current time
# Parameter: None
# Return Value: return time in the format of yyyymmddhhmmss
######################################## ###################
Sub get_time
{
My ($ sec, $ min, $ hour, $ day, $ month, $ year) = localtime (Time ());
$ Year + = 1900;
$ Month ++;
$ Today = sprintf ("% 04d % 02d % 02d % 02d % 02d % 02d", $ year, $ month, $ day, $ hour, $ min, $ Sec );
Return $ today;
}

######################################## ###################
# Function: Date of the current day
# Parameter: None
# Return Value: returns the current date in the format of yyyymmdd.
######################################## ###################
Sub get_date
{
My ($ sec, $ min, $ hour, $ day, $ month, $ year) = localtime (Time ());
$ Year + = 1900;
$ Month ++;
$ Today = sprintf ("% 04d % 02d % 02d", $ year, $ month, $ Day );
Return $ today;
}

######################################## ##################

# Load a Page Template
Sub template {
My ($ file) = @_;
$ File = $ tpl_dir. $ file;
Return "" If (! -F $ file );

Open (datafile, $ file) or perror ("unable to read template files $ File please contact the system administrator ");
My $ TXT = "";
While (<datafile> ){
$ TXT. = $ _;
}
Close (datafile );

Return $ txt;
}

# Replace the variables in the page template with the specified values
Sub set_var {
My ($ page, $ Param, $ value) = @_;
$ Page = ~ S // $ {$ Param}/$ value/g;
Return $ page;
}

# Remove spaces at the beginning and end of a string
Sub trim
{
My $ string = shift;
$ String = ~ S/^/S + //;
$ String = ~ S // s + $ //;
Return $ string;
}

######## File lock ############

Open cgifile, "> $ fileconfig" or perror ("sorry, the $ fileconfig configuration file cannot be published. Please try again later ");
Flock (cgifile, 2); # file lock
Print cgifile $ page;
Flock (cgifile, 8); # release a file lock
Close cgifile;

######################################## ##########

sub set_cookie_by_key
{< br> my ($ key, $ Val) =@_;
Print "Set-COOKIE: $ key = $ val; domain = $ domain; Path = $ path;/N ";
}< br> sub get_cookie_by_key
{< br> my ($ cookie_key) =@_;
my ($ chip, $ Val, % cookie );
foreach (split (//, $ ENV {'HTTP _ cookies'}) {
S // + // G;
($ chip, $ Val) = Split (/=/, $ _, 2); # splits on the first =.
$ chip = ~ S/% ([A-Fa-f0-9] {2})/Pack ("C", Hex ($1)/Ge;
$ val = ~ S/% ([A-Fa-f0-9] {2})/Pack ("C", Hex ($1)/Ge;
$ cookie {$ chip }. = "/1" If (defined ($ cookie {$ chip}); #/1 is the multiple separator
$ cookie {$ chip }. = $ val;
}< br> my $ temp;
while (My ($ K, $ v) = each % env)
{< br> $ temp. = "$ k = >v v
";
}< br> return $ cookie {$ cookie_key };< BR >}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.