Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: subroutine syntax

by Anonymous Monk
on Dec 11, 2000 at 00:14 UTC ( [id://45971]=note: print w/replies, xml ) Need Help??


in reply to subroutine syntax

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.

Replies are listed 'Best First'.
Re: Re: subroutine syntax
by quidity (Pilgrim) on Dec 11, 2000 at 00:40 UTC

    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.

Re: Re: subroutine syntax
by arturo (Vicar) on Dec 11, 2000 at 00:45 UTC

    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 =)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 18:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found