File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 99use HTTP::Request;
1010use HTTP::Request::Common qw( POST) ;
1111use URI::Escape qw( uri_escape) ;
12+ use Dotenv;
1213
1314# !
1415# What this sample does:
3233binmode STDOUT , ' :raw' ;
3334binmode 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
5240my $api_key = $ENV {PDFREST_API_KEY } // ' ' ;
5341if (!$api_key || $api_key =~ / ^\s *$ / ) {
Original file line number Diff line number Diff line change 99use HTTP::Request;
1010use HTTP::Request::Common qw( POST) ;
1111use Encode qw( encode) ;
12+ use Dotenv;
1213
1314# !
1415# What this sample does:
3435
3536# Load .env from the Perl folder root (two levels up from this script)
3637my $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
5240my $api_key = $ENV {PDFREST_API_KEY } // ' ' ;
5341if (!$api_key || $api_key =~ / ^\s *$ / ) {
Original file line number Diff line number Diff line change 77use JSON::PP qw( encode_json decode_json) ;
88use LWP::UserAgent;
99use HTTP::Request;
10+ use Dotenv;
1011
1112# !
1213# What this sample does:
3233
3334# Load .env from the Perl folder root
3435my $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
5038my $api_key = $ENV {PDFREST_API_KEY } // ' ' ;
5139if (!$api_key || $api_key =~ / ^\s *$ / ) {
Original file line number Diff line number Diff line change 66use File::Basename qw( basename) ;
77use LWP::UserAgent;
88use HTTP::Request::Common qw( POST) ;
9+ use Dotenv;
910
1011# !
1112# What this sample does:
3132
3233# Load .env from the Perl folder root
3334my $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
4937my $api_key = $ENV {PDFREST_API_KEY } // ' ' ;
5038if (!$api_key || $api_key =~ / ^\s *$ / ) {
Original file line number Diff line number Diff line change 66use File::Basename qw( basename) ;
77use LWP::UserAgent;
88use HTTP::Request::Common qw( POST) ;
9+ use Dotenv;
910
1011# !
1112# What this sample does:
3132
3233# Load .env from the Perl folder root
3334my $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
4937my $api_key = $ENV {PDFREST_API_KEY } // ' ' ;
5038if (!$api_key || $api_key =~ / ^\s *$ / ) {
Original file line number Diff line number Diff line change @@ -8,3 +8,4 @@ requires 'Mozilla::CA';
88requires ' IO::Socket::SSL' ;
99requires ' Net::SSLeay' ;
1010requires ' URI' ;
11+ requires ' Dotenv' ;
You can’t perform that action at this time.
0 commit comments