Hello,
while working on a package I ran into a problem for which I could not find a pretty solution.
I broke things down to come up with the example code below to demonstrate my issue. When this script is executed, it will print only "12568", because 0 is considered as false and this breaks the while loop. Now I could use while(1) and add some more lines to check $i for undef and then do a "last", but the package is supposed to be used by others so I want to keep it as simple as possible.
What would be a pretty/easy way to make while only treat undef as a breaking argument?
#!/usr/bin/perl -w
use strict;
use warnings;
my $t = test->new();
while (my $i = $t->get) { print $i; };
package test;
use strict;
use warnings;
my @list;
sub new {
my $self = shift;
my $this = {};
@list = ( 1, 2, 5, 6, 8, 0, 9 );
bless($this, $self);
return($this);
};
sub get {
return(shift(@list));
};
1;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|