Skip to content

Instantly share code, notes, and snippets.

@ytnobody
Last active February 18, 2017 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytnobody/47ceda82f35a5323f90ddb92f00968fa to your computer and use it in GitHub Desktop.
Save ytnobody/47ceda82f35a5323f90ddb92f00968fa to your computer and use it in GitHub Desktop.
Azure Function向けのヘルパーライブラリもどきスニペット
use strict;
use warnings;
use utf8;
use JSON::PP;
use Encode;
### タイムゾーン定義 / Azure Functionsでは必須。
BEGIN {
$ENV{TZ} = "JST-9";
};
### 環境ファイル読み込み関数
sub read_from ($) {
my ($envname) = @_;
open my $fh, '<', $ENV{$envname} or die "$!";
my $content = do {local $/; <$fh>};
close $fh;
return $content;
}
### 環境ファイル書き込み関数
sub write_to ($$) {
my ($envname, $data) = @_;
open my $fh, '>', $ENV{$envname} or die "$!";
print $fh $data;
close $fh;
}
### HTTPクエリパラメータ取得
sub param ($) {
my $name = shift;
my $key = sprintf "REQ_QUERY_%s", uc($name);
Encode::decode_utf8($ENV{$key});
}
### HTTPヘッダ取得
sub header ($) {
my $name = shift;
my $key = sprintf "REQ_HEADERS_%s", uc($name);
$ENV{$key};
}
### レスポンスを返す関数
sub res ($$$) {
my ($code, $headers, $content) = @_;
my $res = JSON::PP->new->utf8(1)->encode({
status => $code,
headers => $headers,
body => $content
});
write_to(res => $res);
exit;
}
### 外部コマンド実行
sub run ($;@) {
my ($cmd, @opts) = @_;
my @res = `$cmd @opts`;
join "", @res;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment