Perl: print the name of the input file

The special variable $ARGV could be used to get the name of the input file.
It returns the name of the file or ‘-‘ if the standard input was used.

Here are some examples:

perl -nle 'END { print $ARGV }' /etc/passwd

echo test | perl -nle 'print $ARGV'

$ (echo test; echo test2) | perl -nle 'print $ARGV'

perl -nle 'print $ARGV if !$seen{$ARGV}++' /etc/passwd /etc/shells

Perl: how to get the position of the match

Let’s search for the word “brave” in the string “Hello, new brave world!”.
There are several methods to get the position of the match in Perl.

#1 – use POS function
Function pos returns the position, where search was finished.

$txt = "Hello, brave new world!" ;
$txt =~ m/(brave)/g;
my $pos = pos($txt) - length $1;

#2 – use $PREMATCH variable
$PREMATCH varialbe (short name $`) contains the string, preceeding the match.
This method is easy, however it could significantly affect performance of other regular expressions (see RegExp book for the detailed explanation). Therefor, it’s not the best solution.

use English ;
$txt = "Hello, brave new world!" ;
$txt =~ m/(brave)/g;

###my $pos = length $`
my $pos = length $PREMATCH;

#3 – use @LAST_MATCH_START variable
This method was introduced in Perl 5.6.0 and it does not make any bad impact on performance – so it’s recommended solution.

use English qw( -no_match_vars );
$txt = "Hello, brave new world!" ;
$txt =~ m/(brave)/g;

### $pos = $-[0];
$pos = $LAST_MATCH_START[0];

VBA: how to read text file

This is just example, how to read text file in Visual Basic for Applications (for example, in Excel macro).

It’s just a stub: there is no error processing here, the source file is really text file, no problems with permissions etc.

Sub read_from_text_file()

txtFile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If txtFile = False Then
MsgBox "No file to process"
Exit Sub
End If

Open txtFile For Input As #1
Input #1, myString
MsgBox myString
Close #1

End Sub

Perl: SOAP problem with dateTime

The following error message was received by Perl application:
Unrecognized type ‘{}dateTime

The problem is caused by “dateTime” type: it was not supported in the 1999 XML Specification.
The solution is to switch to 2001 schema:

[cc lang=”Perl”]
my $soap = SOAP::Lite->uri( $NAMESPACE )->proxy( $PROXY_URL )->xmlschema (‘2001’);

Perl: Soap deserialization error

Sometimes the following error is displayed, if application uses the old version of CPAN SOAP module:
“Client Application failed during request deserialization: no element found at line 1, column 0, byte -1 at XML/”

The possilbe solution is to change SOAP::Transport::HTTP module (SOAP/Transport/, function “handle”:

[cc lang=”perl”]
sysread( STDIN, $buffer, $length )

should be replaced by

[cc lang=”perl”]
read( STDIN, $buffer, $length )

AutoIt: script for hotkeys usage

This script allows You to define the global Windows hotkeys via the configuration file.

The syntax of the config file is simple:


Here “Hotkey” is the combination of the keyboard key with one of the identifiers:

^ Ctrl
! Alt
+ Shift
# Windows-Key


;Win+C – start calculator

;Win+Shift+N – start notepad

;Win+N – start notepad++ together with apploc.exe
#{n},%windir%\AppPatch\AppLoc.exe,”%ProgramFiles%\Notepad++\notepad++.exe” -nosession /L0419

Continue reading

Unix: print in color

This simple procedure takes 3 arguments as the parameters:

  • Text
  • Foreground color
  • Background color

It prints the text to the terminal in selected colors.

[cc lang=”bash”]
function print_color {
local text=$1
local fg=$2
local bg=$3
case “$fg” in
red) fg=”31m” ;;
green) fg=”32m” ;;
yellow) fg=”33m” ;;
blue) fg=”34m” ;;
white) fg=”37m” ;;
black) fg=”30m” ;;
*) fg=”37m” ;;
case “$bg” in
red) bg=”41m” ;;
green) bg=”42m” ;;
yellow) bg=”43m” ;;
blue) bg=”44m” ;;
white) bg=”47m” ;;
black) bg=”40m” ;;
*) bg=”40m” ;;
echo -en “\033[${fg}\033[${bg}${text}\033[0m”

Unix: rename files to lowercase

It’s very simple to rename the files to lowercase/uppercase using awk:

[cc lang=”bash”]
ls -1rt | awk ‘{ printf(“mv %s %s\n”, $0, tolower($0)) | “sh” } END { close(“sh”) }’

[cc lang=”bash”]
ls -1rt | awk ‘{ printf(“mv %s %s\n”, $0, toupper($0)) | “sh” } END { close(“sh”) }’