I'm using perl, v5.8.1-RC3 built for darwin-thread-multi-2level, and sometimes the variables do get clobbered (rather than assigned). The explanation you're looking for is that
each array variable is assigned the value ${@_}. BEGIN is called with no arguments, so @_ is empty. ${@_} puts @_ in scalar context so @_ evaluates to the number of its elements, which is 0.

So each array variable is being assigned the value of $0 which is the name of the source file. The while loop reads each line of the source file and sets @_ to the array consisting of that line. At the end of the loop @_ contains only the last line of the file, which evals to 1 and prints the JAPH message as a side effect. The second half of the || should never be executed.

The problem with this explanation is that the order of the variables in %:: is not fixed. Once you set @_ to $0 it is no longer empty, so any subsequent assignments are taken from $1 which is undefined. I've extended your code to print the assignments it's making as they happen:
LINE: while ( defined( $_ = <ARGV> ) ) { @_ = $_; } sub BEGIN { foreach $_ (%main::) { print "\@{$_}='${@_}'\n"; eval '@{$_}=${@_}'; } } { print "Just another perl hacker.\n" unless eval "@_"; }
I realize this messes up your carefully-designed layout, but eval "}\n" seems to evaluate to a false value, so it still prints the JAPH message. Here's partial output from one less-successful run:
@{STDIN}='dedalus.pl.tdy' @{*main::STDIN}='dedalus.pl.tdy' ... @{_}='dedalus.pl.tdy' @{*main::_}='' ... @{ARGV}='' @{*main::ARGV}='' ... Can't open : No such file or directory at dedalus.pl.tdy line 1. Just another perl hacker.
The following modification of your code seems to work reliably here:
#! /usr/bin/perl -n @_=$_}BEGIN{eval'@{$_}=${@_}'for sort keys%::}{eval"@_"|| print "Just another perl hacker.\n"

In reply to Re^3: A simple JAPH by jdalbec
in thread A simple JAPH by Dedalus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.