The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Yandex::Tools - useful functions for Yandex daemons and programs.

LOGGING

use Yandex::Tools qw(/^/);

# logging
set_log_filename("/var/log/mylog");
if (!can_log()) {
  die ("can't log!\n");
}

print "will log to: " . get_log_filename() . "\n";

set_log_options({
  'rotate_size' => 1024*1024, # 1MB
  'rotate_keep_copies' => 2,
});

do_log("yoyo");

FILESYSTEM

use Yandex::Tools qw(/^/);

# test if you can write file (by creating it)
if (!can_write("/root/test123")) {
  die "can't write to /root\n!";
}

# read text file into array of lines, removing \r\n
my $f = read_file_array('/etc/passwd');
foreach my $line (@{$f}) {
  print $line . "\n";
}

# read file removing \r\n; returns undef if file doesn't exist
my $hostname = read_file_option('/etc/hostname');

# read file as is; returns undef if file doesn't exist
my $etc_passwd = read_file_scalar('/etc/passwd');

# read file as is; returns True on success, False otherwise
if (write_file_scalar('/etc/passwd', $etc_passwd)) {
  print "written successfully!\n";
}

# simply write some text to file
# (which would be read with read_file_option)
if (!write_file_option('/tmp/hostname', 'test')) {
  die "unable to write /tmp/hostname!";
}

# get filesystem object struct (fileinfo struct)
#
# $VAR1 = {
#   'uid' => 0,
#   'short_name' => 'passwd',
#   'mtime' => 1273586736,
#   'mode' => 420,
#   'size' => 1081,
#   'absolute_name' => '/etc/passwd',
#   'relative_name' => 'etc/passwd',
#   'type' => 'file',
#   'gid' => 0
#   };
#
my $fs_obj = fileinfo_struct({'absolute_name' => '/etc/passwd'});

# read directory entries into array (by default)
# of fileinfo structs (see above), does not recurse
# into directories, allows optional filtering
# of only-files or only-directories
#
my $mixed_array = read_dir('/etc', {'output_type' => 'arrayref', 'only-directories' => 1});

# read directory into hash (recursive),
# with relative entry name as a key
# and fileinfo struct as value
#
my $dir_struct = read_dir('/etc', {'output_type' => 'hashref'});

STRINGS

use Yandex::Tools qw(/^/);

# remove duplicate entries from array
#
my @arr = qw/1 2 3 4 5 1 2 3 4 5/;
my @uniq = array_clear_dupes(@arr);
print join(",", @uniq) . "\n";

# prints:
# 1,2,3,4,5

# replace all kinds of delimiters like
# multiple spaces, tabs, commas, ampersands, semicolons
# with just one space
my $str = canonize_delimiters($str);

# self explained:
if (!is_ascii(chr(1)) || !is_digital(chr(1))) {
  die "oops";
}

# match any of regexp
if (matches_with_one_of_regexps("something", ["one", "two", "some"])) {
  print "match!\n";
}

# allows to print anything without warnings
# even with `use warnings`. replaces undefined values
# with empty string
#
print safe_string(undef);

OTHER

# print callstack (for debugging)
#
print get_callstack();

# release candidate version of run_forked,
# cool external programs execution routine
# allowing time limiting and some other
# types of control.
#
# distributed widely in L<IPC::Cmd>,
# read documentation there.
#
my $r = run_forked("uptime");
print $r->{'stdout'};

# send mail (different mailers are tried)
#
send_mail({
  'to' => 'root',
  'subject' => 'test',
  'body' => 'test',
  });

AUTHORS

Petya Kohts <petya@kohts.ru>

COPYRIGHT

Copyright 2007 - 2011 Petya Kohts.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.