How can I make a JSON POST request with LWP?

You’ll need to construct the HTTP request manually and pass that to LWP. Something like the following should do it: my $uri = ‘https://orbit.theplanet.com/Login.aspx?url=/Default.aspx’; my $json = ‘{“username”:”foo”,”password”:”bar”}’; my $req = HTTP::Request->new( ‘POST’, $uri ); $req->header( ‘Content-Type’ => ‘application/json’ ); $req->content( $json ); Then you can execute the request with LWP: my $lwp = LWP::UserAgent->new; … Read more

Plack::App::CGIBin via Apache and mod_fastcgi – CGI script not found

I’d guess you need to change builder { mount “/plack” => $app; }; to builder { mount “https://stackoverflow.com/” => $app; }; because your alias is removing /plack/ or just change # URL to be handled by FastCGI Alias /plack/ /tmp/placktest.fcgi/ to # URL to be handled by FastCGI Alias /plack/ /tmp/placktest.fcgi/plack/ After all it does … Read more

How can I unit test Perl functions that print to the screen?

UPDATE: IMHO, the correct answer to this question ought to be to use Test::Output: #!/usr/bin/perl use strict; use warnings; use Test::More tests => 1; use Test::Output; sub myfunc { print “This is a test\n” } stdout_is(\&myfunc, “This is a test\n”, ‘myfunc() returns test output’); Output: C:\Temp> tm 1..1 ok 1 – myfunc() returns test output … Read more

How can I list all variables that are in a given scope?

You can access the symbol table, check out p. 293 of “Programming Perl” Also look at “Mastering Perl: http://www252.pair.com/comdog/mastering_perl/ Specifically: http://www252.pair.com/comdog/mastering_perl/Chapters/08.symbol_tables.html Those variables you are looking for will be under the main namespace A quick Google search gave me: { no strict ‘refs’; foreach my $entry ( keys %main:: ) { print “$entry\n”; } } … Read more

binary sed replacement

bbe is a “sed for binary files”, and should work more efficiently for large binary files than hexdumping/reconstructing. An example of its use: $ bbe -e ‘s/original/replaced/’ infile > outfile Further information on the man page.

How to print variables in Perl

print “Number of lines: $nids\n”; print “Content: $ids\n”; How did Perl complain? print $ids should work, though you probably want a newline at the end, either explicitly with print as above or implicitly by using say or -l/$\. If you want to interpolate a variable in a string and have something immediately after it that … Read more

What is the difference between Perl’s ( or, and ) and ( ||, && ) short-circuit operators?

Due to the low precedence of the ‘or’ operator, or3 parses as follows: sub or3 { my ($a,$b) = @_; (return $a) or $b; } The usual advice is to only use the ‘or’ operator for control flow: @info = stat($file) or die; For more discussion, see the perl manual: http://perldoc.perl.org/perlop.html#Logical-or-and-Exclusive-Or

How can I assign two arrays to a hash in Perl?

You can do it in a single assignment: my %hash; @hash{@array1} = @array2; It’s a common idiom. From perldoc perldata on slices: If you’re confused about why you use an ‘@’ there on a hash slice instead of a ‘%’, think of it like this. The type of bracket (square or curly) governs whether it’s … Read more