[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Any way to Tee in MacPerl?



According to Chris Nandor:
> 
> At 14.16 1998.02.13, Mark Manning/Muniz Eng. wrote:
> >According to Chris Nandor:
> >>
> >> This is not really a MacPerl issue, but I need to correct some
> >> misinformation here.  local() variables are global.  my() variables are
> >> lexical (local, not local()).  A local() variable simply saves the value of
> >> a global variable, and restores that value to that variable when the
> >> dynamic scope of the local() variable exits.
> >>
> >

> I said nothing inaccurate or wrong.  There is no such thing as "global to a
> routine."  Global is global.  The wording there is bad, and old.  It looks
> like perl 4; the perl 5 docs reference my() and point to perlsub for a more
> in-depth explanation.  A local() is global in every sense of the word.

Ok, then can you run this program?

#!/usr/local/bin/perl

        $myVar = "Main Routine";

        print "Main : myVar = $myVar\n";
        &mySub();
        print "Main : myVar = $myVar\n";

sub mySub
{
        local ($myVar) = "First Subroutine!\n";
        print "mySub : myVar = $myVar\n";
        &mySub2();
        print "mySub : myVar = $myVar\n";
}

sub mySub2
{
        local ($myVar) = "Second Subroutine!\n";
        print "mySub2 : myVar = $myVar\n";
        &mySub3();
        print "mySub2 : myVar = $myVar\n";
}

sub mySub3
{
        print "mySub3 : myVar = $myVar\n";
}

And just for the record:

muldar[22]{/tmp_mnt/disk4/mark/perl}(14:07) >/usr/local/bin/perl -v

This is perl, version 5.004_04 built for IP22-irix

Copyright 1987-1997, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5.0 source kit.

		.		.		.

In case you want to say I'm running perl 4.  The output for this program is:

Main : myVar = Main Routine
mySub : myVar = First Subroutine!

mySub2 : myVar = Second Subroutine!

mySub3 : myVar = Second Subroutine!

mySub2 : myVar = Second Subroutine!

mySub : myVar = First Subroutine!

Main : myVar = Main Routine

Which, as you can see, myVar's original value is kept for
the main program.  The second version of myVar (ie:
Subroutine #1) changes the value of myVar to "First
Subroutine!".  The second subroutine changes myVar to
"Second Subroutine!".  The third subroutine only prints the
value of myVar.  Which shows it is global to the third
subroutine.  Then, as everything pops back out the value of
myVar is reloaded from the hidden stack.

You have said it yourself.  "A local modifies the listed
variables to be local to the enclosing block"  A block is
defined as everything contained within a given subroutine
or "{}" area.  Thus, the LOCAL command defines a local (as
per your last posting).  But the statement "local()
variables are global" was incorrect.

You are also correct in that the MY statement is a better
alternative.  As I said in my last post.  The rest of your
first post was correct.  I just felt that saying a variable
defined as LOCAL was in fact GLOBAL was misleading to the
original poster and possibly others.

Further, if there are no subprograms beneath the called
subroutine (ie: As in my example with a subroutine being
called from inside of another subroutine), the LOCAL
statement really does cause a variable to be local with
regards to the rest of the program.

To move on - I have a question:  Is it possible to call the
main program from a subroutine thus making the main program
a subroutine?  I only ask out of curiosity's sake and
nothing more.


***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch