Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Here's an exercise in fun OOP that's fairly unnecessary, but just in case you REALLY want objects :)
package LinkedList; use strict; my $HEAD_NODE = undef; # use a global var to get the head node sub new { my $this = shift; my $class = ref($this) || $this; my $self = {}; $self->{_ID} = shift || "Id"; $self->{_NAME} = shift || "Default"; $HEAD_NODE = $self unless defined $HEAD_NODE; $self->{_NEXT} = $HEAD_NODE; bless $self, $class; } sub name { my $self = shift; $self->{_NAME} = shift if @_; return $self->{_NAME}; } sub id { my $self = shift; $self->{_ID} = shift if @_; return $self->{_ID}; } # this really should handle references to objects, and not # the object itself... sub add_node { my $self = shift; my $node_to_add = shift; $self->{_NEXT} = $node_to_add; } # this really should handle references to objects, and not # the object itself... sub next { my $self = shift; return $self->{_NEXT}; } # may not be necessary DESTROY { my $self = shift; $self->{_NEXT} = undef; } 1; ---- #!/usr/bin/perl use strict; use LinkedList; my $first_node = LinkedList->new( 1, "data" ); my $second_node = LinkedList->new( 2, "more data"); my $third_node = LinkedList->new(3, "even more data"); $first_node->add_node( $second_node ); $second_node->add_node( $third_node ); my $node = $first_node; for ( my $i = 0; $i < 100; $i++ ) { print $node->id." ".$node->name."\n"; $i++; $node = $node->next; }
Update: A note from perlobj:
In the meantime, the best solution is to create a non-
recursive container class that holds a pointer to the self-
referential data structure. Define a DESTROY method for 
the containing object's class that manually breaks the 
circularities in the self-referential structure.
So maybe instead, call the above package (class) LinkedListNode and have a container LinkedList, just to be safe.

In reply to Re: OOP/Linked List Question by RazorbladeBidet
in thread OOP/Linked List Question by hok_si_la

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (7)
    As of 2014-12-28 18:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (182 votes), past polls