For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Bad scoping? Bad prototyping?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Bad scoping? Bad prototyping?
Kim Helliwell

2006-10-12, 6:59 pm

The following test script fails to compile, complaining that there are
not enough arguments

in the call to sub2.



#!/bin/perl



sub1("Hello, ");

sub1("world\n");



sub sub2($str)

{

print $str;

}

sub sub1($str)

{

sub2($str)

}





The basic problem is that I'm trying to call one subroutine from inside
another. I'm sure I've had

this work before, but there's clearly something I'm doing wrong this
time.



Any clues?



Thanks,







Kim Helliwell

LSI Logic Corporation

Work: 408 433 8475

Cell: 408 832 5365

kim.helliwell@lsi.com



Please Note: My email address will change to kim.helliwell@lsi.com
<mailto:kim.helliwell@lsi.com> on Oct 14. The old 'lsil.com' email
address will stop working after Jan 15, 2007. Please update your address
book and distribution lists accordingly. Thank you.




Andy Greenwood

2006-10-12, 6:59 pm

On 10/9/06, Helliwell, Kim <Kim.Helliwell@lsil.com> wrote:
> The following test script fails to compile, complaining that there are
> not enough arguments
>
> in the call to sub2.
>
>
>
> #!/bin/perl
>
>
>
> sub1("Hello, ");
>
> sub1("world\n");


Are you sure you want sub1 prototyped twice?
>
>
>
> sub sub2($str)
>
> {
>
> print $str;
>
> }
>
> sub sub1($str)
>
> {
>
> sub2($str)
>
> }
>
>
>
>
>
> The basic problem is that I'm trying to call one subroutine from inside
> another. I'm sure I've had
>
> this work before, but there's clearly something I'm doing wrong this
> time.
>
>
>
> Any clues?
>
>
>
> Thanks,
>
>
>
>
>
>
>
> Kim Helliwell
>
> LSI Logic Corporation
>
> Work: 408 433 8475
>
> Cell: 408 832 5365
>
> kim.helliwell@lsi.com
>
>
>
> Please Note: My email address will change to kim.helliwell@lsi.com
> <mailto:kim.helliwell@lsi.com> on Oct 14. The old 'lsil.com' email
> address will stop working after Jan 15, 2007. Please update your address
> book and distribution lists accordingly. Thank you.
>
>
>
>
>



--
I'm nerdy in the extreme and whiter than sour cream
John W. Krahn

2006-10-12, 6:59 pm

Helliwell, Kim wrote:
> The following test script fails to compile, complaining that there are
> not enough arguments in the call to sub2.
>
> #!/bin/perl
>
> sub1("Hello, ");
> sub1("world\n");
>
> sub sub2($str)
> {
> print $str;
> }
>
> sub sub1($str)
> {
> sub2($str)
> }
>
> The basic problem is that I'm trying to call one subroutine from inside
> another. I'm sure I've had this work before, but there's clearly
> something I'm doing wrong this time.


It looks like you are trying to write a C program using Perl.

perldoc perlsub


You want something like:

#!/bin/perl
use warnings;
use strict;

sub1("Hello, ");
sub1("world\n");

sub sub2
{
my $str = shift;
print $str;
}

sub sub1
{
my $str = shift;
sub2($str)
}



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
John W. Krahn

2006-10-12, 6:59 pm

Andy Greenwood wrote:
> On 10/9/06, Helliwell, Kim <Kim.Helliwell@lsil.com> wrote:
>
> Are you sure you want sub1 prototyped twice?


Those are function *calls* not prototypes.

perldoc perlsub




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Aaron Priven

2006-10-12, 6:59 pm

On Oct 9, 2006, at 10:35 AM, Helliwell, Kim wrote:

> #!/bin/perl
>
> sub1("Hello, ");
>
> sub1("world\n");
>
>
> sub sub2($str)
>
> {
> print $str;
> }
>
> sub sub1($str)
> {
> sub2($str)
> }
>


Prototyping in perl does not do what you think it does. It does not
turn your arguments into variables. All it does is supply a scalar or
list context to entries in your argument list. It is not a useful
feature for most people most of the time, and should be avoided.

http://library.n0i.net/programming/.../fm_prototypes/

Arguments to subs are always in the array @_

#!/bin/perl
use warnings;
use strict;
# no Top::Posting;

sub1('Hello, ');
sub1("world\n");

sub sub2 {
my $str = shift; # implicitly uses @_
print $str;
}

sub sub1 {
my $str = shift;
sub2($str)
}
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com