Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > (newbie) need help understanding a few lines of code

Reply
Thread Tools

(newbie) need help understanding a few lines of code

 
 
Ben
Guest
Posts: n/a
 
      04-12-2009
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


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-12-2009
Ben wrote:
> 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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
jbenjam
Guest
Posts: n/a
 
      04-13-2009

Thank you Gunnar for a friendly and helpful response!

Perldoc is my new friend.

-Ben

On Apr 12, 6:02*pm, Gunnar Hjalmarsson <nore...@gunnar.cc> wrote:
> Ben wrote:
> > 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.
>
> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl


 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      04-13-2009
jbenjam <> wrote:


> 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


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-13-2009
Tad J McClellan wrote:
> jbenjam <> wrote:
>>
>> Perldoc is my new friend.

>
> Since you did not put the subject of your article in the Subject of
> your article,


Your criticizing of the subject is somewhat misplaced this time IMO.

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


But the posting guidelines are always a good idea to study.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      04-15-2009
In article <>,
Gunnar Hjalmarsson <> wrote:
>Ben wrote:
>> [stuff]

>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.

--
Tim McDaniel,
 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      04-15-2009
In article <qn0ib6->,
Ben Morrow <> wrote:
>
>Quoth :
>> 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.

>
>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 $|.

--
Tim McDaniel,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CAn any one help me in understanding these 3 lines in code from PLL?? maheshkumar0459 VHDL 0 07-06-2011 02:37 PM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM
Help - just a few lines of code needed lory88@gmail.com Python 3 03-07-2006 02:08 PM
Help - just a few lines of code needed lory88@gmail.com C Programming 1 03-07-2006 04:01 AM
Need a few lines of code help, will pay! Robert Johnson ASP .Net 3 08-04-2003 06:06 PM



Advertisments