(newbie) need help understanding a few lines of code

B

Ben

I'm still learning some of the basic element of Perl. In the
following segment,

#!/usr/bin/perl -w
use strict;
use OpenGL qw/ :all /;
use Math::Trig;
eval 'use Time::HiRes qw( gettimeofday )';
my $hasHires = !$@;
$|++;

The last two lines above, what is occurring? I don't understand the
purpose of "!$@", is it related to error handling of the previous eval
command? What's "$|++;" doing? I understand that $| is something
related to flushing piped output.

Later on in the script:
my $now = $hasHires ? gettimeofday() : time();

What's the function of "?" in this context?

Thanks,

-Ben
 
G

Gunnar Hjalmarsson

Ben said:
I'm still learning some of the basic element of Perl. In the
following segment,

#!/usr/bin/perl -w
use strict;
use OpenGL qw/ :all /;
use Math::Trig;
eval 'use Time::HiRes qw( gettimeofday )';

Loads the Time::HiRes module at run time; eval() prevents that the
program dies for the case Perl fails to find Time::HiRes.
my $hasHires = !$@;

If Time::HiRes was found, the $@ variable contains a null string, i.e. a
false value. !$@ means that a true value is assigned to $hasHires.

perldoc -f eval

Sets $| to a true value (adds 1). See "perldoc perlvar" about the
meaning of $|.
Later on in the script:
my $now = $hasHires ? gettimeofday() : time();

What's the function of "?" in this context?

See "perldoc perlop", the "Conditional Operator" section.

Personally I find the coding style somewhat obfuscated. I would probably
have said something like:

my $noHires = $@;

and later:

my $now = $noHires ? time() : gettimeofday();

But that's probably just a matter of taste. :)
 
J

jbenjam

Thank you Gunnar for a friendly and helpful response!

Perldoc is my new friend. :)

-Ben
 
T

Tad J McClellan

Perldoc is my new friend. :)


Since you did not put the subject of your article in the Subject of
your article, included "newbie" in your Subject, top-posted,
quoted a .sig and did not know about perldoc, I assume that you have
not yet seen the Posting Guidelines that are posted here frequently.

It contains many pointers that will help you get solutions to
Perl programming problems.


http://www.rehabitation.com/clpmisc.shtml
 
T

Tim McDaniel

Ben said:
Personally I find the coding style somewhat obfuscated. I would
probably have said something like:

my $noHires = $@;

I now know the rules on what's considered "true", but personally, if I
use a variable in a boolean context, I prefer it to be a clear
boolean, such as the result of a boolean operator, if for no other
reason than documentation. So I'd do
my $hasHires = ($@ eq '');
Sets $| to a true value (adds 1). See "perldoc perlvar" about the
meaning of $|.

Turning on autoflush with an increment is ridiculous, when they could
just as easily do
$| = 1;
and have it actually work in all cases (assuming autoflush is
implemented at all). It makes me want to start the program with
$| = -1; # sets $| to a true value, so it should autoflush
just to show how bad an increment is.
 
T

Tim McDaniel

Quoth (e-mail address removed):

Oh, but it *does* work in all cases. $| is magical, remember.

~% perl -le'$| = -1; print $|'
1
~% perl -le'$| = 1; $|++; print $|'
1
~% perl -le'$| = -1; $|++; print $|'
1

You. MUST. Be. Shitting. Me.

[checking]
It does just that under Perl 5.00502 and 5.010000. Gahhh ...
Whether your find this amusing or obscene probably depends on your
sense of humour

Obscene, highly counter-intuitive, and undocumented:

$| If set to nonzero, forces a flush right away and after every
write or print on the currently selected output channel.
Default is 0 (regardless of whether the channel is really
buffered by the system or not; $| tells you only whether you've
asked Perl explicitly to flush after each write). ...
(I don't understand what you mean by 'assuming autoflush is implemented
at all'. AFAIK autoflush is always implemented...)

Weasel-wording: I was afraid that, if I left it out, someone would
mock my ignorance of the fact that the port to Irish Business Machines
System/42 did nothing with $|.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top