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

I'm back again, struggling with some basics (I have read up on this but I just can't figure it). I will post code below but in a nutshell I pass a string to a function and do stuff, then return a variable. The problem is if, at the end of the function I use:
print $title;

I can see the result in the perl script I called the function from. However, if I use:
return $title;

I can't use that $title back in the script I called it from. There is no error (I am using strict) it just seems the string is empty.
The code I use is below:
foreach my $j (@fucntion64) { if(substr($j,0,2) eq "AU") { my($item,$title) = split(' ',substr($j,2),2); return $title."<br />"; }

If I change that return to print, the function prints the correct result back in the script. If I use return, then try and print $title from the script, I get nothing!

Replies are listed 'Best First'.
Re: Functions (Return V Print)
by jethro (Monsignor) on Feb 04, 2011 at 10:04 UTC

    The script part you show is working:

    my $n= x('AUbla bla bla bla'); print $n; sub x { my @fucntion64= @_; foreach my $j (@fucntion64) { if(substr($j,0,2) eq "AU") { my($item,$title) = split(' ',substr($j,2),2); return $title."<br />"; } } } # prints bla bla bla<br />

    Did you notice that you wrote fucntion64 instead of (I assume) function64?

      That was a typo in the post and not the code. Sorry for the confusion. When I use this syntax in my code I do get $title but it just prints one, and not foreach. If you know what I mean.
        If you want to collect all titles and use them in your main program, just store them in an array (use push for that) and return the array after(!) the foreach loop.
Re: Functions (Return V Print)
by Anonymous Monk on Feb 04, 2011 at 10:02 UTC
    If I use return, then try and print $title from the script, I get nothing!

    $title does not exist outside of the sub.

    You get warnings about this if you Use strict warnings and diagnostics or die

    And off to read perlintro/perlsub (or Tutorials) you go

    #!/usr/bin/perl -- use strict; use warnings; sub many { return @_ } my $one = many( 6 .. 12); my @all = many( 16 .. 18 ); my (undef, $second) = many( 26 .. 28 ); print " one $one all @all some $second "; __END__ one 7 all 16 17 18 some 27
      I am using Strict (like I said in the post!). But there is no error. That's what I don't understand. Is it bad practice to use "our $title" in the function so I can reuse it in the script?
        I am using Strict (like I said in the post!). But there is no error.

        ah, post a complete program :) How do I post a question effectively?

        Is it bad practice to use "our $title" in the function so I can reuse it in the script?

        Yes. Having trouble getting it to work should be convincing :) Variables and Scoping

        If you're returning title are you assigning it to a variable on it's return?
        #!/opt/perl/bin/perl use strict; use warnings; my $title="name"; print "$title\n"; $title = &function64("name"); print "$title\n"; sub function64 { my ($title) = @_; $title="$title " x 3; return ($title); }
        But there is no error.

        Strict can only warn about some bugs, not all of them. Otherwise you could trivially write perl scripts without any bugs.

        If you don't have really compelling reasons, functions should return values only through 'return'. Note that you can return more than one value in perl, different from languages like C. In C this is often the reason why other means are used to return values.

Re: Functions (Return V Print)
by cdarke (Prior) on Feb 04, 2011 at 12:27 UTC
    I'm wondering how you could get these symptoms, and a particular phrase you use "in the perl script I called the function". So, forgive me if I am wrong, but I wonder if you are doing this:
    $var = `function.pl`;
    or maybe using qx.

    The effect would be exactly the symptoms you describe. A return would do nothing, but a print to stdout would be captured.

    Can you confirm/deny?
      I can't say I fully understand but I think you mean how am I referencing the function file? I do this:
      require "functions.pm";
      Is that what you mean? Sorry, still new this all this.
        OK, you are not running the function in a different process, which would have explained your symptoms, you are loading the function from a module at run-time. We certainly need to see your code.