Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageFrom: 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.