Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

subroutine call 1

Status
Not open for further replies.

rjtayl27

Programmer
May 11, 2004
2
0
0
US
$copy(\@x,\@y,$size);
I am having trouble copying the contents of @x into @y
 
1.) sub-routines don't use the dollar sign, it's the ampersand: &
2.) copy isn't a standard function.

if you want to copy an array in PERL

@x = @y;

---------------------------------------
If you helped... thx.
 
The ampersand is rarely needed on subroutine calls in modern Perl. It was required in Perl 4. From perlsub:
To call subroutines:

NAME(LIST); # & is optional with parentheses.
NAME LIST; # Parentheses optional if predeclared/imported.
&NAME(LIST); # Circumvent prototypes.
&NAME; # Makes current @_ visible to called subroutine.
Check out perldoc perlsub for the full lowdown.
 
it's required if you have strict enabled though.

---------------------------------------
If you helped... thx.
 
Nope. I use strict all the time, rarely put & on a subroutine call. Can you point me to something that says I should?
 
All four syntaxes listed by Mike are valid with or without strict enabled.

What is NOT allowed with strict enabled is calling a sub with NEITHER & nor ():
Code:
use strict;
go;
exit;

sub go {
  print "wibble!\n";
}
In this example Perl will complain bitterly:
Code:
Bareword "go" not allowed while "strict subs" in use at x line 3.
Execution of x aborted due to compilation errors.
Perl complains because using a bareword function call is ambiguous - Perl has to guess whether "go" is supposed to be a function call, a scalar, etc. Strict doesn't allow such guessing.

--G
(who has nothing else to do on a Friday night - pitiful, just pitiful)

 
If the sub body or just a declaration occurs before the call, it's still okay, even without & or (). Both of the following work.
Code:
#!perl
use strict;
use warnings;

sub go {
  print "wibble!\n";
}

go;
exit(0);

Code:
#!perl
use strict;
use warnings;

sub go;

go;
exit(0);

sub go {
  print "wibble!\n";
}

 
thanks for the star!!

---------------------------------------
If you helped... thx.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top