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

Howto return a reference of an array of custom class?

by true_atlantis (Acolyte)
on Sep 05, 2007 at 17:00 UTC ( [id://637206]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to write a function that returns an array reference of 'mat::Network' objects. But the returned array doesnt seem to be what im expecting. The error seems to be thinking im trying to call something on 'Main' not 'mat::Network'

Example:
my $aa=x(); foreach(@$aa){ $_.p(); } sub x { my @ret; my $a=mat::Network->new({name=>'test'}); my $b=mat::Network->new({name=>'test2'}); push(@ret,$a); push(@ret,$b); return(\@ret); }

will return:

Undefined subroutine &main::p

What am i doing wrong? I feel like I have to cast '$_' as 'mat::Network'?

Thanks in advance.

Replies are listed 'Best First'.
Re: Howto return a reference of an array of custom class?
by Fletch (Bishop) on Sep 05, 2007 at 17:04 UTC

    For one thing you're attempting to use the concatenation operator ($_.p() parses as "concatenate the string in $_ with the return value from calling the subroutine p with no arguments") to invoke a method rather than the correct syntax, $_->p().

Re: Howto return a reference of an array of custom class?
by Corion (Patriarch) on Sep 05, 2007 at 17:04 UTC

    The line

    $_.p()

    likely isn't doing what you think it does. See perlop for the Perl operators. The dot (.) is the string concatenation operator. So you're concatenating your object with the result of the function p, which is not defined, hence the error message undefined subroutine &main::p. Maybe you wanted the arrow operator -> to call a method?

Re: Howto return a reference of an array of custom class?
by jhourcle (Prior) on Sep 05, 2007 at 17:05 UTC
     $_.p();

    '.' is the concatination operator in the current version of perl. You likely wanted $_->p();

Re: Howto return a reference of an array of custom class?
by andyford (Curate) on Sep 05, 2007 at 17:08 UTC

    I don't think your problem is the mat::Network routines, your program can't find the "p" subroutine. Where is it? Do you need to include some other module or something that contains the definition of p()?

    non-Perl: Andy Ford

      jess... im an idiot... my problem is that i have been switching between java and perl, and classes are way different in each. thanks everyone

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-20 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found