Uhh, did a quick update to my previous script, to print the movies on individual lines, as per your OP. Notice how most of the work was done inside the classes, adding and extending function and procedure calls as necessary. main really is only having to deal with the what side of the equation, as opposed to the how, which is encapsulated inside the classes.

use 5.16.2; use warnings FATAL => qw( all ); package movie; sub new { my $class = shift; my %params = @_; my $self = bless {}, $class; $self->title($params{title}); $self->start_year($params{start_year}); $self->end_year($params{end_year}); $self->media($params{media}); $self->based_on($params{based_on}); $self->company($params{company}); return $self; } sub title { my $self = shift; my $title = shift; $self->{title} = $title if $title; return $self->{title}; } sub start_year { my $self = shift; my $start_year = shift; $self->{start_year} = $start_year if $start_year; return $self->{start_year} // 'TBD'; } sub end_year { my $self = shift; my $end_year = shift; $self->{end_year} = $end_year if $end_year; return $self->{end_year} // 'TBD'; } sub media { my $self = shift; my $media = shift; $self->{media} = $media if $media; return $self->{media} // 'TBD'; } sub based_on { my $self = shift; my $based_on = shift; $self->{based_on} = $based_on if $based_on; return $self->{based_on} // 'TBD'; } sub company { my $self = shift; my $company = shift; $self->{company} = $company if $company; return $self->{company} // 'TBD'; } sub run_time { my $self = shift; my $result = $self->end_year(); $result = $self->start_year() if $self->media() eq 'film'; return $result; } sub print { my $self = shift; my $output = $self->title()." is a ".$self->start_year()." "; if ($self->media() eq 'tv') { $output .= "television series which "; if ($self->end_year()) { $output .= "completed in ".$self->end_year(); } else { $output .= "is still running."; } } else { $output .= $self->media()." based on ".$self->based_on()." by +".$self->company(); } say $output; } package library; sub new { my $class = shift; my %params = @_; my $self = bless {}, $class; $self->name($params{name}); $self->add_movie($_) for @{$params{movies}}; return $self; } sub name { my $self = shift; my $name = shift; $self->{name} = $name if $name; return $self->{name}; } sub add_movie { my $self = shift; my $movie = shift; $self->{movies}{$movie->title()}{movie} = $movie; $self->{movies}{$movie->title()}{location} = 'in'; } sub get_allmovies { my $self = shift; my @movies = map {$_->{movie}} values $self->{movies}; return @movies; } sub print { my $self = shift; say "I am a library called '".$self->name()."' and contain the fol +lowing movies:"; say "\t".$_->{movie}->title() foreach values %{$self->{movies}}; } package main; use XML::Simple; my @data = <DATA>; my $movies_data = eval { XMLin("@data") }; my $library = library->new(name => "Lucky's"); foreach my $movie (@{$movies_data->{movie}}) { $library->add_movie(movie->new(%$movie)); } $library->print(); say ""; foreach my $movie ($library->get_allmovies()) { $movie->print(); } __DATA__ <movies> <movie> <title>Firefly</title> <start_year>2002</start_year> <end_year>2003</end_year> <media>tv</media> </movie> <movie> <title>Criminal Minds</title> <start_year>2005</start_year> <end_year>tbd</end_year> <media>tv</media> </movie> <movie> <title>The 10th Kingdom</title> <start_year>2000</start_year> <end_year></end_year> <media>miniseries</media> </movie> <movie> <title>Iron Man</title> <start_year>2008</start_year> <end_year></end_year> <media>film</media> <based_on>comics</based_on> <company>Marvel Comics</company> </movie> <movie> <start_year>2007</start_year> <title>Tin Man</title> <media>miniseries</media> <based_on>novel</based_on> <company>L. Frank Baum</company> </movie> <movie> <title>The Avengers (1998)</title> <start_year>1998</start_year> <media>film</media> <based_on>television series</based_on> <company>Thames Television</company> </movie> </movies>

In reply to Re^3: How do I go from procedural to object oriented programming? by SimonPratt
in thread How do I go from procedural to object oriented programming? by Lady_Aleena

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.