Use variable as RegEx pattern
As documentation says: $re = qr/$pattern/; $string =~ /foo${re}bar/; # can be interpolated in other patterns $string =~ $re; # or used standalone $string =~ /$re/; # or this way So, use the qr quote-like operator.
As documentation says: $re = qr/$pattern/; $string =~ /foo${re}bar/; # can be interpolated in other patterns $string =~ $re; # or used standalone $string =~ /$re/; # or this way So, use the qr quote-like operator.
Perl facilitates the use of lists/hashes to implement named parameters, which I consider very elegant and a tremendous aid to self-documenting code. my $result = $obj->method( flux_capacitance => 23, general_state => ‘confusion’, attitude_flags => ATTITUDE_PLEASANT | ATTITUDE_HELPFUL, );
We can’t really answer that without knowing a lot more. Just because you’re not currently dependent on any other projects, are you likely to interact with them at some point in the future? If so, what technologies do they prefer? At the BBC, we’ve had some projects “JSON-only”, only to find out that Java developers … Read more
Because Perl modules are required to return a value to signal if the require directive must succeed (true value returned) or fail (false value returned; this can make sense if the module failed to initialize for some reason). If you don’t return anything, the interpreter cannot know if the require must succeed or fail; at … Read more
The Perl pack function will return “binary” data according to a template. open(my $out, ‘>:raw’, ‘sample.bin’) or die “Unable to open: $!”; print $out pack(‘s<‘, 255); close($out); In the above example, the ‘s’ tells it to output a short (16 bits), and the ‘<‘ forces it to little-endian mode. In addition, ‘:raw’ in the call … Read more
Perl doesn’t offer a way to check whether or not a variable has been initialized. However, scalar variables that haven’t been explicitly initialized with some value happen to have the value of undef by default. You are right about defined being the right way to check whether or not a variable has a value of … Read more
While BEGIN and END blocks can be used as you describe, the typical usage is to make changes that affect the subsequent compilation. For example, the use Module qw/a b c/; statement actually means: BEGIN { require Module; Module->import(qw/a b c/); } similarly, the subroutine declaration sub name {…} is actually: BEGIN { *name = … Read more
use List::Util qw(first); $idx = first { $array[$_] eq ‘whatever’ } 0..$#array; (List::Util is core) or use List::MoreUtils qw(firstidx); $idx = firstidx { $_ eq ‘whatever’ } @array; (List::MoreUtils is on CPAN)
Add \n to your string: die “My error message\n” This is documented in die: If the last element of LIST does not end in a newline, the current script line number and input line number (if any) are also printed, and a newline is supplied.