Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Return Value from sub

by Dr Manhattan (Beadle)
on Sep 06, 2013 at 09:23 UTC ( #1052676=perlquestion: print w/ replies, xml ) Need Help??
Dr Manhattan has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

This may be a very silly question- I am using a subroutine to search through a list of names. The sub finds correct name and returns it. Everything seems to work fine when I debug until the sub returns the name, then the return value suddenly becomes 1

I am probably just missing something obvious? Any help would be appreciated

Comment on Return Value from sub
Re: Return Value from sub
by daxim (Chaplain) on Sep 06, 2013 at 09:27 UTC
    Context problem. It should be

    my ($return_value) = the_function

    not

    my $return_value = the_function

Re: Return Value from sub
by vinoth.ree (Parson) on Sep 06, 2013 at 09:28 UTC

    Could you please post your code so that we can help you better.

    You need to check how you are printing the return value, scalar or list context.?


    All is well
Re: Return Value from sub
by Corion (Pope) on Sep 06, 2013 at 09:31 UTC

    You don't show the relevant code, but my guess is that the issue is scalar context vs. list context.

    Most likely you have something like

    sub nonblank { return grep { /\S/ } @_ } print nonblank('foo'); # list context through print() print $_ for nonblank('foo'); # explicit list context my $temp= nonblank('foo'); # scalar context print $temp; # still scalar ( $temp)= nonblank('foo'); # list context, discarding all but the firs +t element print $temp; # First element of that list $temp= (nonblank('foo'))[0]; # Scalar context in assignment, but we + only take the first element explicitly print $temp;

    ... and grep returns a list, even if you are really, really certain that there will only ever be one matched element. And that list, when evaluated in scalar context turns into the number of elements. Which is 1.

      I'm not using grep

      my ($name) = seekName($id) print "$name\n"; sub seekName { my ($id) = $_[0]; open (IN, "<:utf8", "names.txt") or die "Can't open: $!"; seek In, $id, 0; my $line = <In>; return $line; }

      Output just gives a bunch of 1's

        Context problem. Change the relevant line to:

        my $name = seekName($id);

        Alternate solution: return an array from your subroutine:

        return ($line); Update: added the second alternate solution.

        When I try to run the code you posted, I only get these errors:

        C:\>perl -w tmp.pl Name "main::IN" used only once: possible typo at tmp.pl line 8. Use of uninitialized value $id in seek at tmp.pl line 10. seek() on unopened filehandle In at tmp.pl line 10. readline() on unopened filehandle In at tmp.pl line 11. Use of uninitialized value $name in concatenation (.) or string at tmp +.pl line 3.

        Please try to make sure that the code you post actually exhibits the problem you are trying to diagnose.

        When I change your code to the following:

        my $id= shift; my ($name) = seekName($id); print "$name\n"; sub seekName { my ($id) = $_[0]; open (IN, "<:utf8", $0) or die "Can't open: $!"; seek IN, $id, 0; my $line = <IN>; return $line; }

        ... it assigns $name a value depending on what number I give on the command line. So I would assume that this works and does not exhibit the problem of always printing 1.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2014-11-23 19:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls