# perldoc > IPC::SharedMem

---
type: CommandReference
command: IPC::SharedMem
mode: perldoc
section: ""
source: perldoc
---

## Quick Reference
- `$shm = IPC::SharedMem->new(IPC_PRIVATE, 8, S_IRWXU)` — Create a new shared memory segment
- `$shm->write(pack("S", 4711), 2, 2)` — Write data at offset
- `$data = $shm->read(0, 2)` — Read data from offset
- `$ds = $shm->stat` — Get segment statistics
- `$shm->remove` — Remove the segment
- `$shm->attach` — Attach for memory-mapped access
- `$shm->detach` — Detach from segment
- `my $id = $shm->id` — Get the segment identifier

## Name
SysV Shared Memory IPC object class

## Synopsis
perl
use IPC::SysV qw(IPC_PRIVATE S_IRUSR S_IWUSR);
use IPC::SharedMem;

$shm = IPC::SharedMem->new(IPC_PRIVATE, 8, S_IRWXU);
$shm->write(pack("S", 4711), 2, 2);
$data = $shm->read(0, 2);
$ds = $shm->stat;
$shm->remove;
## Methods
- `new(KEY, SIZE, FLAGS)` — Creates a new shared memory segment of `SIZE` bytes associated with `KEY`. A new segment is created if `KEY` is `IPC_PRIVATE` or if `KEY` does not already have a segment and `(FLAGS & IPC_CREAT)` is true. `FLAGS` sets permissions; avoid execute bits on some systems.
- `id` — Returns the shared memory identifier.
- `read(POS, SIZE)` — Reads `SIZE` bytes from the segment at offset `POS`. Returns the string or `undef` on error. The return value becomes tainted.
- `write(STRING, POS, SIZE)` — Writes `SIZE` bytes of `STRING` to the segment at offset `POS`. Returns true on success, false on error.
- `remove` — Removes the segment from the system, or marks it for removal if still attached.
- `is_removed` — Returns true if the segment has been removed or marked for removal.
- `stat` — Returns an `IPC::SharedMem::stat` object (subclass of `Class::Struct`) with fields: `uid`, `gid`, `cuid`, `cgid`, `mode`, `segsz`, `lpid`, `cpid`, `nattch`, `atime`, `dtime`, `ctime`.
- `attach([FLAG])` — Permanently attaches to the segment; subsequent access uses `memread`/`memwrite` instead of `shmread`/`shmwrite`. Returns true on success, false on error. See `shmat(2)`.
- `detach` — Detaches from the segment. Returns true on success, false on error. See `shmdt(2)`.
- `addr` — Returns the address of the attached shared memory in a format suitable for `pack('P')`. Returns `undef` if not attached.

## See Also
- [IPC::SysV](http://localhost/phpMan.php/perldoc/IPC::SysV/markdown)
- [Class::Struct](http://localhost/phpMan.php/perldoc/Class::Struct/markdown)