no autovivication for subs 
Author Message
 no autovivication for subs

Why don't the return values of perl subs autovivify?

  % perl -wle ' sub f {$_} print f() -> [0] '
  Can't use an undefined value as an ARRAY reference at -e line 1.

It doesn't even work for lvalued subs:

  % perl -wle ' sub f:lvalue {$_} print f() -> [0] '
  Can't use an undefined value as an ARRAY reference at -e line 1.

Can this feature be exploited in perl6 to control autovivication?

--
Joe Schaefer



Sat, 15 May 2004 01:07:11 GMT  
 no autovivication for subs
Joe Schaefer wrote in comp.lang.perl.misc:

Quote:
} Why don't the return values of perl subs autovivify?
}
}   % perl -wle ' sub f {$_} print f() -> [0] '
}   Can't use an undefined value as an ARRAY reference at -e line 1.

This behavior is correct : you're basically doing "undef->[0]".

Quote:
} It doesn't even work for lvalued subs:
}
}   % perl -wle ' sub f:lvalue {$_} print f() -> [0] '
}   Can't use an undefined value as an ARRAY reference at -e line 1.

Hm, this looks like a bug. As $_ is undefined, it should spring into
existence, as documented in perlref. Can you report it via perlbug?

Quote:
} Can this feature be exploited in perl6 to control autovivication?

Perhaps it's not too late for perl 5.

--
Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/
perl -sleprint -- -_='Just another Perl hacker,'



Sat, 15 May 2004 11:16:50 GMT  
 no autovivication for subs

Quote:

>...
>}
>}   % perl -wle ' sub f:lvalue {$_} print f() -> [0] '
>}   Can't use an undefined value as an ARRAY reference at -e line 1.

>Hm, this looks like a bug. As $_ is undefined, it should spring into
>existence, as documented in perlref. Can you report it via perlbug?

I thought autovivifying worked only for a hash or array element...
as in perlref's example:  

   $array[$x]->{"foo"}->[0] = "January";

That is, I don't see any doc instances in this context of an
unsubscripted lvalue which could autovivify - unless there's
an actual array or hash ref. The base lvalue ref to the left
of the -> as in  $array->{foo}->{"foo"} will generate an error
unless $array is a valid ref.  

Or have I missed something...

--
Charles DeRykus



Sat, 15 May 2004 22:51:31 GMT  
 no autovivication for subs
Charles DeRykus wrote in comp.lang.perl.misc:

Quote:

} >...
} >}
} >}   % perl -wle ' sub f:lvalue {$_} print f() -> [0] '
} >}   Can't use an undefined value as an ARRAY reference at -e line 1.
} >
} >Hm, this looks like a bug. As $_ is undefined, it should spring into
} >existence, as documented in perlref. Can you report it via perlbug?
}
} I thought autovivifying worked only for a hash or array element...
} as in perlref's example:  
}
}    $array[$x]->{"foo"}->[0] = "January";
}
} That is, I don't see any doc instances in this context of an
} unsubscripted lvalue which could autovivify - unless there's
} an actual array or hash ref. The base lvalue ref to the left
} of the -> as in  $array->{foo}->{"foo"} will generate an error
} unless $array is a valid ref.  

The following works :


    <1>

Obviously $x becomes an array ref here. So why should this not
work for lvalue subs ?

--
Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/



Sun, 16 May 2004 08:54:21 GMT  
 no autovivication for subs

Quote:

>Charles DeRykus wrote in comp.lang.perl.misc:
>...
>The following works :


>    <1>

>Obviously $x becomes an array ref here. So why should this not
>work for lvalue subs ?

Indeed. I was confused. Since the left side of the
arrow must be a location capable of holding a hard
reference, the lvalue sub evidently just isn't that
versatile. Something like below works but this is
hardly useful:

   print +(x()="foo")->[0];
   print +(x()=[1,2])->[0];

--
Charles DeRykus



Sun, 16 May 2004 21:19:39 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Autovivication and pure scalar

2. autovivication paradox causes rip in space-time

3. differance between &sub and &'sub

4. Bug in Net::FTP sub _extract_path (or Net::Cmd sub parse_response)

5. Outer scope of a sub inside a sub

6. get name of previous sub that called the actual sub

7. Differences between anonymous subs and named subs

8. Finding sub name from sub pointer

9. getting the name of a sub from a sub ref

10. Rank HoH by sub-sub-key?

11. Error : calling sub routine inside another sub routine

12. Sub to call sub and wait and retry on failure

 

 
Powered by phpBB® Forum Software