belg4mit has asked for the wisdom of the Perl Monks concerning the following question:
Gives:#Minimal package A; use Want; sub sub :lvalue{ if( want('LVALUE', 'ASSIGN') ){ print want('ASSIGN'), "\n"; lnoreturn; } if( want('LIST') ){ rreturn 'xyzzy'; } else{ rreturn 'you were eaten by a grue'; } return; } package main; print A::sub, "\n"; print scalar A::sub, "\n"; A::sub() = 42;
xyzzy you were eaten by a grue 42The full code (toggle the comments):
Gives either#To work properly comment out the next line package Xmas; use Want; use Lingua::EN::Numbers::Ordinate; @days = <DATA>; sub days :lvalue { if( want('LVALUE', 'ASSIGN') ){ my $n = shift; $days[$n] = want('ASSIGN'); lnoreturn; } if( want('SCALAR') ){ my $str = ''; foreach( 1..12 ){ $str .= sprintf $days[0], ordinate($_); $str .= join'', reverse(@days[1..$_]), "\n"; } return $str; } if( want('ARRAY') ){ if( my $n = howmany() ){ return @days[1..$n]; } return @days; } } return; } #To work properly comment out the next line, I also tried calling Xmas +::days *main::days = \&days; package main; print scalar days(); days(5) = "Five onion rings!\n"; print scalar days(); __DATA__ On the %s day of Christmas, my true love gave to me A partridge in a pear tree. Two turtle doves Three French hens Four calling birds Five golden rings. Six geese a-laying, Seven swans a-swimming Eight maids a-milking Nine ladies dancing Ten lords a-leaping Eleven pipers piping Twelve drummers drumming
Five onion rings! Five onion rings! Five onion rings! Five onion rings! Five onion rings! Five onion rings! Five onion rings! Five onion rings!or the expected result if in the same package
On the 1st day of Christmas, my true love gave to me A partridge in a pear tree. On the 2nd day of Christmas, my true love gave to me Two turtle doves A partridge in a pear tree. On the 3rd day of Christmas, my true love gave to me Three French hens Two turtle doves A partridge in a pear tree. On the 4th day of Christmas, my true love gave to me Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 5th day of Christmas, my true love gave to me Five golden rings. Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 6th day of Christmas, my true love gave to me Six geese a-laying, Five golden rings. Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 7th day of Christmas, my true love gave to me Seven swans a-swimming Six geese a-laying, Five golden rings. Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 8th day of Christmas, my true love gave to me Eight maids a-milking Seven swans a-swimming Six geese a-laying, Five golden rings. Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 9th day of Christmas, my true love gave to me Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying, Five golden rings. Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 10th day of Christmas, my true love gave to me Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying, Five golden rings. Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 11th day of Christmas, my true love gave to me Eleven pipers piping Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying, Five golden rings. Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 12th day of Christmas, my true love gave to me Twelve drummers drumming Eleven pipers piping Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying, Five golden rings. Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 1st day of Christmas, my true love gave to me A partridge in a pear tree. On the 2nd day of Christmas, my true love gave to me Two turtle doves A partridge in a pear tree. On the 3rd day of Christmas, my true love gave to me Three French hens Two turtle doves A partridge in a pear tree. On the 4th day of Christmas, my true love gave to me Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 5th day of Christmas, my true love gave to me Five onion rings! Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 6th day of Christmas, my true love gave to me Six geese a-laying, Five onion rings! Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 7th day of Christmas, my true love gave to me Seven swans a-swimming Six geese a-laying, Five onion rings! Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 8th day of Christmas, my true love gave to me Eight maids a-milking Seven swans a-swimming Six geese a-laying, Five onion rings! Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 9th day of Christmas, my true love gave to me Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying, Five onion rings! Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 10th day of Christmas, my true love gave to me Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying, Five onion rings! Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 11th day of Christmas, my true love gave to me Eleven pipers piping Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying, Five onion rings! Four calling birds Three French hens Two turtle doves A partridge in a pear tree. On the 12th day of Christmas, my true love gave to me Twelve drummers drumming Eleven pipers piping Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying, Five onion rings! Four calling birds Three French hens Two turtle doves A partridge in a pear tree.
--
In Bob We Trust, All Others Bring Data.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Cross-namespace problem with lvalue subs? Want?
by eric256 (Parson) on Dec 22, 2005 at 16:53 UTC | |
by belg4mit (Prior) on Dec 22, 2005 at 16:56 UTC | |
by ikegami (Patriarch) on Dec 22, 2005 at 17:16 UTC | |
by Tanktalus (Canon) on Dec 22, 2005 at 18:04 UTC | |
by belg4mit (Prior) on Dec 22, 2005 at 18:24 UTC |