[tpm] dereferencing anonymous hashes

Rob Janes janes.rob at gmail.com
Fri Apr 13 13:44:12 PDT 2007


here's more of my 2 cents (have fun Uri! :)

I notice there's two examples of for, but no examples of multiple lexical
(my) variable assignments in for.

for ($i = 1, $j = 6; $i < 10; $i++, $j++) {

and

for (my $i = 1; $i < 10; $i++) {

Normally multiple lexical variables are declared in the same line like

my ($i, $j);

But if you do that you can't initialize them.  my ($i=1, $j=2); gives an
error.  this works ...

my $i=1;
my $j=2;

this is the common way.  but ...

my ($i, $j) = (1,2);

is another way.

here's another way, in a for statement:

for (my $i=1, my $j=6; $i < 10; $i++, $j++) { print "$i\n" }

and so,

my $i=1, my $j=2;

works too.

even more thrilling is

my $k = (my $i=1, my $j=2);

which gives $k a 2.



On 4/13/07, Liam R E Quin <liam at holoweb.net> wrote:
>
> On Thu, 2007-04-12 at 11:21 -0400, Uri Guttman wrote:
>
> > useful? that is what it is designed for. it was taken from c which has
> > the same operator but no list context to confuse things.
>
> The , in C that's used in parameter lists (for example), as in
>     int m = max(3, 12, 9, 76);
> is like the comma in a Perl list,
>     my $m = max(3, 12, 9, 76);
> which is not at all like
>     my @mm = max(3, 12, 9, 76);
> :-)
>
> Similarly, the C comma operator, really only used in a few
> places these days because it's dangerous otherwise, is the same
> as semicolon except that it doesn't terminate the statement, so
>
>     if (e) a = 3,
>     b = 6,
>     c = 12;
>
> is the same as
>
>     if (e) {
>         a = 3;
>         b = 6;
>         c = 12;
>     }
>
> except harder to follow.  In Perl, you have to use the braces, so the
> second form is enforced, but you can still do things like
>
> for ($i = 1, $j = 6; $i < 10; $i++, $j++) {
>     stuff
> }
>
> and that's also the main place the comma operator is used in C.
>
> I'd prefer in most cases
> for (my $i = 1; $i < 10; $i++) {
>     my $j = $i + 5;
>     stuff;
> }
>
> because the relationship between in and j is now made explicit.
>
> Liam
>
> --
> Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
> Pictures from old books: http://fromoldbooks.org/
> Ankh: irc.sorcery.net irc.gnome.org www.advogato.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/toronto-pm/attachments/20070413/85406bdd/attachment.html 


More information about the toronto-pm mailing list