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

[MacPerl] Q&A Unix Perl 5.003 answer



As I said, I was trying this on the Unix machine here (Irix
5.2, Perl 5.003 with EMBED).  Anyway, I got a few
surprises.  Here they are:

Program #1:
#!/usr/local/bin/perl
#
#   5/28/96     Mark Manning    Original Program
#
    require "generic.pl";

    do 'vars.pl';
    do 'showFiles.pl';

    @array1 = ();
    @array2 = ();
    for( $i=0; $i<20; $i++ ){
        $array1[$i] = $i;
        $array2[$i] = $i;
        }

    for( $i=0; $i<20; $i++ ){
        $array1[$i] = &myFunc( $array1[$i] );
        }

    foreach( @array2 ){
        $_ = &myFunc( $_ );
        }

    for( $i=0; $i<20; $i++ ){
        print "Array1[$i] = $array1[$i], Array2[$i] = $array2[$i]\n";
        }

    exit( 0 );

sub myFunc
{
    local( $value ) = @_;

    return( $value+1 );
}

This works fine.

Program #2:
#!/usr/local/bin/perl
#
#   5/28/96     Mark Manning    Original Program
#
    require "generic.pl";

    do 'vars.pl';
    do 'showFiles.pl';

    @array1 = ();
    @array2 = ();
    for( $i=0; $i<20; $i++ ){
        $array1[$i] = $i;
        $array2[$i] = $i;
        }

    for( $i=0; $i<20; $i++ ){
        $array1[$i] = &myFunc( $array1[$i] );
        }

    foreach( @array2 ){
        &myFunc( $_ );
        }

    for( $i=0; $i<20; $i++ ){
        print "Array1[$i] = $array1[$i], Array2[$i] = $array2[$i]\n";
        }

    exit( 0 );

sub myFunc
{
    local( $value ) = @_;

    return( $value+1 );
}

This does not work (the "$_ =" was removed).  Although I
thought the "$_ =" was implied on functions which returned
a value and were not assigned an explicit variable to store
the information into.  Obviously not.

However, the shocker I had was this:

Program #3:
#!/usr/local/bin/perl
#
#   5/28/96     Mark Manning    Original Program
#
    require "generic.pl";

    do 'vars.pl';
    do 'showFiles.pl';

    @array1 = ();
    @array2 = ();
    for( $i=0; $i<20; $i++ ){
        $array1[$i] = $i;
        $array2[$i] = $i;
        }

    for( $i=0; $i<20; $i++ ){
        $array1[$i] = &myFunc( $array1[$i] );
        }

    foreach( @array2 ){
        $_ = &myFunc( $_ );
        }

    for( $i=0; $i<20; $i++ ){
        print "Array1[$i] = $array1[$i], Array2[$i] = $array2[$i]\n";
        }

    exit( 0 );

sub myFunc
{
    my $value = @_;

    return( $value+1 );
}

I thought that the "my" command was the same as the "local"
command.  Obviously not.  The "my" version just returns the
number "2" for everything.  Anyone want to comment on why
this is so?  Thanks!  :-)


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



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