in reply to IPC::SharedMem::stat using Class::Struct

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!

Replies are listed 'Best First'.
Re^2: IPC::SharedMem::stat using Class::Struct (UPDATED)
by haukex (Archbishop) on Sep 29, 2017 at 06:55 UTC
    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!