In another article Perl Shareable Memory, I explained how perl uses shareable memory in detail, in this article, I'll show some examples:

Include shareable module

use IPC::Shareable ;

Define options for the shareable variables

You could have more options defined for different shareable memory.

my %option =(
 create    => 1,
 exclusive => 0,
 destroy   => 1,
 mode      => 0600,
 size      => IPC::Shareable::SHM_BUFSIZ(),
);

Tie the variable to a shared memory segment

my $dri_handle= tie our %s_dri, 'IPC::Shareable', 'drive', \%option ;

Use it in rest of the script, share the variable between processes.

Just use the varialbe %s_drives like regular variable, in case you want to update the variable amoung processes, lock it first.

$dri_handle->shlock();  # lock the shareable memory segment
$s_dri{'name'}="/dev/sda";
$dri_handle->shunlock(); # unlock the shareable memory segment

That's it.

Remove the shareable memory when exiting

$dri_handle->remove;

or to remove all shareable memory created by this process

IPC::Shareable->clean_up;

To remove all shareable segments, even not created by the process

IPC::Shareable->clean_up_all;

If you see permission related issue, see How to fix perl shareable memory permission issue
For shareable memory removing issue, see How to create check and delete ipc share memory semaphare and message queue on linux

 

For detail of each options, GLUE meaning, see Perl Shareable Memory