Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^3: Sorting array, getting modification of read-only value error

by GrandFather (Sage)
on Nov 29, 2012 at 23:20 UTC ( #1006374=note: print w/replies, xml ) Need Help??

in reply to Re^2: Sorting array, getting modification of read-only value error
in thread Sorting array, getting modification of read-only value error

We are not at all annoyed to see someone trying, especially when they consider and make use of the advice they are given!

Actually I'd put neatness first because with a little practice syntax and logic both flow from it and if it is neat at least someone else may be prepared to give a hand with tricky bits.

There isn't enough context to show a "tidy" version of your code, but the following sample extrapolated somewhat from what you seem to be doing may be of interest. Note that the code below makes use of Perl objects to avoid global variables and to avoid domain specific knowledge leaking too much between different areas of the code.

#!/usr/bin/perl use strict; use warnings; use Carp qw(); package Character; sub new { my ($class, %props) = @_; # Validation of %props here as required Carp::confess "Characters require a 'name' property" if !$props{na +me}; return bless \%props, $class; } sub newFromStr { my ($class, $str) = @_; my $config = extract($str); return $class->new(%$config); } sub newFromFile { my ($class, $filename) = @_; open my $fin, '<', $filename or Carp::confess "Can't open $filenam +e: $!\n"; my $str = do {local $/; <$fin>}; close $fin; return $class->newFromStr($str); } sub extract { my ($str) = @_; require ''; return YAML::Load($str); } # subs defining what a Character is here sub Name { my ($self) = @_; return $self->{name}; } sub SetAttributes { my ($self, %attribs) = @_; for my $attrib (keys %attribs) { my ($rolls, $die) = $attribs{$attrib} =~ /(\d+)d(\d+)/i or nex +t; $self->{$attrib} = 0; $self->{$attrib} += int(1 + rand($die)) for 1 .. $rolls; } } package Game; sub new { my ($class, %props) = @_; # Validation of %props here as required return bless \%props, $class; } sub AddCharacter { my ($self, $character) = @_; my $name = $character->Name(); Carp::confess "Character name must be unique. $name is already in +use!" if exists $self->{characters}{$name}; $self->{characters}{$name} = $character; } sub LoadCharacters { my ($self, $source) = @_; opendir my ($scan), $source or Carp::confess "Can't open $source: +$!\n"; my @files = grep {-f $_} map {"$source\\$_"} readdir $scan; closedir $scan; foreach my $file (@files) { my $character = Character->newFromFile($file); $character->SetAttributes(hp => '1d20'); $self->AddCharacter($character); } } sub DumpCharacters { my ($self) = @_; my $format = "%-15s %5s %5s %5s\n"; printf $format, qw(Name init dex hp); for my $name (sort keys %{$self->{characters}}) { printf $format, $name, @{$self->{characters}{$name}}{qw(init d +ex hp)}; } } package main; my $testFolder = "./Characters"; createTestCharacters($testFolder); my $game = Game->new(); $game->LoadCharacters($testFolder); $game->DumpCharacters(); sub createTestCharacters { my ($folder) = @_; my @characters = ( {name => 'Red', init => 5, dex => 10}, {name => 'Spotty', init => 8, dex => 15}, {name => 'Night Runner', init => 15, dex => 17}, ); require ''; for my $character (@characters) { my $name = $character->{name}; YAML::DumpFile("$folder\\$name.yaml", $character); } }


Name init dex hp Night Runner 15 17 14 Red 5 10 16 Spotty 8 15 10
True laziness is hard work

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006374]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2018-06-25 14:33 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.