Skip to content

Commit db600ef

Browse files
Use Dotenv for environment variable management in Perl examples
- Replaced custom `.env` file parsing logic with `Dotenv` library across all Perl examples. - Updated `cpanfile` to include `Dotenv` as a dependency. - Adjusted related documentation in `AGENTS.md` to reflect the usage of `Dotenv`.
1 parent 79bc23f commit db600ef

7 files changed

Lines changed: 21 additions & 72 deletions

File tree

Perl/AGENTS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
- Base URL: `$ENV{PDFREST_URL} // 'https://api.pdfrest.com'`.
2626
- I/O: print API responses to STDOUT; send diagnostics to STDERR; exit non‑2xx with non‑zero status.
2727

28+
### Environment Loading
29+
- Use `Dotenv` to read `.env` into `%ENV` (do not hand‑roll parsing).
30+
- Add dependency in `cpanfile`: `requires 'Dotenv';`.
31+
- Load with a guarded call so missing files are fine:
32+
- JSON/Multipart examples: `my $env_path = "$Bin/../../.env"; -e $env_path and Dotenv->load($env_path);`
33+
- Complex Flow examples: `my $env_path = "$Bin/../.env"; -e $env_path and Dotenv->load($env_path);`
34+
- Do not override pre‑existing environment variables; rely on library defaults (no explicit override).
35+
2836
## Testing Guidelines
2937
- No formal test suite required for samples. Validate by running against small, known inputs.
3038
- Success: non‑zero exit on failures; JSON body printed to STDOUT on success.

Perl/Complex Flow Examples/merge-different-file-types.pl

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use HTTP::Request;
1010
use HTTP::Request::Common qw(POST);
1111
use URI::Escape qw(uri_escape);
12+
use Dotenv;
1213

1314
#!
1415
# What this sample does:
@@ -32,22 +33,9 @@
3233
binmode STDOUT, ':raw';
3334
binmode STDERR, ':encoding(UTF-8)';
3435

