Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

subroutine syntax

by Anonymous Monk
on Dec 10, 2000 at 13:22 UTC ( [id://45930]=perlquestion: print w/replies, xml ) Need Help??

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

I am new to perl and I can't seem to find an answer for this nagging question.

What is the different between putting arguments when calling subroutines vs a blank subroutine call?

&example($1,$2,etc..); vs &example;

Why do you need to pass arguments when calling a subroutine when you can call/use them inside the subroutine without passing them first.

ex:

$xyz = 15;
&example;
sub example {
$a = $xyz + 49;
print $xyz;
print $a;}

what am I not getting, not understanding about the syntax? Thank you for any response.

Replies are listed 'Best First'.
Re: subroutine syntax
by marius (Hermit) on Dec 10, 2000 at 13:36 UTC
    The thing you are missing is do you really need to use $xyz outside of the scope of the subroutine. I would read up on variable scope in the Llama book and/or the Camel book. Basically, you can substitute the following:
    &example(15); sub example { my($xyz) = shift || 0; my($a) = $xyz + 49; print $xyz; print $a }
    After doing this, $a and $xyz won't be available to you outside of the subroutine. This is all assuming you're operating under strict. Really, you ought to read the Llama book if nothing else, it's fantastic information and explains a lot of this much more clearly than I can. =]

    -marius
Re: subroutine syntax
by epoptai (Curate) on Dec 10, 2000 at 13:40 UTC
    What if you want to use sub example not only for $xyz but also $abc, $etc, etc? When i first started perl i would just add another sub with $abc in place of $xyz and things got sloppy very quickly. Then i learned how to pass variables and write much more generalized subroutines.

    Like this:

    $xyz = 15; $abc = 30; &example($xyz); &example($abc); sub example { local $var = @_; $a = $var + 49; print $var; print $a; }
    See how elegant that is? Whatever variable you pass to example gets assigned to $var and operated upon.

    enjoy - epoptai

      For that matter, let's take a look at uses of the return function. (perldoc -f return)
      my $xyz = 15; my $var = &example($xyz); print &example($xyz); sub example { my $tmp = shift || 0; my $tmp += 49; return $tmp }
      Now, you can set the global $var to by equal to the return value of the subroutine. Also, you can just print the return of the subroutine if you like. return is a very handy tool when it comes to subroutine programming and cleaning up perl code.

      -marius
Re: subroutine syntax
by snax (Hermit) on Dec 10, 2000 at 15:24 UTC
    Above suggestions will help get you off the ground, but you've got perl's own documentation available, too. Use
    perldoc perlsub
    at your command line or plunk "perlsub" in the search box at the upper left of this page.

    Use the docs, Luke!

Re: subroutine syntax
by Anonymous Monk on Dec 11, 2000 at 00:14 UTC
    Thanks for the responses.
    So, As I understand it, I don't have to pass a $variable into a subroutine call before i use the $variable inside the sub?
    Also, does the $variable get modified globally if it is called directly inside the subroutines and modified?

    I am using the learn-perl-quick books to try to put together some cgi's.
    Thanks again for the responses.

      So, As I understand it, I don't have to pass a $variable into a subroutine call before i use the $variable inside the sub?

      No, you don't have to pass the variable as an argument to the subroutine, but you will write clearer code if you do. By passing values, you know which values will affect the outcome of the subroutine without having to look through the code for the sub.

      Also, does the $variable get modified globally if it is called directly inside the subroutines and modified?

      Yes, anywhere that you modify a variable, you will modify the variable (with the exception of localised variables, where it gets a bit more complicated). You can still modify the variable sent to a sub, as @_ becomes an alias to the variables themselves so that:

      $dog = 'spot';

      Is exactly the same as:

      sub dog_spot {$_[0] = 'spot'}; dog_spot($dog);

      You can make a copy of the variable passed to the subroutine, so that changes do not propagate outside the sub so that code like this is safe:

      sub foo {$_[0] =~ s/foo/bar/; print $_[0]} # bad sub bar {my $temp = $_[0]; $temp =~ s/foo/bar/; print $temp} # better

      Thereason why you want to pass variables to subroutines as arguments, rather than by using global variables is that if you have a bug in your code, then using global variables means that the bug could be caused by any of the many lines in your program. If you pass variables as arguments, then the bug could only be caused by the lines which call the subroutine, or the subroutine itself.

      Perl has, like many other languages global variables. So you don't *need* to pass a variable to a subroutine in order for the subroutine to "see" that variable. However, in subroutines, relying on globals to be set, and modifying them is a recipe for disaster. It may be OK in small scripts, but when your scripts grow you'll curse the decision to use globals more often than not (see tilly's post RE (tilly) 3: redeclaring variables with 'my' for a very good explanation of why "going global" without good reason is a bad idea). The general rule is: you pass variables to your subroutines, and then you localize them using my, as other monks in this thread have shown you (snax's suggestion for other reading material is where you should start). I'm not going to reproduce the perl documentation -- read up on the my operator and please don't confuse it with the local operator =)

Re: subroutine syntax
by Anonymous Monk on Dec 11, 2000 at 05:56 UTC
    Thank you all for clarifying my questions. As I stated, I am trying to piece together a simple script for cgi. I don't have the aspiration or 'time' to learn the intricate details of the language. This website is great to use as a reference point, and the monks here are very kind.
      I think this sentiment ties in with the comments in The three virtues of Perl are CONFLICTING!, particularly with regards to false laziness.

      By investing the time now to actually learn Perl, you will be so much more productive in the future. Something to consider.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-03-28 15:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found