CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Subroutines questions

 



abcperl
New User

Mar 27, 2010, 6:34 AM

Post #1 of 5 (885 views)
Subroutines questions Can't Post

Hi,

i'm trying to understand some code which I have to modify. I've been going through some tutorials, but can't figure out, how this function/subroutine works (sorry if I'm not using the proper perl terms yet, but i'm learning ;)

So I have this function:


Code
sub createWorkSheet($,$,$,$,@) { 
my ($excel, $db, $value,
$speed, $ascd, $session,
$time, $id, @tArray) = @_;

my @time_start = @$time;
my @ids = @$id;
...
}


which is called by the following line:

Code
my %Results = &createWorkSheet($excel, $db, $value,  
$speed, $ascd, $session,
\@time, \@id, @tArray);


Here my questions:
- The subroutine is called with 9 different arguments, but in the declaration of &createWorkSheets we have just five arguments (four scalars and an array). I don't understand why this is still working. I am aware that _@ represents the array of arguments passed to the subroutine.

- What do the backslashes in \@time and \@id are for?

- Why are the arguments \@time and \@id are passed as arrays and are transformed to the scalars $time and $id?

- And why are scalar values transformed back to arrays with this lines:

Code
 my @time_start = @$time;  my @ids = @$id;

?

Thank you for your help. Also If you can give me some good sources to read about the topics of this questions, that would be great!

123perl


rovf
Veteran

Mar 27, 2010, 7:26 AM

Post #2 of 5 (882 views)
Re: [abcperl] Subroutines questions [In reply to] Can't Post


In Reply To

Code
sub createWorkSheet($,$,$,$,@) { 
my ($excel, $db, $value,
$speed, $ascd, $session,
$time, $id, @tArray) = @_;

my @time_start = @$time;
my @ids = @$id;
...
}


which is called by the following line:

Code
my %Results = &createWorkSheet($excel, $db, $value,  
$speed, $ascd, $session,
\@time, \@id, @tArray);


Here my questions:
- The subroutine is called with 9 different arguments,


This is not correct. The prototype says that there are at least 4 arguments to be passed to the subroutine. The @ does not mean that there is a 5th argument which should be bound to an array (\@ would be the closest notation to do such a thing), but that there can be more arguments (even zero).

IMO, proto type declarations are rarely useful, and this seems to be a very bad example for a prototype.

See the sections "Prototypes" in perldoc perlsub for details.


In Reply To
I am aware that _@ represents the array of arguments passed to the subroutine.


It is @_, not _@.


In Reply To
What do the backslashes in \@time and \@id are for?


Backslash is an unary operator which creates a reference, so we have here references to the arrays @time and @id.

See perldoc perlref for details.


In Reply To
Why are the arguments \@time and \@id are passed as arrays and are transformed to the scalars $time and $id?


They are not. References are scalars.


In Reply To
And why are scalar values transformed back to arrays with this lines:

Code
 my @time_start = @$time;  my @ids = @$id;

?


This is how an array reference is dereferenced.

HTH

Ronald


FishMonger
Veteran / Moderator

Mar 27, 2010, 8:13 AM

Post #3 of 5 (879 views)
Re: [rovf] Subroutines questions [In reply to] Can't Post

Due to the way the sub is being called, the prototype is being ignored.

There are rare exceptions, but prototypes should not be used in modern scripts.

http://www.perlfoundation.org/perl5/index.cgi?prototype

http://www.perl.com/language/misc/fmproto.html


rovf
Veteran

Mar 27, 2010, 9:45 AM

Post #4 of 5 (875 views)
Re: [FishMonger] Subroutines questions [In reply to] Can't Post


In Reply To
Due to the way the sub is being called, the prototype is being ignored.


Indeed, I overlooked the &. Thank you for pointing this out.


In Reply To
There are rare exceptions, but prototypes should not be used in modern scripts.


Absolutely true, though it does not apply so much in our case, as the OP's interest was understanding an existing program, not one s/he is going to write.


abcperl
New User

Mar 28, 2010, 5:45 AM

Post #5 of 5 (867 views)
Re: [rovf] Subroutines questions [In reply to] Can't Post

Thanks a lot!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives