|
Prev: Why does Net::SFTP trigger my die handler when no errors?
Next: FAQ 4.7 How do I multiply matrices?
From: xhoster on 31 Jan 2006 15:20 "rhorizon74" <rhorizon74(a)yahoo.com> wrote: > Thanks for the help. > > That explains what is happening (i had used the exists approach to work > around). > But it does seem bogus that for a simple print statement i need to > check each and every level. I havent seen too many languages do > something like this (i may be wrong). Right. Many languages would just seg-fault, or perhaps corrupt memory and take down the entire system, instead. > But a print statement shouldnt really be creating any data. What should it do? Xho -- -------------------- http://NewsReader.Com/ -------------------- Usenet Newsgroup Service $9.95/Month 30GB
From: rhorizon74 on 31 Jan 2006 16:43 Hi, The code i posted is just a sample, i was trying to understand exactly what was happening. The are a bunch of scenarios where a different module is supposed to build a hash, and my module needs to print the values. In which case for each level of nesting of a hash i will be forced to check if the node exists (starting with the outer hash and going onto the inner hash). With my limited exposure to perl i guess it seemed a bit diffcult to grasp. I do understand the efficiency it provides while creating a structure. I did check with a lot of colleagues some who were not exactly novices like me with way much more experience in perl and they too had come across this scenario (they grappled with this by adding checks for each level before printing the values) , but to be honest none with any exact knowlege of vivication. So it did seem like this problem was commonly faced by a lot of programmers. Thanks for the reply Uri Guttman wrote: > >>>>> "r" == rhorizon74 <rhorizon74(a)yahoo.com> writes: > > r> Thanks for the help. > r> That explains what is happening (i had used the exists approach to work > r> around). > r> But it does seem bogus that for a simple print statement i need to > r> check each and every level. I havent seen too many languages do > r> something like this (i may be wrong). > r> But a print statement shouldnt really be creating any data. > > if you read my article (which sinan posted the url for) then you will > learn more about autoviv and why it is like it is. and even calling > exists will do the same thing (but that is being fixed in perl6). the > win of autovivication is much greater then the loss of rare code like > yours. if you didn't have it you would need to check each time you used a > reference in structure and either assign a new one or use the existing > one. that is massively ugly and clumsy (and i have seen newbies coding > like that all the time). > > uri > > -- > Uri Guttman ------ uri(a)stemsystems.com -------- http://www.stemsystems.com > --Perl Consulting, Stem Development, Systems Architecture, Design and Coding- > Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
From: rhorizon74 on 31 Jan 2006 16:45 Hi, Id rather have my program fail, most scripting languages would print an error (they would not seg-fault). That would be better than giving incorrect results. Moral of the story is i need to do an exists before i do anything with any hash related structure in perl. Thank you
From: Abigail on 31 Jan 2006 16:46 Dr.Ruud (rvtol+news(a)isolution.nl) wrote on MMMMDXXXVI September MCMXCIII in <URL:news:dro98o.vk.1(a)news.isolution.nl>: == A. Sinan Unur schreef: == == > if( exists $hash{$key} ) { == > print "$hash{$key}\n"; == > } == == == Hunting for alternatives: == == $ echo 'if( exists $hash{$key} ) { print "$hash($key)\n"; }' \ == | perl -MO=Deparse,-x7 == == exists $hash{$key} and do { == print "$hash($key)\n" == }; == == I don't know why the do-block is needed. Because the then part of an if statement is a block. 'do {}' turns a block into an expression. == $ echo 'print "$hash{$key}\n" if exists $hash{$key};' \ == | perl -MO=Deparse,-x7 == == exists $hash{$key} and print "$hash{$key}\n"; Here, the if is a "statement" modifier, which is badly named, as it may only follow an expression. Abigail -- perl -we 'print split /(?=(.*))/s => "Just another Perl Hacker\n";'
From: Ch Lamprecht on 31 Jan 2006 16:52 rhorizon74 wrote: > I havent seen too many languages do > something like this (i may be wrong). Who could tell this but you? Christoph -- perl -e "print scalar reverse q/ed.enilno(a)ergn.l.hc/"
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Why does Net::SFTP trigger my die handler when no errors? Next: FAQ 4.7 How do I multiply matrices? |