--- David Gilden <[EMAIL PROTECTED]> wrote:
> Dear Casey and the list,
> Thanks for your all of your valuable help,
>
> What is $|++ for?
$| is a boolean Perl variable that determines whether the currently
selected default output stream will be unbuffered. The default it
STDOUT, so $|=1; means don't buffer STDOUT. $|++ does basically the
same thing, making it true by incrementing it to some positive number.
If you want another stream unbuffered, you have to select it first,
like this (including a trick to put the default back to *whatever* it
might have been before):
========
open FOO, $myFile or die $!; # creates filehandle FOO
my $prev = select FOO; # sets FOO as current default
# (and saves previous default in $prev)
$|=1; # sets FOO unbuffered
select $prev; # restores previous default
========
Now FOO will be unbuffered, and the previous default is still the
default.
> #!/usr/local/bin/perl -w
> use strict;
> $|++;
>
> while ( <DATA> ) {
> chomp;
> print( &validate( $_ ) ? 'valid entry' : 'try again punk' );
> print "\n";
> }
>
> sub validate {
> my $currency = shift;
>
> # return false if we have an empty string or the string is just
> '$'.
> return 0 unless length( $currency ) > 0 && $currency ne '$';
>
> $currency =~ m<
> ^ # The beginning of the string
> \$? # We may have a $ sign, we may not
> \s* # We may encounter some space here
> \d* # We may nave a numerator but could just have
> '.50'
> (?:\.?\d{1,2})? # and we might have a denominator
> $ # The end of the string
> >x ? 1: 0; # true if match succeeded, false otherwise
> }
>
> -----------
>
> sub checkPrice{
> return ($_[0] !~ /^\$?(\d+|\d*\.\d\d)$/) ? 1 : 0;
> }
>
> Is there a shorthand? Or do I need $_[0],
>
> ---------
>
> I could not seem to include the sub chekPrice in my if statement
>
> $x = &checkPrice($in{'price'});
> if ( ($in{'name'} eq "") or
> ($in{'type'} eq "") or
> ($in{'price'} eq "") or
> $x)
>
> { .....
>
>
> ### won't pass syntax validation!
> if ( ($in{'name'} eq "") or
> ($in{'type'} eq "") or
> ($in{'price'} eq "") or
> &checkPrice($in{'price'})
> )
>
> { ......
>
>
>
> -----------
>
> Are there any issues with using
> if (!length $in{price}) ....
> as opposed to:
> if ($in{price} eq "") ...
I would probably just say
unless ($in{'price'} and $in{'type') and $in{'name'}) { # . . .
This will fail if the value is zero, but that's probably ok in this
case.
> I am not sure about !length,
> I mean it is not a $length, so it is not scalar is it some special
> variable?
length() is a perl function. =o)
__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/