develooper Front page | perl.perl5.porters | Postings from November 2010

Re: [PATCH] Add "safe arrow/safe dereference" operator: &&->

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
November 13, 2010 05:12
Subject:
Re: [PATCH] Add "safe arrow/safe dereference" operator: &&->
Message ID:
20101113130734.GA7074@klangraum.plasmasturm.org
* demerphq <demerphq@gmail.com> [2010-11-13 13:30]:
> I dont understand really why this needs new syntax. Why cant it
> be done with a pragma.
>
> no autovivification 'lookup';
>
> something like that...

Consider this not very hypothetical example:

    my $stash = $c->stash;
    if (
            $stash->{'foo'}
        and $stash->{'foo'}{'bar'}
        and $stash->{'foo'}{'bar'}{'baz'}
    ) {
        push @{ $stash->{'quux'}{ $_->{'qux'} }{ $_->{'id'} } }, $_
            for @$records;
    }

How would one rewrite this with a pragma? Like this?

    my $stash = $c->stash;
    if ( do {
        no autovivification 'lookup';
        $stash->{'foo'}{'bar'}{'baz'};
    } ) {
        push @{ $stash->{'quux'}{$qux}{$id} }, $record;
    }

Or this?

    {
        no autovivification 'lookup';
        my $stash = $c->stash;
        if ( $stash->{'foo'}{'bar'}{'baz'} ) {
            use autovivification 'lookup';
            push @{ $stash->{'quux'}{$qux}{$id} }, $record;
        }
    }

I will prefer the original code over any of the pragma-adorned
versions.

It’s not even the worst example. It would get impossibly messy if
you wanted to protect only some parts of an expression but not
others.

And several of my code bases contain variations on the following
theme, just because of how clumsy it is to inject pragmata:

    sub coerce_num {
        no warnings qw( numeric uninitialized );
        ( map { 0+$_ } @_ )[ 0 .. $#_ ];
    }

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About