Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^4: Stuck on packages

by Yoda_Oz (Sexton)
on Jan 04, 2007 at 06:43 UTC ( #592891=note: print w/replies, xml ) Need Help??

in reply to Re^3: Stuck on packages
in thread Stuck on packages

ok i understand now!
so what would i need to code if i wanted something like this?
sub new { my $class = shift; return bless { engine => "engine", wheels => "wheels", etc...}, $c +lass; } sub get_foo { my $self = shift; return $self->{engine,wheels,etc...}; }
how do i make it an array or hash or whatever?

Replies are listed 'Best First'.
Re^5: Stuck on packages
by SheridanCat (Pilgrim) on Jan 04, 2007 at 07:16 UTC
    Within the getter method you can access any of the attributes, so you just have to put them in the data structure you are wanting to return, such as:
    sub get_as_array { my $self = shift; return [ $self->{engine}, $self->{wheels} ]; }
    Is there something else you were wanting to do? If you dump the return of this, you'll see it's in an array.
      ARRAY(0x18012b8) is the output.
      here is the entire code:
      #!/usr/bin/perl package Foo; sub new { my $class = shift; return bless { engine => "engine", wheels => "wheels" }, $class; } sub get_as_array { my $self = shift; return [ $self->{engine}, $self->{wheels} ]; } 1; my $foo = Foo->new; print $foo->get_as_array;
        You need to dereference the array reference returned by get_as_array() by saying @{$foo->get_as_array} before you print it.
        ARRAY(0x18012b8) is the output.

        Which was expected. Indeed in the post you're replying to, but failed to quote properly, there was clearly written "If you dump the return of this, you'll see it's in an array."

        Just use (e.g.) Data::Dumper's Dump instead of print.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://592891]
[hippo]: Benchmarking?
[GotToBTru]: hippo++
erix .oO( apparently but does not GotToBFast )
[Discipulus]: now i must come back to my house/hoven monks: dont do interesting discussion in my absence!
[erix]: or apparently, now it does :)
[GotToBTru]: heh
erix prods ambrus to (re-)start cbstream...
[GotToBTru]: I've got a program with an if ... or ... statement with 80 clauses
[stevieb]: 80 clauses? dispatch table?

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2017-06-22 12:56 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (519 votes). Check out past polls.