For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2004 > ref() is not reporting correctly









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author ref() is not reporting correctly
Rich Fernandez

2004-08-05, 8:55 pm

I have a hash which looks something like this:
%hash = ( foo => 'a', bar => 'b', filespec => "$filespec");

I also have a function which includes the following code:

# Figure out if we were passed a filename or an array ref
if ( exists $info -> {filespec} ) {

$filespec = $info -> {filespec};

if ( ref($filespec) eq "ARRAY" ) {
#process filenames from array;
print "filespec = $filespec, filespec type = ", ref($filespec),
"\n";
print "Processing an array\n";
} else {
print "Processing single filename: $filespec\n";
print "filespec = $filespec, filespec type = ", ref($filespec),
"\n";
}
} else {
die "No Files passed in for processing!\n";
}

__END__


I'm passing a reference to the hash into the function. The function is
actually
contained in a module I'm developing, but I don't think that should matter?

When processing gets to this part of the code the "if" condition always
fails
Instead it always processes the "else" block.

When $filespec is a string I get (correct) output that looks like this:
filespec = somefile.txt, filespec type =

When $filespec is an arrayref I get (incorrect) output that looks like this
out of the else block:
filespec = ARRAY(0x3c01b424), filespec type =

where I expect to see something like:
filespec = ARRAY(0x3c01b424), filespec type = ARRAY

Anybody see where I've gone wrong?

Thanks for the help!

richf

Jenda Krynicky

2004-08-05, 8:55 pm

From: Rich Fernandez <rfernandez@arrow.com>
> I have a hash which looks something like this:
> %hash = ( foo => 'a', bar => 'b', filespec => "$filespec");
>
> I also have a function which includes the following code:
>
> # Figure out if we were passed a filename or an array ref
> if ( exists $info -> {filespec} ) {
>
> $filespec = $info -> {filespec};
>
> if ( ref($filespec) eq "ARRAY" ) {
> #process filenames from array;
> print "filespec = $filespec, filespec type = ",
> ref($filespec),
> "\n";
> print "Processing an array\n";
> } else {
> print "Processing single filename: $filespec\n";
> print "filespec = $filespec, filespec type = ",
> ref($filespec),
> "\n";
> }
> } else {
> die "No Files passed in for processing!\n";
> }
>
> __END__
>
>
> I'm passing a reference to the hash into the function. The function is
> actually contained in a module I'm developing, but I don't think that
> should matter?
>
> When processing gets to this part of the code the "if" condition
> always fails Instead it always processes the "else" block.


Now you know why writing
"$variable"
is a bad idea.

You forced Perl to stringify the reference, therefore the $info-
>{filespec} is not a reference anymore, it's string

"ARRAY(0x3c01b424)".

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com