How can I hook into Perl’s print?

There are a number of built-ins that you can override (see perlsub). However, print is one of the built-ins that doesn’t work this way. The difficulties of overriding print are detailed at this perlmonk’s thread. However, you can Create a package Tie a handle Select this handle. Now, a couple of people have given the … Read more

What pseudo-operators exist in Perl 5?

Nice project, here are a few: scalar x!! $value # conditional scalar include operator (list) x!! $value # conditional list include operator ‘string’ x/pattern/ # conditional include if pattern “@{[ list ]}” # interpolate list expression operator “${\scalar}” # interpolate scalar expression operator !! $scalar # scalar -> boolean operator +0 # cast to numeric … Read more

Perl memory usage profiling and leak detection?

You could have a circular reference in one of your objects. When the garbage collector comes along to deallocate this object, the circular reference means that everything referred to by that reference will never get freed. You can check for circular references with Devel::Cycle and Test::Memory::Cycle. One thing to try (although it might get expensive … Read more

What’s the best way to open and read a file in Perl?

There are no universal standards, but there are reasons to prefer one or another. My preferred form is this: open( my $input_fh, “<“, $input_file ) || die “Can’t open $input_file: $!”; The reasons are: You report errors immediately. (Replace “die” with “warn” if that’s what you want.) Your filehandle is now reference-counted, so once you’re … Read more

Perl flags -pe, -pi, -p, -w, -d, -i, -t?

Yes, Google is notoriously difficult for looking up punctuation and, unfortunately, Perl does seem to be mostly made up of punctuation 🙂 The command line switches are all detailed in perlrun (available from the command line by calling perldoc perlrun). Going into the options briefly, one-by-one: -p: Places a printing loop around your command so … Read more

Using perl’s `system`

Best practices: avoid the shell, use automatic error handling – IPC::System::Simple. require IPC::System::Simple; use autodie qw(:all); system qw(command –arg1=arg1 –arg2=arg2 -arg3 -arg4); use IPC::System::Simple qw(runx); runx [0], qw(command –arg1=arg1 –arg2=arg2 -arg3 -arg4); # ↑ list of allowed EXIT_VALs, see documentation Edit: a rant follows. eugene y’s answer includes a link to the documentation to system. … Read more