(newbie) need help understanding a few lines of code

Discussion in 'Perl Misc' started by Ben, Apr 12, 2009.

  1. Ben

    Ben Guest

    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
    Ben, Apr 12, 2009
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Apr 12, 2009
    #2
    1. Advertising

  3. Ben

    jbenjam Guest

    Thank you Gunnar for a friendly and helpful response!

    Perldoc is my new friend. :)

    -Ben

    On Apr 12, 6:02 pm, Gunnar Hjalmarsson <> 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
    jbenjam, Apr 13, 2009
    #3
  4. 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/"
    Tad J McClellan, Apr 13, 2009
    #4
  5. 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
    Gunnar Hjalmarsson, Apr 13, 2009
    #5
  6. Ben

    Tim McDaniel Guest

    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,
    Tim McDaniel, Apr 15, 2009
    #6
  7. Ben

    Tim McDaniel Guest

    In article <>,
    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,
    Tim McDaniel, Apr 16, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Johnson

    Need a few lines of code help, will pay!

    Robert Johnson, Aug 3, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    313
    makthar
    Aug 4, 2003
  2. Replies:
    3
    Views:
    353
    Cameron Laird
    Mar 7, 2006
  3. Replies:
    1
    Views:
    248
    Walter Roberson
    Mar 7, 2006
  4. Murali
    Replies:
    2
    Views:
    544
    Jerry Coffin
    Mar 9, 2006
  5. Replies:
    3
    Views:
    95
    William James
    Mar 7, 2006
Loading...

Share This Page