35-
# Load .env from the Perl folder root
36-
my $env_path = "$Bin/../.env"; # one level up from Complex Flow Examples
37-
if (-f $env_path) {
38-
open my $env_fh, '<:encoding(UTF-8)', $env_path or die "Cannot open $env_path: $!\n";
39-
while (my $line = <$env_fh>) {
40-
chomp $line;
41-
next if $line =~ /^\s*#/;
42-
next if $line !~ /\S/;
43-
if ($line =~ /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$/) {
44-
my ($k, $v) = ($1, $2);
45-
$v =~ s/^['"]|['"]$//g;
46-
$ENV{$k} = $v if !exists $ENV{$k};
47-
}
48-
}
49-
close $env_fh;
50-
}
36+
# Load .env from the Perl folder root (one level up from this script)
37+
my $env_path = "$Bin/../.env";
38+
-e $env_path and Dotenv->load($env_path);
5139

5240
my $api_key = $ENV{PDFREST_API_KEY} // '';
5341
if (!$api_key || $api_key =~ /^\s*$/) {

Perl/Endpoint Examples/JSON Payload/markdown.pl

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use HTTP::Request;
1010
use HTTP::Request::Common qw(POST);
1111
use Encode qw(encode);
12+
use Dotenv;
1213

1314
#!
1415
# What this sample does:
@@ -34,20 +35,7 @@
3435

3536
# Load .env from the Perl folder root (two levels up from this script)
3637
my $env_path = "$Bin/../../.env";
37-
if (-f $env_path) {
38-
open my $env_fh, '<:encoding(UTF-8)', $env_path or die "Cannot open $env_path: $!\n";
39-
while (my $line = <$env_fh>) {
40-
chomp $line;
41-
next if $line =~ /^\s*#/;
42-
next if $line !~ /\S/;
43-
if ($line =~ /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$/) {
44-
my ($k, $v) = ($1, $2);
45-
$v =~ s/^['"]|['"]$//g; # strip surrounding quotes
46-
$ENV{$k} = $v if !exists $ENV{$k};
47-
}
48-
}
49-
close $env_fh;
50-
}
38+
-e $env_path and Dotenv->load($env_path);
5139

5240
my $api_key = $ENV{PDFREST_API_KEY} // '';
5341
if (!$api_key || $api_key =~ /^\s*$/) {

Perl/Endpoint Examples/JSON Payload/rasterized-pdf.pl

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use JSON::PP qw(encode_json decode_json);
88
use LWP::UserAgent;
99
use HTTP::Request;
10+
use Dotenv;
1011

1112
#!
1213
# What this sample does:
@@ -32,20 +33,7 @@
3233

3334
# Load .env from the Perl folder root
3435
my $env_path = "$Bin/../../.env";
35-
if (-f $env_path) {
36-
open my $env_fh, '<:encoding(UTF-8)', $env_path or die "Cannot open $env_path: $!\n";
37-
while (my $line = <$env_fh>) {
38-
chomp $line;
39-
next if $line =~ /^\s*#/;
40-
next if $line !~ /\S/;
41-
if ($line =~ /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$/) {
42-
my ($k, $v) = ($1, $2);
43-
$v =~ s/^['"]|['"]$//g;
44-
$ENV{$k} = $v if !exists $ENV{$k};
45-
}
46-
}
47-
close $env_fh;
48-
}
36+
-e $env_path and Dotenv->load($env_path);
4937

5038
my $api_key = $ENV{PDFREST_API_KEY} // '';
5139
if (!$api_key || $api_key =~ /^\s*$/) {

Perl/Endpoint Examples/Multipart Payload/markdown.pl

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use File::Basename qw(basename);
77
use LWP::UserAgent;
88
use HTTP::Request::Common qw(POST);
9+
use Dotenv;
910

1011
#!
1112
# What this sample does:
@@ -31,20 +32,7 @@
3132

3233
# Load .env from the Perl folder root
3334
my $env_path = "$Bin/../../.env";
34-
if (-f $env_path) {
35-
open my $env_fh, '<:encoding(UTF-8)', $env_path or die "Cannot open $env_path: $!\n";
36-
while (my $line = <$env_fh>) {
37-
chomp $line;
38-
next if $line =~ /^\s*#/;
39-
next if $line !~ /\S/;
40-
if ($line =~ /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$/) {
41-
my ($k, $v) = ($1, $2);
42-
$v =~ s/^['"]|['"]$//g;
43-
$ENV{$k} = $v if !exists $ENV{$k};
44-
}
45-
}
46-
close $env_fh;
47-
}
35+
-e $env_path and Dotenv->load($env_path);
4836

4937
my $api_key = $ENV{PDFREST_API_KEY} // '';
5038
if (!$api_key || $api_key =~ /^\s*$/) {

Perl/Endpoint Examples/Multipart Payload/rasterized-pdf.pl

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use File::Basename qw(basename);
77
use LWP::UserAgent;
88
use HTTP::Request::Common qw(POST);
9+
use Dotenv;
910

1011
#!
1112
# What this sample does:
@@ -31,20 +32,7 @@
3132

3233
# Load .env from the Perl folder root
3334
my $env_path = "$Bin/../../.env";
34-
if (-f $env_path) {
35-
open my $env_fh, '<:encoding(UTF-8)', $env_path or die "Cannot open $env_path: $!\n";
36-
while (my $line = <$env_fh>) {
37-
chomp $line;
38-
next if $line =~ /^\s*#/;
39-
next if $line !~ /\S/;
40-
if ($line =~ /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$/) {
41-
my ($k, $v) = ($1, $2);
42-
$v =~ s/^['"]|['"]$//g;
43-
$ENV{$k} = $v if !exists $ENV{$k};
44-
}
45-
}
46-
close $env_fh;
47-
}
35+
-e $env_path and Dotenv->load($env_path);
4836

4937
my $api_key = $ENV{PDFREST_API_KEY} // '';
5038
if (!$api_key || $api_key =~ /^\s*$/) {

Perl/cpanfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ requires 'Mozilla::CA';
88
requires 'IO::Socket::SSL';
99
requires 'Net::SSLeay';
1010
requires 'URI';
11+
requires 'Dotenv';

0 commit comments

Comments
 (0)