You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use Date::Calc qw(Today Add_Delta_Days);
my ($year, $month, $day) = Today();
my ($new_year, $new_month, $new_day) = Add_Delta_Days($year, $month, $day, 7);
Executing external commands
my$output = `ls -l`;
Random Number Generation
my$random_number = int(rand(100));
Error Handling
eval {
# Code that might throw an exception
};
if ($@) {
# Handle the exception
}
Using modules
use Module::Name;
Sending email (with NET::SMTP)
use Net::SMTP;
my$smtp = Net::SMTP->new('smtp.example.com');
$smtp->mail('sender@example.com');
$smtp->to('recipient@example.com');
$smtp->data();
$smtp->datasend("Subject: Test Email\n");
$smtp->datasend("Hello, this is a test email.\n");
$smtp->dataend();
$smtp->quit;
Working with JSON (JSON module)
use JSON;
my$json_text = '{"key":"value"}';
my$data = decode_json($json_text);
Multiline Strings
my$multiline = <<"END_TEXT";
This is amultiline string.END_TEXT
Fetching URL Content (with LWP::Simple)
use LWP::Simple;
my$content = get('https://example.com');
URL Encoding/Dencoding
use URI::Escape;
my$encoded = uri_escape($string);
my$decoded = uri_unescape($encoded);
Reading and writing CSV files (with Text::CSV)
use Text::CSV;
my$csv = Text::CSV->new({ binary=> 1 });
openmy$file, '<', 'data.csv'ordie"Couldn't open file: $!";
while (my$row = $csv->getline($file)) {
# Process each row
}
close$file;
HTTP Server (with HTTP::Server::Simple)
use HTTP::Server::Simple;
use base qw(HTTP::Server::Simple::CGI);
subhandle_request {
# Handle HTTP requests
}
my$server = MyWebServer->new;
$server->run();
Calculating Hash digesets (with Digest::MD5)
use Digest::MD5;
my$digest = Digest::MD5->new;
$digest->add('data_to_hash');
my$hash = $digest->hexdigest;
XML Parsing (with XML::LibXML)
use XML::LibXML;
my$parser = XML::LibXML->new;
my$doc = $parser->parse_file('data.xml');
Database Interaction (with DBI)
use DBI;
my$dbh = DBI->connect('dbi:SQLite:dbname=database.db', '', '', { RaiseError=> 1 });
my$sth = $dbh->prepare('SELECT * FROM table');
$sth->execute();
while (my$row = $sth->fetchrow_hashref()) {
# Process database rows
}
$sth->finish();
$dbh->disconnect();
use Mojo::IOLoop;
Mojo::IOLoop->delay(
sub {
my$delay = shift;
# Asynchronous code here$delay->pass('Result');
},
sub {
my ($delay, $result) = @_;
# Handle the result
},
)->wait;
Handling Binary Data (with Encode)
use Encode;
my$encoded_data = encode_base64($binary_data);
my$decoded_data = decode_base64($encoded_data);
Creating custom exception classes
packageMyException;
use base 'Exception::Class';
packagemain;
use Try::Tiny;
try {
MyException->throw("Custom exception message");
}
catch {
if (MyException->caught($_)) {
warn"Caught custom exception: $_\n";
}
};
Database Transactions (with DBIx::Class)
my$schema = DBIx::Class::Schema->connect('dbi:SQLite:dbname=database.db');
my$txn = $schema->txn_scope;
# Perform database operations within the transaction$txn->commit; # or $txn->rollback;
Using Moose for Object Oriented Programming
packageMyClass;
use Moose;
has 'data'=> (is=>'ro', isa=>'Str');
subprint_data {
my ($self) = @_;
print$self->data;
}
my$obj = MyClass->new(data=>'Some data');
$obj->print_data();
Creating custom modules
packageMyModule;
subnew {
my ($class) = @_;
my$self = {};
bless$self, $class;
return$self;
}
subdo_something {
my ($self) = @_;
# Code here
}
1; # Required for modules# In another scriptuse MyModule;
my$module = MyModule->new();
$module->do_something();
Parsing JSON with Error Handling (with JSON::MaybeXS)