[VPM] Perl Bug?

Peter Scott Peter at PSDT.com
Wed Jun 16 15:57:54 CDT 2004


At 12:31 PM 6/16/2004, Darren Duncan wrote:
>At 12:12 PM -0700 6/16/04, Jeremy Stashewsky wrote:
>Can any of you comment on why this perl statement doesn't or shouldn't work?
>Seems to happen under perl 5.8.4, but not under 5.8.0.
>(Code is a snippet from Cricket: cricket.sf.net)
>$ perl
>my(@oidsToQuery) =  my(@indices) = ();
>__END__
>Bizarre copy of ARRAY in aassign at - line 1.
>$
>Thanks,
>~jeremy
>
>Well, that brings up a question for me.  Namely, can 'my' be used as 
>an rvalue, or only as an lvalue?  I've only ever seen it used as an 
>lvalue. -- Darren Duncan

I don't see why not.  'my' is basically a modifier on the use of a 
variable.  I do this all the time:

         getopts('dvit', \my %Opt);

However, the usage quoted is not, I think, an lvalue.  Observe:

% perl -MO=Deparse -e 'my(@oidsToQuery) =  my(@indices) = ()'
my(@oidsToQuery) = (my(@indices) = ());


First the empty list assignment is done to my @indices.  Then the 
result of that assignment is used in a list context to assign to 
@oidsToQuery.  I always remember it as "the result of an assignment is 
the *value* assigned."  Experiment time:

use strict;
use warnings;

tie my $x, 'main';
my $y = ($x = 42);
print "\$y = $y\n";

sub TIESCALAR { return bless \my $new, shift }
sub STORE { ${$_[0]} = $_[1]; print "STORE $_[1]\n" }
sub FETCH { print "FETCH ${$_[0]}\n"; ${$_[0]} }

Result:

STORE 42
FETCH 42
$y = 42

Okay, I was wrong... it does fetch the variable.  So a my modifier can 
be used in an rvalue context (that example doesn't prove it, but you 
can do your own experiments to see that the example that Jeremy was 
trying will work, usually).

Incidentally, the code Jeremy was trying is unnecessary; arrays start 
out empty, so

         my (@oidsToQuery, at indices)

will do just fine.

-- 
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/
*** New! *** http://www.perlmedic.com/




More information about the Victoria-pm mailing list