sprinter has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear Monks, I would like to ask a simple question to your unlimited Monk knowledge. In recent days I've been reading Shared Memory modules code but all of the sudden I encountered some 'unpack' syntax that was very unfamiliar to me:

package IPC::SharedMem; use IPC::SysV qw(IPC_STAT IPC_RMID shmat shmdt memread memwrite); use strict; use vars qw($VERSION); use Carp; $VERSION = do { my @r = '$Snapshot: /IPC-SysV/2.01 $' =~ /(\d+\.\d+(?: +_\d+)?)/; @r ? $r[0] : '9.99' }; $VERSION = eval $VERSION; # Figure out if we have support for native sized types my $N = do { my $foo = eval { pack "L!", 0 }; $@ ? '' : '!' }; { package IPC::SharedMem::stat; use Class::Struct qw(struct); struct 'IPC::SharedMem::stat' => [ uid => '$', gid => '$', cuid => '$', cgid => '$', mode => '$', segsz => '$', lpid => '$', cpid => '$', nattch => '$', atime => '$', dtime => '$', ctime => '$', ]; } sub new { @_ == 4 or croak 'IPC::SharedMem->new(KEY, SIZE, FLAGS)'; my($class, $key, $size, $flags) = @_; my $id = shmget $key, $size, $flags; return undef unless defined $id; bless { _id => $id, _addr => undef, _isrm => 0 }, $class } sub id { my $self = shift; $self->{_id}; } sub addr { my $self = shift; $self->{_addr}; } sub stat { my $self = shift; my $data = ''; shmctl $self->id, IPC_STAT, $data or return undef; IPC::SharedMem::stat->new->unpack($data); }

What I can not figure out is the line of code that has 'unpack' in it. If any Monk could share his/her wisdom to this apprentice would be great. My initial guess is that somehow 'unpack' is a method of the class IPC::SharedMem::stat but I may also think that it is actually the core unpack function that somehow is being used as a method

Replies are listed 'Best First'.
Re: IPC::SharedMem::stat using Class::Struct (UPDATED)
by thanos1983 (Parson) on Sep 27, 2017 at 20:15 UTC

    Hello sprinter,

    Welcome to the Monastery. I am not sure where you got the source code that you have posted because it is a bit different than from the CPAN IPC::SharedMem/source.

    To answer your question, no the module does not have a separate pack and unpack method. See source code on the link above or sample below:

    sub read { @_ == 3 or croak '$shm->read(POS, SIZE)'; my($self, $pos, $size) = @_; my $buf = ''; if (defined $self->addr) { memread($self->addr, $buf, $pos, $size) or return undef; } else { shmread($self->id, $buf, $pos, $size) or return undef; } $buf; } sub write { @_ == 4 or croak '$shm->write(STRING, POS, SIZE)'; my($self, $str, $pos, $size) = @_; if (defined $self->addr) { return memwrite($self->addr, $str, $pos, $size); } else { return shmwrite($self->id, $str, $pos, $size); } }

    From the documentation IPC::SharedMem/methods, it also mentioned:

    read ( POS, SIZE ) Read SIZE bytes from the shared memory segment at POS . Returns the st +ring read, or undef if there was an error. The return value becomes t +ainted. See shmread. write ( STRING, POS, SIZE ) Write SIZE bytes to the shared memory segment at POS . Returns true if + successful, or false if there is an error. See shmwrite.

    Update: Forgot to add from the Synopsis of the module:

    $shm->write(pack("S", 4711), 2, 2);

    If you see pack documentation for S:

    S An unsigned short value.

    So in conclusion it is just the memory position that you read and write. I assume that the copy of the code that you post was from an earlier version but on latest version is clear that the module is using the core pack and unpack of Perl

    Hope this helps, unless I did not understand your question.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      the module does not have a separate pack and unpack method ... the module is using the core pack and unpack of Perl

      Sorry, that's not correct. As the AM post pointed out, those methods are coming from XS, specifically SysV.xs. It appears the job of IPC::SharedMem::stat::unpack is to take the fields of struct shmid_ds and store them in a Perl object of type IPC::SharedMem::stat (defined in IPC::SharedMem).

        Hello haukex,

        Thanks for correcting my assumption(s). The AM showed some code that possibly is an older version. I was under the impression that an older version was using internal implemented methods (pack and unpack). From the latest documentation on perldoc/IPC::SharedMem/SYNOPSIS is shown as an example:

        $shm->write(pack("S", 4711), 2, 2);

        I assumed that pack from the example above is the core pack. I also checked the source code from CPAN Source and I was unable to find the pack and unpack methods until I saw:

        sub stat { my $self = shift; my $data = ''; shmctl $self->id, IPC_STAT, $data or return undef; IPC::SharedMem::stat->new->unpack($data); }

        Thanks again for clearing things out. BR / Thanos.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: IPC::SharedMem::stat using Class::Struct
by Anonymous Monk on Sep 27, 2017 at 19:00 UTC
Re: IPC::SharedMem::stat using Class::Struct
by Anonymous Monk on Sep 28, 2017 at 12:00 UTC
    If you possibly can help it, don't use shared memory for IPC. Doing it properly requires complex logic involving mutexes and such. Thread-safe queues such as Thread::Queue are very readily available and far easier to manage. It is also much easier to test such logic and to determine that it actually works under production-level load.