Perl で、1GB のデータ(テキストファイル)を作成する方法

次のコードを makefile.pl などというファイル名で保存して、chmod +x ./makefile.pl し、そして ./makefile.pl を実行すれば makefile.pl と同じディレクトリ上に sample.txt に1GiB (ギビバイト=2^30)ぴったりのデータ(「a」の文字で埋め尽くされた テキストファイル)が作成されていると思う。プログラム的には、例えば 1KiB のダミーデータを変数($data)に入れてそれを 1024 x 1024 回書き込んだ方が 1文字ずつ 2^30 回書き込むよりも速いと思うが、試してみたらバァッファオーバーフローしてるのか?うまくいかなかった。
ちなみに、2^30 = 1ギガバイト = 1,073,741,824 バイト = 1024 バイト x 1024 キロバイト x 1024 メガバイト

#!/usr/bin/perl

open(OUT, '>sample.txt');
for(my $i = 0; $i < 2^30; $i++) { print OUT 'a'; }
close(OUT);

1;

次のコードを makefile.pl などというファイル名で保存して、chmod +x ./makefile.pl し、そして ./makefile.pl を実行すれば makefile.pl と同じディレクトリ上に sample.txt に1GiB (ギビバイト=2^30)ぴったりのデータ(「a」の文字で埋め尽くされた テキストファイル)が作成されていると思う。プログラム的には、例えば 1KiB のダミーデータを変数($data)に入れてそれを 1024 x 1024 回書き込んだ方が 1文字ずつ 2^30 回書き込むよりも速いと思うが、試してみたらバァッファオーバーフローしてるのか?うまくいかなかった。
ちなみに、2^30 = 1ギガバイト = 1,073,741,824 バイト = 1024 バイト x 1024 キロバイト x 1024 メガバイト

#!/usr/bin/perl

open(OUT, '>sample.txt');
for(my $i = 0; $i < 2^30; $i++) { print OUT 'a'; }
close(OUT);

1;

PHP で、ディレクトリの中のファイル名を取得する方法

こんな感じ。 !ereg('(^\.$)|(^\.\.$)', $file) とある行は、カレントディレクトリ(.)と、親ディレクトリを除いて処理するためのもの。 $filename[] の配列中に、ファイル一覧が入る。
<?php

$dir_path
= 'your_directory';
$filename = array();

$dir = dir($dir_path);
while(
$file = $dir->read()) {
  if(!
ereg('(^\.$)|(^\.\.$)', $file)) {
   
$filename[] = $file;
  }
}
$dir->close();

?>
こんな感じ。 !ereg('(^\.$)|(^\.\.$)', $file) とある行は、カレントディレクトリ(.)と、親ディレクトリを除いて処理するためのもの。 $filename[] の配列中に、ファイル一覧が入る。
<?php

$dir_path
= 'your_directory';
$filename = array();

$dir = dir($dir_path);
while(
$file = $dir->read()) {
  if(!
ereg('(^\.$)|(^\.\.$)', $file)) {
   
$filename[] = $file;
  }
}
$dir->close();

?>

Perl で、Base64 のファイルをデコードして元のバイナリファイルに戻す方法

メールに添付されてきた .pdf ファイルが巨大すぎてメールサーバによって分割されて自分のところに配信された結果、Base64 のデータがバラバラに届いたケースを考える。 このような場合は Perl が活躍する。まず、バラバラになった Base64 のファイルは仕方がないのでエディタを使って手作業でくっつけて 1つのファイルにする。そのあと、以下のコマンドを実行すればよい。 使用するモジュール use MIME::Base64;
perl -MMIME::Base64 -ne 'print decode_base64($_)'
< くっつけたBase64ファイル名.txt > 出力ファイル名.pdf

(上は、すべて1行に書く)
メールに添付されてきた .pdf ファイルが巨大すぎてメールサーバによって分割されて自分のところに配信された結果、Base64 のデータがバラバラに届いたケースを考える。 このような場合は Perl が活躍する。まず、バラバラになった Base64 のファイルは仕方がないのでエディタを使って手作業でくっつけて 1つのファイルにする。そのあと、以下のコマンドを実行すればよい。 使用するモジュール use MIME::Base64;
perl -MMIME::Base64 -ne 'print decode_base64($_)'
< くっつけたBase64ファイル名.txt > 出力ファイル名.pdf

(上は、すべて1行に書く)

