Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Learning Perl - Modules

A module in Perl is a reusable piece of code that can be included in your scripts to provide additional functionality. Modules are typically used to encapsulate related subroutines, variables, and other code that can be shared across…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

A module in Perl is a reusable piece of code that can be included in your scripts to provide additional functionality. Modules are typically used to encapsulate related subroutines, variables, and other code that can be shared across multiple scripts or applications.



Modules help in organising code, promoting code reuse, and maintaining a clean separation of concerns. They can be used to implement libraries, frameworks, or any reusable components that you might need in your Perl programs.



As your Perl programs grow in complexity, you will find that organising your code into modules can help keep things manageable. Modules allow you to encapsulate functionality and reuse code across different scripts.



A module in Perl is simply a package that contains subroutines and variables.



A package is defined using the package keyword, and it typically corresponds to a file with the same name as the package, ending in .pm. For example, a package named MyModule would be in a file called lib/MyModule.pm. It is good practice in perl to also name your modules with :: instead of camel casing and then use sub directories to organize your code. For example, a module named My::Module would be in a file called lib/My/Module.pm. however this is not a strict requirement and you can use any file name as long as you specify the correct package name in your code. You will notice that the file path is relative to the lib directory, which is a common convention in Perl projects also. The lib directory is where you typically place your custom modules. To create a package, you would start your file with the following line:




package My::Module;
1;






This line defines a package named My::Module. The package name should match the directory structure where the file is located, so if your file is in lib/My/Module.pm, the package name should be My::Module. We also include a 1; at the end of the file, which is a common convention in Perl modules to indicate that the module has been loaded successfully. If the module does not return a true value, Perl will not load it.



We can 'use' a module in our Perl script by including it with the use statement, followed by the module name. For example:




use My::Module;






This tells Perl to load the My::Module package, which should be located in a file named lib/My/Module.pm.



When you use a module, Perl will look for it in the directories specified in the @INC array, which includes the current directory and any directories specified by the PERL5LIB environment variable. If you have installed your module in a standard location, Perl will find it automatically. If you have not installed your module yet or installed it in a non standard location, you can add the directory to the @INC array at runtime by using the use lib pragma:




use lib 'lib';






or by setting the PERL5LIB environment variable before running your script:




PERL5LIB=lib perl myscript.pl






Perl also has an extensive ecosystem of modules available on CPAN (Comprehensive Perl Archive Network), which is a repository of Perl modules that you can install and use in your projects. You can search for modules on CPAN and install them using tools like cpanm or cpan. If you have never visited metacpan then I would recommend you do https://metacpan.org/, it is a great resource for finding Perl modules and documentation. In the next post we will look at how to install a CPAN module as there is one non core dependancy which will be useful going forward.



In this post we will do all steps manually, we will create a simple Perl module and use it in a script. This will give you a basic understanding of how to create and use modules in Perl. So lets create your first module in Perl, first we need to create the directory structure for our module.




mkdir My-Module
cd My-Module
mkdir -p lib/My
touch lib/My/Module.pm
touch myscript.pl






Now, let's edit the lib/My/Module.pm file to define our module, we will do a simple 'Hello World' module that prints a message when called:




# lib/My/Module.pm
package My::Module;
use strict;
use warnings;
sub hello {
print "Hello from My::Module!\n";
}
1; # Return true value to indicate successful loading






Next, let's edit the myscript.pl file to use our module:




# myscript.pl
use strict;
use warnings;
use lib 'lib'; # Add the lib directory to @INC
use My::Module; # Use the My::Module package
My::Module::hello(); # Call the hello subroutine from My::Module






Now, you can run your script:




perl myscript.pl






You should see the output:




Hello from My::Module!






This demonstrates how to create a simple Perl module and use it in a script. You can expand your module by adding more subroutines, variables, and functionality as needed.



In future posts I will show you how to export functions from your module so that you can use them without the package name, I will also cover several approaches for object orientation and how to create more complex modules with additional features like overloading of operators. For now, this should give you a good starting point for creating and using modules in Perl.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Learning Perl - Modules

Thematisch verwandte Begriffe: Learning, Perl, Modules · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-79918 | MaxKB is an open-source AI assistant for enterprise. Prior to version 2.…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick