References Subroutines and Arrays

K

Ketema

I am a little lost on how PERL handles references and array's,
especially when you pass and array to a subroutine. Here is some code
I have working to try and get a better understanding. I read perldoc
perlref, and I just don't see why this isn't working...Please help!

#I'm working with a mime::entity object from net::pop3
&getParts($mime_entity);

sub getParts {
print "The wierd variable \@_ consists of: ".scalar @_."
variables\n";
print "That passed variable: ".scalar @_->[0]." is a:
".ref(@_->[0])."\n";
#lets take the address of the passed array out of the hard to read
@_ variable which is a list of everything passed to the sub
my $passedArray = \@_[0];
print "passedArray now holds the address for the passed object.
passedArray holds: $passedArray\n";
print "The object located at the memory address passedArray holds is
a: ". ref({$passedArray})."\n";
print "Lets do some Tests.\n";
print "\@_->{'ME_Parts'} attribute is a
:".ref(@{@_[0]->{'ME_Parts'}});
print "passedArray->{'ME_Parts'} attribute is a
:".ref(@$$passedArray->{'ME_Parts'});
die "Debugging The real Way!";
}
 
B

Ben Morrow

I am a little lost on how PERL handles references and array's,

Perl or perl... see the FAQ.
especially when you pass and array to a subroutine. Here is some code
I have working to try and get a better understanding. I read perldoc
perlref, and I just don't see why this isn't working...Please help!

#I'm working with a mime::entity object from net::pop3

MIME::Entity, Net::pOP3.
&getParts($mime_entity);

At the risk of infuriating Tassilo :), don't call subs with & unless you
know you need those semantics, not least because it will confuse readers
of your code.
sub getParts {
print "The wierd variable \@_ consists of: ".scalar @_."
variables\n";

Rather than put "\n" on the end of every print, you want to learn to use
$\:

local $\ = "\n";
print "That passed variable: ".scalar @_->[0]." is a:
".ref(@_->[0])."\n";

@_ is an array, not an arrayref (it starts with @), so you access
elements with $_[0]. Yes, this is confusing :). This element, $_[0], is
a scalar; if it is an arrayref you can get at the array with @{ $_[0] }.

print 'that passed variable: ' . scalar $_[0] . 'is a: ' . ref $_[0];
#lets take the address of the passed array out of the hard to read
@_ variable which is a list of everything passed to the sub
my $passedArray = \@_[0];

my $passedArray = $_[0];
print "passedArray now holds the address for the passed object.
passedArray holds: $passedArray\n";
print "The object located at the memory address passedArray holds is
a: ". ref({$passedArray})."\n";

Oh my goodness me. What on Earth did you think that would do, and why
did you think that? That will attempt to construct an anonymous hash out
of $passedArray, fail because it doesn't have an even number of
elements, and pass a reference to that hash to the ref() function.
print "Lets do some Tests.\n";
print "\@_->{'ME_Parts'} attribute is a
:".ref(@{@_[0]->{'ME_Parts'}});
print "passedArray->{'ME_Parts'} attribute is a
:".ref(@$$passedArray->{'ME_Parts'});

I think you are completely confused. Read perldata and perlref again,
and try some simpler examples until you get the hang of it.

Ben
 
I

Iain

(e-mail address removed) (Ketema) wrote in
I am a little lost on how PERL handles references and array's,
especially when you pass and array to a subroutine. Here is some code
I have working to try and get a better understanding. I read perldoc
perlref, and I just don't see why this isn't working...Please help!

*snip* some very confused code.

The following might help you get things a bit clearer in your head.
Perl's parameter-passing technique is simple -- so simple in fact that
it often confuses those used to other high-level languages!

If you don't understand how or why the following code does what it does,
have a read of 'perldoc perlsub' and 'perldoc perlref'. Play with the
code, adding in your own parameters, look at the code again, re-read the
documentation and if you still can't work it out, feel free to post here
again!


#!/usr/bin/perl

use strict;
use warnings;

# Simplest case
print "Passing parameters directly: ";
show_params( 'one', 2, 3, 'four items' );

my @array;

@array = ( 'foo', 'bar', 'wibble' );
print "Array of 3 scalars: ";
show_params( @array );

@array = ( [ 'alpha', 'beta', 'gamma' ], [ 'a', 'b', 'c' ],
[ 'aleph', 'beth', 'gimel' ] );
print "Array of 3 arrayrefs (LoL): ";
show_params( @array );

@array = ( { foo => 'bar' }, { fee => 'far' } );
print "Array of 2 hashrefs (LoH): ";
show_params( @array );



sub show_params {

# Perl _ALWAYS_ passes all parameters into the special array @_

# A C programmer might do the following (but please don't):
# my ($argc, $argv) = (scalar(@_), @_);

my @parameters = @_;
print "I was passed " . scalar( $n_params ) . " parameters\n";

foreach my $i ( 0..@parameters ) {
my $param = $parameters[$i];
# This loop sets $param to each parameter in turn. We can find out
# what sort of thing $param is using the ref() function:
ref( $param ) =~ /ARRAY/ and print " Parameter $i is an ARRAYREF\n"
or
ref( $param ) =~ /HASH/ and print " Parameter $i is a HASHREF\n"
or
ref( $param ) and print " Parameter $i is an OBJECT\n"
or
print " Parameter $i is a SCALAR\n";
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top