Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#!perl
use strict;
open(ENVTMP, ">envtmp.txt") || die qq(Can't open "envtmp.txt" for output\n);
my $delim = "="; #use your favorite delimiter
my ($k, $v);
#Write key/value pairs from %ENV to file, joined by $delim
while (($k, $v) = each %ENV) {
print ENVTMP join($delim, ($k, $v)), "\n";
}
close(ENVTMP) || die qq(Can't close output file "envtmp.txt"\n);
open(ENVTMP, "<envtmp.txt") || die qq(Can't open "envtmp.txt" for input\n);
my %h;
#Read key/value pairs from file into hash.
while (<ENVTMP>) {
chomp;
($k, $v) = split(/$delim/);
$h{$k} = $v;
}
close(ENVTMP) || die qq(Can't close input file "envtmp.txt"\n);
#Print the hash.
for (sort keys %h) {
print qq($_ => $h{$_}\n);
}
#!perl
# env2hash, more functional (and slightly OO) approach
use strict;
use warnings;
use Carp;
use FileHandle;
my $fh = FileHandle->new();
main: {
my $delim = "="; #use your favorite delimiter
my $filename = "envtemp.txt";
hash2file($filename, $delim, \%ENV);
my $h = file2hash($filename, $delim);
printhash($h);
exit(0);
}
sub hash2file {
# Write key/value pairs from hash to file, joined by $delim
my ($filename, $delim, $environ) = @_;
$fh->open($filename, "w") or
croak qq(Can't open "$filename" for output.\n);
my ($k, $v);
while (($k, $v) = each %$environ) {
$fh->print(join($delim, ($k, $v)), "\n");
}
$fh->close() or
croak qq(Can't close output file "$filename".\n);
}
sub file2hash {
# Read key/value pairs from file into hash.
my ($filename, $delim) = @_;
$fh->open($filename, "r") or
croak qq(Can't open "$filename" for input\n);
my %h;
my ($k, $v);
local $_;
while ($_ = $fh->getline()) {
chomp;
($k, $v) = split(/$delim/);
$h{$k} = $v;
}
$fh->close() or
croak qq(Can't close input file "$filename"\n);
\%h;
}
sub printhash {
# Print the hash.
my $h = shift;
for (sort keys %$h) {
print qq($_ => $h->{$_}\n);
}
}
print FILE join("\n", %ENV), "\n";