[kw-pm] is perl 6 really that different?
Simon Law
sfllaw at engmail.uwaterloo.ca
Tue Jul 8 19:15:14 CDT 2003
On Tue, Jul 08, 2003 at 06:58:39PM -0400, Robert P. J. Day wrote:
> On Tue, 8 Jul 2003, Andrew Kohlsmith wrote:
>
> > > > I don't have the book, but that's one of perl6 changes- arrays and
> > > > hashes retain their sigil when they're sliced/diced.
> >
> > > holy crap. so perl 6 is not even remotely backward compatible?
> > > apparently, i have some serious reading to do. thanks.
> >
> > You can do that today, you just get a warning:
> >
> > $ perl -wTe '@ary = ("Zaphod", "Ford", "Trillian"); $second_element =
> > @ary[1];'
> > Scalar value @ary[1] better written as $ary[1] at -e line 1.
> > Name "main::second_element" used only once: possible typo at -e line 1.
>
> oh, i knew that. but there were definite gotchas with confusing those
> two syntaxes if you were, say, assigning from an open filehandle.
> consider the massive difference between these two assignments:
>
> $arr[0] = FH ;
> @arr[0] = FH ;
>
> as i recall, the first would simply read the next line (since this
> was in a scalar context).
>
> the second, however, would (because of the array context) read the
> entire remainder of the file and throw away everything except for
> the one line it assigned to the LHS.
The second does not seem semantically correct for Perl 5.
Especially if you've already declared
my @arr;
earlier in the program. (Otherwise, the first line would not make
sense.)
> this suggests that there *will* be major incompatibility problems
> since using the "@" sigil will be the correct way to do this in
> Perl 6.
Well, that's because Perl 6 is throwing away most of the cruft
in Perl. For instance, filehandles are no longer barewords but are
properly scalars. So instead of saying:
open(FH, '<myfile') or die;
while (<FH>) {
# do stuff
}
you'd say:
my $FH = open('<myfile') or die;
while (<$FH>) {
# do stuff
}
but that's not quite idiomatic. What you really would say is:
while (<open('<myfile') or die>) {
# do stuff
}
Simon
More information about the kw-pm
mailing list