Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

two problems about passing var between classes

by benlaw (Scribe)
on Apr 11, 2002 at 15:07 UTC ( #158335=perlquestion: print w/ replies, xml ) Need Help??
benlaw has asked for the wisdom of the Perl Monks concerning the following question:

hi monks, i got 2 problems on passing variables between them

1. i try to pass a string "problem1" to a class, but it result "packa=HASH(0x1b0effc)" when I pass the value to the class, what i am not expected.

$string = $obj->pass("problem1");
2. I have a array inside a hash, mm when i pass the array back into main.pl, how to get value?
if ($key eq "problem2"){ foreach (@ha{$key}){ print "array : @{$_}\n";
full code here
#in main.pl use packa; $obj = packa->new; $string = $obj->pass("problem1"); #print $string; %ha = $obj -> passhash; foreach $key (keys (%ha)){ if ($key eq "problem2"){ foreach (@ha{$key}){ print "array : @{$_}\n"; } } } #in packa.pm package packa; sub new{ my $class = shift; my $self = {}; bless($self); return $self; } sub pass{ my ( $problem1 )= @_; print "$problem1\n"; } sub passhash{ %hash = qw { dog voa cat mice }; $hash {'problem2'} = [["ha" , "thing", "thorw"], ["good", "ssa" , "ham"], ["as", "kid", "wo"], ]; return %hash; } 1;

Comment on two problems about passing var between classes
Select or Download Code
Re: two problems about passing var between classes
by Biker (Priest) on Apr 11, 2002 at 15:16 UTC

    1 - The first parameter to your method will always be the object handle. Try the following:

    sub pass{ shift(); # To ignore the handle # or # my $self = shift(); if you want to use it. my ( $problem1 )= @_; print "$problem1\n"; }



    Everything went worng, just as foreseen.

Re: two problems about passing var between classes
by perlplexer (Hermit) on Apr 11, 2002 at 15:20 UTC
    sub pass{ my ( $problem1 )= @_; # <-- Your mistake is here print "$problem1\n"; }
    First value passed to pass() is a reference to $obj.
    The following will work.
    sub pass{ my ( $self, $problem1 )= @_; print "$problem1\n"; }


    --perlplexer
Re: two problems about passing var between classes
by Ovid (Cardinal) on Apr 11, 2002 at 16:03 UTC

    Style issues.

    A couple of things that you probably want to keep in mind while coding. First, if you won't use strict, at least be sure to lexically scope your variables by declaring them with my. For example, in your &passhash method, you don't declare your hash, thus making it a package variable and you lose encapsulation.

    • Whenever you are doing OO programming, remember that the first argument to the method is the object.
    • Be sure to use proper (or at least consisten) indenting to indicate scope.
    • Your constructor should use the two-argument form of bless. Otherwise, you can't inherit from this.
    package packa; use strict; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } sub pass{ my ( $self, $problem1 )= @_; print "$problem1\n"; } sub passhash{ my %hash = qw { dog voa cat mice }; $hash{'problem2'} = [ ["ha" , "thing", "thorw"], ["good", "ssa" , "ham"], ["as", "kid", "wo"], ]; return %hash; }

    Hope this helps.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: two problems about passing var between classes
by benlaw (Scribe) on Apr 12, 2002 at 01:58 UTC
    thx a lot :) !
Re: two problems about passing var between classes
by Hofmator (Curate) on Apr 12, 2002 at 11:13 UTC

    I think noone commented so far on your second problem. So let me do that and look at your code first:

    %ha = $obj -> passhash; foreach $key (keys (%ha)){ if ($key eq "problem2"){ foreach (@ha{$key}){ print "array : @{$_}\n"; } } }
    The loop over all keys doesn't make much sense for a hash, after all, you use a hash to get easy access to its elements via the key (here: 'problem2'). So instead you would write: $ha{'problem2'}.

    The entry of the hash %ha corresponding to the key 'problem2' is a reference to an array of arrays. To get to the values of that you have to dereference it: @{$ha{'problem2'}} or equivalent @$ha{'problem2'}.

    Now you have an array which elements are references to arrays. You could print the elements of these (referenced) subarrays like this:

    foreach my $arr_ref (@$ha{'problem2'}) { print "subarray: @{$arr_ref} \n"; }

    Note: Your code works, if you replace the hash slice notation (@ha{$key}) with the dereferencing of the hash entry @$ha{$key}. The hash slice notation is normally used to get multiple values out of hash in one go, you (probably accidently) used it to extract just one value. This is then equivalent to $ha{$key}. An example of a good usage of a hash slice would be

    my %hash; @hash{qw/one two three/} = qw/1 2 3/;

    -- Hofmator

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2014-07-31 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls