[comp.lang.perl] shift within eval gives null?

chrise@hpnmdla.HP.COM (Chris Eich) (08/15/90)

I'm using perl 3.0pl18.  Consider the following:

------------------------------------------------------------------------
# List of arrays containing values that occur once per file.

@PerFile = ('FileName', 'FileSize', 'DateWritten', 'RetryDensity',
	    'DataHost', 'TapeHost', 'WriteCommand');

sub main'AddToToc {	# &AddToToc(<values for @PerFile arrays, in same order>);
    local($[) = 1;		# So we can index arrays by $FileNumber.

    # Update the per-file values with the subroutine arguments.
    $FileNumber++;
    foreach (@PerFile) {
	eval '$' . $_ . "[$FileNumber] = shift";
    }
}
------------------------------------------------------------------------

When I call AddToToc with seven values, I'd like the first element of
each of the per-file arrays to be set to the respective value.  It
doesn't happen.  The elements are set to null.

Doesn't shift (of the subroutine arguments @_) work within an eval?

Chris

chrise@hpnmdla.HP.COM (Chris Eich) (08/16/90)

In comp.lang.perl, chrise@hpnmdla.HP.COM (Chris Eich) writes:

    sub main'AddToToc {	# &AddToToc(<values for @PerFile arrays, in same order>);
	...
	eval '$' . $_ . "[$FileNumber] = shift";
        ...
    }

    Doesn't shift (of the subroutine arguments @_) work within an eval?

After more experimenting, I've found that the eval:

1.  Runs in the "main" package, so that I have to add "toc'" to my idents.

2.  Runs in main-program context (so that the shift above is shifting @ARGV).

Here's a line that works:

	eval '$toc\'' . $_ . "[$FileNumber] = shift(\@_)";

(Why is @_ available if inference (2) is true?)

TFM says that eval "is executed in the context of the current perl
program", which led me to write the simpler code above.

I've read the description of patches 19-27, and see no change in eval's
behavior.  But I'd like the former, simpler code to work.  Is there a
good reason not to fix it?

Chris

chrise@hpnmdla.HP.COM (Chris Eich) (08/16/90)

In comp.lang.perl, chrise@hpnmdla.HP.COM (Chris Eich) writes:

    After more experimenting, I've found that the eval:

    1.  Runs in the "main" package, so that I have to add "toc'" to my idents.

    2.  Runs in main-program context (so that the shift above is shifting @ARGV).

Forget (1).  My code had other errors that were causing me to believe it.

It still seems strange a subroutine can shift away @ARGV just by using
shift in an eval.

Just another perl plodder,

Chris