Graph::Writer(3pm)

πŸ’– Graph::Writer β€” the base class era πŸ’–

okay so bestie, we're looking at Graph::Writer, a User Contributed Perl Documentation page, and it's literally the blueprint page for making Graph file format writers. it's giving founding mother energy fr fr πŸ‘‘


✨ NAME ✨

Graph::Writer β€” bestie it's the base class for Graph file format writers. that's the whole vibe. πŸ’…


🌸 SYNOPSIS 🌸

here's the little starter pack, copy it verbatim okay:

package Graph::Writer::MyFormat;
use Graph::Writer;
use vars qw(@ISA);
@ISA = qw(Graph::Writer);

sub _write_graph
{
  my ($self, $graph, $FILE) = @_;

  # write $graph to $FILE
}

πŸ¦‹ DESCRIPTION πŸ¦‹

so Graph::Writer is the base class for Graph file format writers. a specific subclass of Graph::Writer handles one specific file format, and generates a Graph, represented using Jarkko Hietaniemi's Graph class. iconic honestly.

NOT me creating an instance of this class myself πŸ’€ bestie you should never do that, it is only meant for subclassing. if you try to create an instance of Graph::Writer, the constructor will throw an exception. periodt.

πŸ‘‘ METHODS πŸ‘‘

new()

Constructor β€” generate a new writer instance. this is a virtual method, or whatever the correct lingo is πŸ™ˆ. you're not meant to call this on the base class, it's inherited by the subclasses. like, if you do something like:

$writer = Graph::Writer->new();

it will throw an exception. no cap, it's not gonna work bestie. 😭

write_graph()

read a graph from the specified file:

$graph = $writer->write_graph($file);

the $file argument can either be a filename, or a filehandle for a previously opened file. either one, she's flexible like that. 🫢


🧚 SUBCLASSING 🧚

to create your own graph format writer, create a module which subclasses Graph::Writer. for example, suppose DGF is a directed graph format β€” create a Graph::Writer::DGF module, with the following structure:

package Graph::Writer::DGF;

use Graph::Writer;
use vars qw(@ISA);
@ISA = qw(Graph::Writer);

sub _write_graph
{
  my $self  = shift;
  my $graph = shift;
  my $FILE  = shift;

  while (<$FILE>)
  {
  }

  return 1;
}

1;

note the leading underscore on the _write_graph() method. the base class provides the public method, and invokes the private method which you're expected to provide, as above. that's the deal, that's the assignment. πŸ“

if you want to perform additional initialisation at construction time, you can provide an _init() method, which will be invoked by the base class's constructor. you should invoke the superclass's initialiser as well, as follows:

sub _init
{
  my $self = shift;

  $self->SUPER::_init();

  # your initialisation here
}

someone can then use your class as follows:

use Graph::Writer::DGF;

$writer = Graph::Writer::DGF->new();
$writer->write_graph($graph, 'foo.dgf');

slay. your little module is out there living its main character life ✨


🌈 SEE ALSO 🌈

  • Graph β€” Jarkko Hietaniemi's modules for representing directed graphs, available from CPAN under modules/by-module/Graph/
  • Algorithms in Perl β€” the O'Reilly book has a chapter on directed graphs, which is based around Jarkko's modules.
  • Graph::Writer::Dot β€” a simple subclass of this class for writing graphs in the file format used by dot, which is part of the graphviz package from AT&T.
  • Graph::Writer::VCG β€” a simple subclass of this class for writing graphs in the file format used by VCG, a tool for visualising directed graphs, initially developed for visualising compiler graphs.
  • Graph::Writer::XML β€” a simple subclass of this class for writing graphs as XML, using a simple graph markup.
  • Graph::Reader β€” a baseclass for Graph file format readers.

πŸŽ€ REPOSITORY πŸŽ€

<https://github.com/neilb/Graph-ReadWrite>

go look, it's right there bestie 🫢


πŸ’œ AUTHOR πŸ’œ

Neil Bowers <neil@bowers.com>


Copyright (c) 2001-2012, Neil Bowers. All rights reserved. Copyright (c) 2001, Canon Research Centre Europe. All rights reserved.

This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


okay and that's the whole page!! this was documented by perl v5.32.1 and dated 2021-09-19, so if you're reading this in the future, the vibes are preserved, don't worry πŸ’– bye bestie, go subclass something iconic πŸŒΈπŸ’…

πŸ“¦ where this page comes from