Real Perl has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I need your wisdom to understand why I am getting a SCALLAR(....) instead of the value I expect. Here is where I assign the returned value after I call the sub:
sub confirm(){ if($gGui{single}==1){ print "source variable BEFORE fix_path $gGui{original_source}\n"; $gGui{original_source} = \&fix_path("$gGui{original_so +urce}"); print "source variable AFTER fix_path $gGui{original_source}\n"; . . .
This is the fix_path sub:
sub fix_path{ my ($path) = @_; my @patharray= split '/', $path; my @newpatharray; $path = ""; foreach (@patharray){ $_ =~s/ /_/g; $_ .= "\\"; push @newpatharray, $_; }#foreach foreach (@newpatharray){ $path .= $_; }#foreach return $path; }#fix_path
With print statements, I was able to determine that the value sent to and returned by the fix_path are correct, but the second print statement give me SCALAR(0x3618594).
Any idea would be appreciated.
Thanks in advance for your time.
Claire

Replies are listed 'Best First'.
Re: Assigning a returned value to a hash variable
by pg (Canon) on Sep 04, 2005 at 04:45 UTC

    Get rid of the \ in front of call to fix_path (actually even that &, but that's not the problem here), otherwise you stored a ref to a scalar instead of a scalar. In the future, if you can simplify your code to a runnable form, it will help those who are trying to help you.

    my $a = &fix_path(); print "source variable AFTER fix_path $a\n"; sub fix_path{ return "abcd"; }
Re: Assigning a returned value to a hash variable
by GrandFather (Saint) on Sep 04, 2005 at 06:37 UTC

    To add a little emphasis to pg's reply: there are a number of good reasons for providing a "cleaned up" and runable version of your code when posting a question.

    1/ If other Monks can just download your sample code and run it to see the problem for themselves they are more likely to investigate something subtle.

    2/ Even if you can't provide a sample that runs and demonstrates the problem, making the code as simple as you can helps both you and the Monks in focusing on the problem at hand.

    3/ In the process of simplifying the code you will better appreciate the nature of problem. Indeed you may even solve your own problem.

    As an asside, by writing sub confirm() you are saying explicitly that confirm does not take any parameters and that it is a contender for inlining. That may or may not have been what you were intending, but is a trap that is often sprung.

    Note too that the form TYPE(0x...) is almost always a clue that you have attempted to print out a reference, rahter than the thing referred to.


    Perl is Huffman encoded by design.
Re: Assigning a returned value to a hash variable
by jonadab (Parson) on Sep 04, 2005 at 09:58 UTC

    If you want to better understand what's going on here, the topic you want to read up on is references. The perldoc page for this is called perlref. The Camel book also has a chapter on this, and it is well covered in Effective Perl Programming as well.

    update: ISBN linking does not work the way I thought I remembered... Oh, I see, it's deliberate.