Perl で、配列中の重複レコードを削除するには?

配列の中にある重複するレコードを削除する場合、Perl なのでいろいろな方法があると思うが、以下のコードを試してみて欲しい。
#/usr/bin/perl -w

my @array = ('a', 'b', 'c', 'a');

my %count;
@array = grep {!$count{$_}++} @array;

print @array; # 出力は abc となる。

1;
配列の中にある重複するレコードを削除する場合、Perl なのでいろいろな方法があると思うが、以下のコードを試してみて欲しい。
#/usr/bin/perl -w

my @array = ('a', 'b', 'c', 'a');

my %count;
@array = grep {!$count{$_}++} @array;

print @array; # 出力は abc となる。

1;

Perl で、複数のファイルまたがる文字列を置換するには?

以下のように、replace.conf のハッシュ変数に置換前・置換後の文字列を書いておき、replace.pl を実行する。 replace.conf の内容
#/usr/bin/perl -w

use strict;

our $HOME_DIR = '/home/youraccount';
our $DIR = "$HOME_DIR/targetdirectory";
our %WORDS = (

                'ブラウザー'   # 置換前の文字列
            =>   'ブラウザ',   # 置換後の文字列

                'FTP'       # 置換前の文字列
            =>     'FTP',    # 置換後の文字列

# ... 以下、

                '置換前の文字列1'
            =>  '置換後の文字列1',

                '置換前の文字列2'
            =>  '置換後の文字列2'

# ... という形で追加していけばよい。

        );
1;
replace.pl の内容
#!/usr/bin/perl

require './jcode.pl';
require './replace.conf';

opendir(DIR, $DIR) or die "Couldn't open $DIR directory.";
my @files = readdir(DIR);
closedir(DIR);
foreach my $file (@files) {
    if(!(-d $file) && $file ne '.' && $file ne '..' && $file ne __FILE__) {
        print "$file\n";
        replace($file);
    }
}

sub replace {

    my ($file) = @_;

    open(IN, "$DIR/$file") or die die "Couldn't open $file.";
    my @lines = <IN>;
    close(IN);
    open(OUT, ">$DIR/$file.tmp") or die die "Couldn't open $file.";
    foreach my $line (@lines) {

        &amp;jcode'h2z_sjis(\$line);

        while(my($before, $after) = each %WORDS) {

            if($line =~ /$before/) {
                $line =~ s/$before/$after/g;
            }
        }
        print OUT $line;
    }
    close(OUT);

    rename("$DIR/$file.tmp", "$DIR/$file");
    unlink("$DIR/$file.tmp");
}

1;
以下のように、replace.conf のハッシュ変数に置換前・置換後の文字列を書いておき、replace.pl を実行する。 replace.conf の内容
#/usr/bin/perl -w

use strict;

our $HOME_DIR = '/home/youraccount';
our $DIR = "$HOME_DIR/targetdirectory";
our %WORDS = (

                'ブラウザー'   # 置換前の文字列
            =>   'ブラウザ',   # 置換後の文字列

                'FTP'       # 置換前の文字列
            =>     'FTP',    # 置換後の文字列

# ... 以下、

                '置換前の文字列1'
            =>  '置換後の文字列1',

                '置換前の文字列2'
            =>  '置換後の文字列2'

# ... という形で追加していけばよい。

        );
1;
replace.pl の内容
#!/usr/bin/perl

require './jcode.pl';
require './replace.conf';

opendir(DIR, $DIR) or die "Couldn't open $DIR directory.";
my @files = readdir(DIR);
closedir(DIR);
foreach my $file (@files) {
    if(!(-d $file) && $file ne '.' && $file ne '..' && $file ne __FILE__) {
        print "$file\n";
        replace($file);
    }
}

sub replace {

    my ($file) = @_;

    open(IN, "$DIR/$file") or die die "Couldn't open $file.";
    my @lines = <IN>;
    close(IN);
    open(OUT, ">$DIR/$file.tmp") or die die "Couldn't open $file.";
    foreach my $line (@lines) {

        &amp;jcode'h2z_sjis(\$line);

        while(my($before, $after) = each %WORDS) {

            if($line =~ /$before/) {
                $line =~ s/$before/$after/g;
            }
        }
        print OUT $line;
    }
    close(OUT);

    rename("$DIR/$file.tmp", "$DIR/$file");
    unlink("$DIR/$file.tmp");
}

1;