How to use dos2unix?

You should be able to get dos2unix from your package manager on Linux. If you are using a Debian-based distro, you should be able to do sudo apt-get install dos2unix. If you are using a RH-like distro, you should be able to do sudo yum install dos2unix. Once it is installed, you can just give the target file as … Read more

The correct way to read a data file into an array

Just reading the file into an array, one line per element, is trivial: Now the lines of the file are in the array @lines. If you want to make sure there is error handling for open and close, do something like this (in the snipped below, we open the file in UTF-8 mode, too):

How to print variables in Perl

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 would looks like part of the variable but isn’t, enclose the variable name in {}:

How can I call a shell command in my Perl script?

How to run a shell script from a Perl program 1. Using system system($command, @arguments); For example: System will execute the $command with @arguments and return to your script when finished. You may check $! for certain errors passed to the OS by the external application. Read the documentation for system for the nuances of how … Read more

What is the meaning of @_ in Perl?

perldoc perlvar is the first place to check for any special-named Perl variable info. Quoting: @_: Within a subroutine the array @_ contains the parameters passed to that subroutine. More details can be found in perldoc perlsub (Perl subroutines) linked from the perlvar: Any arguments passed in show up in the array @_ . Therefore, if you called a function with two … Read more

What is the meaning of @_ in Perl?

perldoc perlvar is the first place to check for any special-named Perl variable info. Quoting: @_: Within a subroutine the array @_ contains the parameters passed to that subroutine. More details can be found in perldoc perlsub (Perl subroutines) linked from the perlvar: Any arguments passed in show up in the array @_ . Therefore, if you called a function with two … Read more

How to replace a string in an existing file in Perl

Use a one-liner: Explanation -p processes, then prints <> line by line -i activates in-place editing. Files are backed up using the .bak extension The regex substitution acts on the implicit variable, which are the contents of the file, line-by-line

Find size of an array in Perl

The first and third ways are the same: they evaluate an array in scalar context. I would consider this to be the standard way to get an array’s size. The second way actually returns the last index of the array, which is not (usually) the same as the array size.