I believe when you have

sub marine{
    my $n
}

my is local to sub marine, so it is lost after each call of marine.  Whereas
in the second example you have my $n outside of sub marine.

matt


Ron Smith wrote:

> Is it just me, or does anyone out there notice that the example subroutine
> on pg. 57 of "Learning Perl" (Third Edition) doesn't work, as presented?
>
> When written like:
> ----------------snip------------------
> #!/usr/bin/perl -w
> use strict;
>
> sub marine {
>     my $n += 1; # Global variable $n
>     print "Hello, sailor number $n!\n";
> }
>
> &marine;
> &marine;
> &marine;
> &marine;
> ----------------snip------------------
>
> STDOUT gets:
>
> Hello, sailor number 1!
> Hello, sailor number 1!
> Hello, sailor number 1!
> Hello, sailor number 1!
>
> But, when written like:
> ----------------snip------------------
> #!/usr/bin/perl -w
> use strict;
>
> my $n = 1;
> sub marine {
>     print "Hello, sailor number $n!\n";
>     $n += 1; # Global variable $n
> }
>
> &marine;
> &marine;
> &marine;
> &marine;
> ----------------snip------------------
>
> STDOUT gets:
>
> Hello, sailor number 1!
> Hello, sailor number 2!
> Hello, sailor number 3!
> Hello, sailor number 4!
>
> ...like it's supposed to. Am I missing some key information here?? Or, is
> the book just a bit unclear??
>
> Ron
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to