Sorry, I completely misread your original code. I see what the return is doing now. It's inside another sub. I just didn't see the sub because of your indenting. While it's true that whitespace (mostly) doesn't matter to the computer, the fact is that code is not written for the computer as much as it is written for the next human that has to read it (usually yourself). You should check out perlstyle for some ideas on how to improve the layout of your code. For example, your IP_check sub could look better this way:

sub IP_check { my $_info2 = $ENV{'REMOTE_ADDR'}; my $info; if (!$_info2) { $info2 = "No IP address available"; return $info2; } elsif ($_info2 !~ /[\%]+/ && $_info2 =~ /^(\d{1,3}\.{1})(\d{1,3}\.{1})(\d{1,3}\.{1})(\ +d{1,3})$/) { my $info2 = "$1$2$3$4"; return $info2; } else { my $info2 = "Bad IP address submitted"; return $info2; } }
Notice how I did nothing but add whitespace. It's much easier to follow now. Now that I can read it properly, the return is perfectly legal. It may not be completely optimal, but it's legal.

Instead, if I can just focus on this sub for a minute, you may want to use the $info you declared rather than the global $info2 you are using. That helps keep the subroutine from having side effects - side effects aren't necessarily bad, but when you can avoid them, do so, as it will make your code easier for the human to follow.

Or, to make the subroutine just a bit tighter, you don't need that at all. For example:

sub IP_check { my $_info2 = $ENV{'REMOTE_ADDR'}; if (!$_info2) { return "No IP address available"; } elsif ($_info2 !~ /[\%]+/ && $_info2 =~ /^(\d{1,3}\.{1})(\d{1,3}\.{1})(\d{1,3}\.{1})(\ +d{1,3})$/) { return "$1$2$3$4"; } else { return "Bad IP address submitted"; } }
Now, some people get mad at having multiple returns in a single subroutine, arguing that it's easier to follow when there's a single entry and a single exit to each function. I'm not a complete believer in this style, but I'm not going to say it's wrong, either. So here's that example:
sub IP_check { my $_info2 = $ENV{'REMOTE_ADDR'}; my $ip; if (!$_info2) { $ip = "No IP address available"; } elsif ($_info2 !~ /[\%]+/ && $_info2 =~ /^(\d{1,3}\.{1})(\d{1,3}\.{1})(\d{1,3}\.{1})(\ +d{1,3})$/) { $ip = "$1$2$3$4"; } else { $ip = "Bad IP address submitted"; } return $ip; }
I'll leave it up to you to decide which you think is more readable and easier to understand. Then there's the more perlish way to do all of the above - but it's a wee bit tricky, I'd suggest one of the above over this next example. This next one only works because the if statement is the last statement in the subroutine since, in perl, the return value from a sub is determined from the last value in the subroutine if no 'return' is otherwise encountered. (It's because it requires a long explanation that I don't recommend it ;-})
sub IP_check { my $_info2 = $ENV{'REMOTE_ADDR'}; my $ip; if (!$_info2) { "No IP address available"; } elsif ($_info2 !~ /[\%]+/ && $_info2 =~ /^(\d{1,3}\.{1})(\d{1,3}\.{1})(\d{1,3}\.{1})(\ +d{1,3})$/) { "$1$2$3$4"; } else { "Bad IP address submitted"; } }
Now the next question is ... how to use it? That's easy - regardless of which one of the above you use, you call it like this: my $info2 = IP_check(); Hope that helps!


In reply to Re^3: passing variables between subroutines by Tanktalus
in thread passing variables between subroutines by budreaux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.