Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re:(2) Parse Loops with flat text files. (code)

by deprecated (Priest)
on May 13, 2001 at 23:51 UTC ( [id://80091]=note: print w/replies, xml ) Need Help??


in reply to Re: Parse Loops with flat text files. (code)
in thread Parse Loops with flat text files. (code)

Hm, I knew of the flip-flop operator but I havent ever used it. Time to go read perlop.

with regards to returning undef. I use it just as a rule of thumb because it will evaluate to false. However, in subs where I am returning the value of the tested object, I want to be able to return 0 if the test is good -- so I always check to see if defined test( $foo ). dig?

brother dep.

--
Laziness, Impatience, Hubris, and Generosity.

  • Comment on Re:(2) Parse Loops with flat text files. (code)

Replies are listed 'Best First'.
Re: Re:(2) Parse Loops with flat text files. (code)
by danger (Priest) on May 14, 2001 at 00:39 UTC

    My only point was regarding when you simply want to return a false value to indicate a subroutine failure. Consider the following contrived example where we only want to process strings beginning with a particular pattern:

    #!/usr/bin/perl -w use strict; my @patterns = (qr/^foo/, qr/^qux/); my @strings = ('foo bar', 'bar bar', 'qux bar'); my @stuff; foreach my $string (@strings){ if(@stuff = dice_it($string)){ print "Processing: $string\n"; process_stuff(@stuff); } } sub dice_it { my $string = shift; foreach my $pat (@patterns) { return split //, $string if $string =~ /$pat/; } return undef; } sub process_stuff { foreach (@_) { print "<$_>"; } print "\n"; }

    I'm not suggesting this a terribly common problem (or that the above is a good way to approach this particular example). I just wanted to point out that returning 'undef' as a failure mode isn't always appropriate -- and people who do so may forget that an array containing one undefined element still evaluates to true so they may bang their head for a while before they realize why they are processing the string 'bar bar' and getting a warning. Changing the last line of dice_it() to just a bare 'return' statement alleviates the problem because it returns the 'right' thing depending on context.

Re: Re:(2) Parse Loops with flat text files. (code)
by Anonymous Monk on May 15, 2001 at 00:07 UTC
    I was also thinking the flip-flop operator would be good for this. It's what I always try and use for this kind of "in the middle" test.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://80091]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-23 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found