Windows 10 Pro + VsCode で Strawberry Perl を少し試してみる

Windows 10 Pro + VsCode で Strawberry Perl を少し試してみる

 

随分前に触ったっきりのPerlなんだけど、今はどうなんだろう?と少し試してみる。

※DB関連は DBI::DBD で良いのかな?というのもあるんだが

ActivePerlは商用ライセンスじゃないと使いづらい感じなので、Strawberryを試すことにする

コンパイルログは出ないのだが、CPANモジュールがわりと使えそうな感じかな?

※ネットワーク系ゴリゴリだと素直にLinuxベースで行うべきだろうけどな

 

参考にしたサイトはこちら

ActivePerlを卒業して、Strawberry Perlを使用することにした

PerlからODBCでSQL Serverのデータを取得

接続するODBCWindows[SQL Server ログイン] ダイアログ ボックス (ODBC)

 

The Perl for MS Windows, free of charge!

より Recommended version:Strawberry Perl 5.26.1.1 (64bit) をクリックしてDLする

strawberry-perl-5.26.1.1-64bit.msi を実行して適当なフォルダにインストールする

※自分は C:\Dev\tools\Strawberry

 

環境変数に 以下を追加する

C:\Dev\tools\Strawberry\perl\bin

C:\Dev\tools\Strawberry\c\bin

 

コンソールを起動して以下の動作を確認する

> perl -v

This is perl 5, version 26, subversion 1 (v5.26.1) built for MSWin32-x64-multi-thread

Copyright 1987-2017, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the

GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on

this system using "man perl" or "perldoc perl". If you have access to the

Internet, point your browser at http://www.perl.org/, the Perl Home Page.

 

> gcc -v

Using built-in specs.

COLLECT_GCC=C:\Dev\tools\Strawberry\c\bin\gcc.exe

Thread model: posix

gcc version 7.1.0 (x86_64-posix-seh, Built by strawberryperl.com project)

 

VsCodeの設定

拡張機能より

 Perl Debug をインストールする

 Code Runner をインストールする

・フォルダを作成する

・ファイル -> フォルダを選ぶ より上記フォルダを選択

・CTRL+SHIFT+D(デバッグボタン)

 ->launch.jsonの構成や修正を押下して perl を選択する

use strict;
use warnings;

print "Hello 日本語";

・右クリック->Run Code(Ctrl+Alt+N)で実行

 

SQL Server との接続を確認

対象データはこちらで作成したもの

 Excel VBA での Transaction について

コンソールより、以下を順次実行する

> cpanm DBD::ODBC

--> Working on DBD::ODBC

Fetching http://www.cpan.org/authors/id/M/MJ/MJEVANS/DBD-ODBC-1.58.tar.gz ... OK

Configuring DBD-ODBC-1.58 ... OK

Building and testing DBD-ODBC-1.58 ... OK

Successfully installed DBD-ODBC-1.58 (upgraded from 1.56)

1 distribution installed

 

> cpanm DDP

DDP is up to date. (undef)

 

> cpanm DBI

--> Working on DBI

Fetching http://www.cpan.org/authors/id/T/TI/TIMB/DBI-1.641.tar.gz ... OK

Configuring DBI-1.641 ... OK

Building and testing DBI-1.641 ... OK

Successfully installed DBI-1.641 (upgraded from 1.637)

1 distribution installed

use strict;
use utf8;
use DBI;

my $dbh=DBI->connect('dbi:ODBC:SQLSVODBC64','user','password') or die $!;
my $sth=$dbh->prepare("select * from POSTAL") or die $dbh->errstr;

# 出力エラー対策
$sth->{LongTruncOk}=1;
$sth->{LongReadLen}=2000000;

$sth->execute or die $dbh->errstr;

while(my $arrayref = $sth->fetchrow_arrayref){
        use DDP;
        p $arrayref;
}

$sth->finish;
$dbh->disconnect;

[Running] perl "d:\Dev\plsamp\0002.pl"
Wide character in print at C:/Dev/tools/Strawberry/perl/vendor/lib/Data/Printer.pm line 181.
\ [
    [0] "9200000 ",
    [1] undef,
    [2] "金沢市                                     ",
    [3] "以下に掲載がない場合                              ",
    [4] undef,
    [5] undef
]
~