in reply to Functions (Return V Print)

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

Replies are listed 'Best First'.
Re^2: Functions (Return V Print)
by packetstormer (Monk) on Feb 04, 2011 at 10:35 UTC
    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); }

        Ignore me, you are, just re-read your original post.

        Errr, ignore my ignore, you've not shown enough code. Could you show us where you're calling the function?

        Updated: 20110204 11:53 GMT

      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.