Assigning using ternary operator?
I’d usually write this as: $review = ( defined($model->test) ? 1 : ” ); where the parentheses are for clarity for other people reading the code.
I’d usually write this as: $review = ( defined($model->test) ? 1 : ” ); where the parentheses are for clarity for other people reading the code.
It’s for backwards compatibility. Perl 4 didn’t have strict at all, and there are most likely still scripts out there originally written for Perl 4 that still work fine with Perl 5. Making strict automatic would break those scripts. The situation is even worse for one-liners, many of which don’t bother to declare variables. Making … Read more
From Perlfaq8: You’re confusing the purpose of system() and backticks (“). system() runs a command and returns exit status information (as a 16 bit value: the low 7 bits are the signal the process died from, if any, and the high 8 bits are the actual exit value). Backticks (“) run a command and return … Read more
You’re confused because you think = is a single operator while it can result in two different operators: a list assignment operator or a scalar assignment operator. Mini-Tutorial: Scalar vs List Assignment Operator explains the differences. my $b = (33,22,11); —————— Scalar assign in void context. ———- List literal in scalar context. Returns last. my … Read more
Because in Haskell, we have laziness & guarded recursion and tail call optimization and Perl has neither. This essentially means that every function call allocates a set amount of memory called “the stack” until the function returns. When you write recursive code, you build up a huge amount of memory because of these nested function … Read more
Let’s say I have a package MyPackage that uses @EXPORT. #this is MyPackage.pm package MyPackage; @EXPORT = qw(do_awesome_thing); sub do_awesome_thing { … } sub be_awesome { … } Now, when I use MyPackage in my code, #this is myscript.pl use MyPackage; do_awesome_thing(); #works be_awesome(); #doesn’t work MyPackage::be_awesome(); #works do_awesome_thing gets automatically exported to my code … Read more
Here’s a complete, working script that demonstrates what you’re asking. sub a { print “Hello World!\n”; } sub b { my $func = $_[0]; $func->(); } b(\&a); Here’s an explanation: you take a reference to function a by saying \&a. At that point you have a function reference; while normally a function would be called … Read more
Nginx doesn’t have native CGI support (it supports fastCGI instead). The typical solution for this is to run your Perl script as a fastCGI process and edit the nginx config file to re-direct requests to the fastCGI process. This is quite a complex solution if all you want to do is run a CGI script. … Read more
I’d use LWP::Simple for this. #!/usr/bin/perl use strict; use warnings; use LWP::Simple; my $url=”http://marinetraffic2.aegean.gr/ais/getkml.aspx”; my $file=”data.kml”; getstore($url, $file);
perldoc -f map is one way: use warnings; use strict; use Data::Dumper; my @ns = map { 5 * $_ } 1 .. 4; print Dumper(\@ns); __END__ $VAR1 = [ 5, 10, 15, 20 ]; See also: perldoc perlop