Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Novice problem: How to push a MooX Struct into a list?

by dissident (Sexton)
on Oct 31, 2014 at 21:34 UTC ( [id://1105754]=perlquestion: print w/replies, xml ) Need Help??

dissident has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I got stuck with a "Can't use an undefined value as an ARRAY reference at ..." error when trying to push a struct using MooX::Struct onto an array. Please apologize I am still a novice monk and I don't understand why this error happens. I probably did some smaller or larger mistake, but have no clue what could be wrong. :(

Here a stripped-down piece of code. It fails at the line with the push.

use strict; use warnings; use diagnostics; use 5.014; # so push/pop/etc work on scalars (experimental) use MooX::Struct -rw, Dirnode => [ qw( $dirname @subdlist ) ]; my $dirroot = crawldir( "."); sub crawldir { my $curpath = shift; my $dno = Dirnode[]; $dno->dirname( $curpath ); opendir(DIR, $curpath); my @files = readdir(DIR); closedir(DIR); print "crawldir in directory ", $dno->dirname, "\n"; foreach (@files) { next if $_ eq '.' or $_ eq '..'; my $file_path = "$curpath/$_"; $file_path =~ s|/+|/|g; if (-d $file_path) { print "crawldir in directory ", $dno->dirname, ": calling c +rawldir( $curpath\/$_)\n"; push @{$dno->subdlist}, crawldir( "$curpath\/$_" ); } } return $dno; }

Replies are listed 'Best First'.
Re: Novice problem: How to push a MooX Struct into a list?
by boftx (Deacon) on Oct 31, 2014 at 21:52 UTC

    If I am not mistaken, change my $dno = Dirnode[]; to my $dno = Dirnode->new;

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

      Thank you boftx, but this sadly didn't work. new() is the most common constructor but in Perl constructor functions can be named differently AFAIR. MooX seems to have no new()...

      According to the documentation and the working examples, the MooX struct could be initialized by

      my $dno = Dirnode["", undef];

      but undef yields an error message and I don't know how a zero-element array could be specified in another way. So I did the init with an empty list which works except for the array variables.

      So maybe my question should be better asked as "how to initialize a zero-element array?" perhaps?

        You could create with an empty array reference instead of undef:

        my $dno = Dirnode[$curpath, []];

        Check this link: http://api.metacpan.org/source/BOFTX/Games-Dukedom-v0.1.1/lib/Games/Dukedom.pm

        You will see where I use MooX::Struct and later initialize an attribute with StructName->new; in several places.

        You can specify a zero-element array with my @array = ();. However, I seem to recall that if you do not provide a value for an attribute in MooX::Struct the element is not created and is in fact undef. But you are using the accessor in your push statement so that shouldn't be a problem.

        If I get a break from the kids tonight I'll run your code and see what I can find. (No promises on Halloween, though.)

        Update: The actual CPAN docs (which include the module source and an executable) can be found here if you're interested: Games::Dukedom

        You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1105754]
Approved by Paladin
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found