What does “[0]” mean in Perl? [duplicate]












0
















This question already has an answer here:




  • What does “select((select(s),$|=1)[0])” do in Perl?

    7 answers




What is the [0] doing in this code:



select((select(LOG_FILE),$!=1)[0]);









share|improve this question















marked as duplicate by brian d foy perl
Users with the  perl badge can single-handedly close perl questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 18:48


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    0
















    This question already has an answer here:




    • What does “select((select(s),$|=1)[0])” do in Perl?

      7 answers




    What is the [0] doing in this code:



    select((select(LOG_FILE),$!=1)[0]);









    share|improve this question















    marked as duplicate by brian d foy perl
    Users with the  perl badge can single-handedly close perl questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Dec 29 '18 at 18:48


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      0












      0








      0









      This question already has an answer here:




      • What does “select((select(s),$|=1)[0])” do in Perl?

        7 answers




      What is the [0] doing in this code:



      select((select(LOG_FILE),$!=1)[0]);









      share|improve this question

















      This question already has an answer here:




      • What does “select((select(s),$|=1)[0])” do in Perl?

        7 answers




      What is the [0] doing in this code:



      select((select(LOG_FILE),$!=1)[0]);




      This question already has an answer here:




      • What does “select((select(s),$|=1)[0])” do in Perl?

        7 answers








      perl






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 29 '18 at 18:16









      brian d foy

      102k27168474




      102k27168474










      asked Dec 29 '18 at 3:04









      cod 4cod 4

      111




      111




      marked as duplicate by brian d foy perl
      Users with the  perl badge can single-handedly close perl questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 29 '18 at 18:48


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by brian d foy perl
      Users with the  perl badge can single-handedly close perl questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 29 '18 at 18:48


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          3














          UPDATE: I answered this ten years ago! What does “select((select(s),$|=1)[0])” do in Perl?





          You're looking at a single element access to a list. The expression in side the parentheses produces some sort of list and the [0] selects one item from the list.



          This bit of code is a very old idiom to set a per-filehandle kinda-global variable. I think you probably meant $| (the autoflush setting) instead of $!.



          First, remember that Perl has the concept of a "default filehandle". That starts out as standard output, but you can change it. That's what the select does.



          Next, realize that each file handle knows its own settings for various things; these are represented by special variables such as $| (see perlvar's section on "Variables related to Filehandles"). When you change these variables, they apply to the current default filehandle.



          So, what you see in this idiom is an inner select that changes the default filehandle. You change the default then set $| to whatever value you want. It looks a bit odd because you have two expressions separated by a comma instead of a semicolon, the use statement separator:



          (select(LOG_FILE), $|=1)


          From this, the idiom wants the result of the select; that's the previous default filehandle. To get that you want the first item in that list. That's in index 0:



          (select(LOG_FILE), $|=1)[0]


          The result of that entire expression is the previous default filehandle, which you now want to restore. Do that with the outer select:



          select((select(LOG_FILE), $|=1)[0]);


          You could have written that with an intermediate variable:



          my $previous = select LOG_FILE;
          $| = 1;
          select($previous);


          If you're writing new stuff on your own, you might use scalar variable for the filehandle then call its autoflush method:



          open my $log_file_fh, '>', $log_filename or die ...;
          $log_file_fh->autoflush(1);





          share|improve this answer































            2














            ( LIST1 )[ LIST2 ] is a list slice. In list context, it evaluates to the elements of LIST1 specified by LIST2.



            In this case, it returns the result of the select.





            select((select(LOG_FILE),$!=1)[0]);


            should be



            select((select(LOG_FILE),$|=1)[0]);


            The latter enables auto-flushing for the LOG_FILE file handle. It can be written more clearly as follows:



            use IO::Handle ();       # Only needed in older versions of Perl.
            LOG_FILE->autoflush(1);




            By the way, you shouldn't be using global variables like that. Instead of



            open LOG_FILE, ...


            you should be using



            open my $LOG_FILE, ...





            share|improve this answer


























            • So the OP meant to write $| instead of $! ?

              – clamp
              Dec 29 '18 at 13:18











            • @clamp, Yeah. Fixed

              – ikegami
              Dec 29 '18 at 21:34


















            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            UPDATE: I answered this ten years ago! What does “select((select(s),$|=1)[0])” do in Perl?





            You're looking at a single element access to a list. The expression in side the parentheses produces some sort of list and the [0] selects one item from the list.



            This bit of code is a very old idiom to set a per-filehandle kinda-global variable. I think you probably meant $| (the autoflush setting) instead of $!.



            First, remember that Perl has the concept of a "default filehandle". That starts out as standard output, but you can change it. That's what the select does.



            Next, realize that each file handle knows its own settings for various things; these are represented by special variables such as $| (see perlvar's section on "Variables related to Filehandles"). When you change these variables, they apply to the current default filehandle.



            So, what you see in this idiom is an inner select that changes the default filehandle. You change the default then set $| to whatever value you want. It looks a bit odd because you have two expressions separated by a comma instead of a semicolon, the use statement separator:



            (select(LOG_FILE), $|=1)


            From this, the idiom wants the result of the select; that's the previous default filehandle. To get that you want the first item in that list. That's in index 0:



            (select(LOG_FILE), $|=1)[0]


            The result of that entire expression is the previous default filehandle, which you now want to restore. Do that with the outer select:



            select((select(LOG_FILE), $|=1)[0]);


            You could have written that with an intermediate variable:



            my $previous = select LOG_FILE;
            $| = 1;
            select($previous);


            If you're writing new stuff on your own, you might use scalar variable for the filehandle then call its autoflush method:



            open my $log_file_fh, '>', $log_filename or die ...;
            $log_file_fh->autoflush(1);





            share|improve this answer




























              3














              UPDATE: I answered this ten years ago! What does “select((select(s),$|=1)[0])” do in Perl?





              You're looking at a single element access to a list. The expression in side the parentheses produces some sort of list and the [0] selects one item from the list.



              This bit of code is a very old idiom to set a per-filehandle kinda-global variable. I think you probably meant $| (the autoflush setting) instead of $!.



              First, remember that Perl has the concept of a "default filehandle". That starts out as standard output, but you can change it. That's what the select does.



              Next, realize that each file handle knows its own settings for various things; these are represented by special variables such as $| (see perlvar's section on "Variables related to Filehandles"). When you change these variables, they apply to the current default filehandle.



              So, what you see in this idiom is an inner select that changes the default filehandle. You change the default then set $| to whatever value you want. It looks a bit odd because you have two expressions separated by a comma instead of a semicolon, the use statement separator:



              (select(LOG_FILE), $|=1)


              From this, the idiom wants the result of the select; that's the previous default filehandle. To get that you want the first item in that list. That's in index 0:



              (select(LOG_FILE), $|=1)[0]


              The result of that entire expression is the previous default filehandle, which you now want to restore. Do that with the outer select:



              select((select(LOG_FILE), $|=1)[0]);


              You could have written that with an intermediate variable:



              my $previous = select LOG_FILE;
              $| = 1;
              select($previous);


              If you're writing new stuff on your own, you might use scalar variable for the filehandle then call its autoflush method:



              open my $log_file_fh, '>', $log_filename or die ...;
              $log_file_fh->autoflush(1);





              share|improve this answer


























                3












                3








                3







                UPDATE: I answered this ten years ago! What does “select((select(s),$|=1)[0])” do in Perl?





                You're looking at a single element access to a list. The expression in side the parentheses produces some sort of list and the [0] selects one item from the list.



                This bit of code is a very old idiom to set a per-filehandle kinda-global variable. I think you probably meant $| (the autoflush setting) instead of $!.



                First, remember that Perl has the concept of a "default filehandle". That starts out as standard output, but you can change it. That's what the select does.



                Next, realize that each file handle knows its own settings for various things; these are represented by special variables such as $| (see perlvar's section on "Variables related to Filehandles"). When you change these variables, they apply to the current default filehandle.



                So, what you see in this idiom is an inner select that changes the default filehandle. You change the default then set $| to whatever value you want. It looks a bit odd because you have two expressions separated by a comma instead of a semicolon, the use statement separator:



                (select(LOG_FILE), $|=1)


                From this, the idiom wants the result of the select; that's the previous default filehandle. To get that you want the first item in that list. That's in index 0:



                (select(LOG_FILE), $|=1)[0]


                The result of that entire expression is the previous default filehandle, which you now want to restore. Do that with the outer select:



                select((select(LOG_FILE), $|=1)[0]);


                You could have written that with an intermediate variable:



                my $previous = select LOG_FILE;
                $| = 1;
                select($previous);


                If you're writing new stuff on your own, you might use scalar variable for the filehandle then call its autoflush method:



                open my $log_file_fh, '>', $log_filename or die ...;
                $log_file_fh->autoflush(1);





                share|improve this answer













                UPDATE: I answered this ten years ago! What does “select((select(s),$|=1)[0])” do in Perl?





                You're looking at a single element access to a list. The expression in side the parentheses produces some sort of list and the [0] selects one item from the list.



                This bit of code is a very old idiom to set a per-filehandle kinda-global variable. I think you probably meant $| (the autoflush setting) instead of $!.



                First, remember that Perl has the concept of a "default filehandle". That starts out as standard output, but you can change it. That's what the select does.



                Next, realize that each file handle knows its own settings for various things; these are represented by special variables such as $| (see perlvar's section on "Variables related to Filehandles"). When you change these variables, they apply to the current default filehandle.



                So, what you see in this idiom is an inner select that changes the default filehandle. You change the default then set $| to whatever value you want. It looks a bit odd because you have two expressions separated by a comma instead of a semicolon, the use statement separator:



                (select(LOG_FILE), $|=1)


                From this, the idiom wants the result of the select; that's the previous default filehandle. To get that you want the first item in that list. That's in index 0:



                (select(LOG_FILE), $|=1)[0]


                The result of that entire expression is the previous default filehandle, which you now want to restore. Do that with the outer select:



                select((select(LOG_FILE), $|=1)[0]);


                You could have written that with an intermediate variable:



                my $previous = select LOG_FILE;
                $| = 1;
                select($previous);


                If you're writing new stuff on your own, you might use scalar variable for the filehandle then call its autoflush method:



                open my $log_file_fh, '>', $log_filename or die ...;
                $log_file_fh->autoflush(1);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 29 '18 at 18:48









                brian d foybrian d foy

                102k27168474




                102k27168474

























                    2














                    ( LIST1 )[ LIST2 ] is a list slice. In list context, it evaluates to the elements of LIST1 specified by LIST2.



                    In this case, it returns the result of the select.





                    select((select(LOG_FILE),$!=1)[0]);


                    should be



                    select((select(LOG_FILE),$|=1)[0]);


                    The latter enables auto-flushing for the LOG_FILE file handle. It can be written more clearly as follows:



                    use IO::Handle ();       # Only needed in older versions of Perl.
                    LOG_FILE->autoflush(1);




                    By the way, you shouldn't be using global variables like that. Instead of



                    open LOG_FILE, ...


                    you should be using



                    open my $LOG_FILE, ...





                    share|improve this answer


























                    • So the OP meant to write $| instead of $! ?

                      – clamp
                      Dec 29 '18 at 13:18











                    • @clamp, Yeah. Fixed

                      – ikegami
                      Dec 29 '18 at 21:34
















                    2














                    ( LIST1 )[ LIST2 ] is a list slice. In list context, it evaluates to the elements of LIST1 specified by LIST2.



                    In this case, it returns the result of the select.





                    select((select(LOG_FILE),$!=1)[0]);


                    should be



                    select((select(LOG_FILE),$|=1)[0]);


                    The latter enables auto-flushing for the LOG_FILE file handle. It can be written more clearly as follows:



                    use IO::Handle ();       # Only needed in older versions of Perl.
                    LOG_FILE->autoflush(1);




                    By the way, you shouldn't be using global variables like that. Instead of



                    open LOG_FILE, ...


                    you should be using



                    open my $LOG_FILE, ...





                    share|improve this answer


























                    • So the OP meant to write $| instead of $! ?

                      – clamp
                      Dec 29 '18 at 13:18











                    • @clamp, Yeah. Fixed

                      – ikegami
                      Dec 29 '18 at 21:34














                    2












                    2








                    2







                    ( LIST1 )[ LIST2 ] is a list slice. In list context, it evaluates to the elements of LIST1 specified by LIST2.



                    In this case, it returns the result of the select.





                    select((select(LOG_FILE),$!=1)[0]);


                    should be



                    select((select(LOG_FILE),$|=1)[0]);


                    The latter enables auto-flushing for the LOG_FILE file handle. It can be written more clearly as follows:



                    use IO::Handle ();       # Only needed in older versions of Perl.
                    LOG_FILE->autoflush(1);




                    By the way, you shouldn't be using global variables like that. Instead of



                    open LOG_FILE, ...


                    you should be using



                    open my $LOG_FILE, ...





                    share|improve this answer















                    ( LIST1 )[ LIST2 ] is a list slice. In list context, it evaluates to the elements of LIST1 specified by LIST2.



                    In this case, it returns the result of the select.





                    select((select(LOG_FILE),$!=1)[0]);


                    should be



                    select((select(LOG_FILE),$|=1)[0]);


                    The latter enables auto-flushing for the LOG_FILE file handle. It can be written more clearly as follows:



                    use IO::Handle ();       # Only needed in older versions of Perl.
                    LOG_FILE->autoflush(1);




                    By the way, you shouldn't be using global variables like that. Instead of



                    open LOG_FILE, ...


                    you should be using



                    open my $LOG_FILE, ...






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 29 '18 at 21:34

























                    answered Dec 29 '18 at 3:10









                    ikegamiikegami

                    262k11176396




                    262k11176396













                    • So the OP meant to write $| instead of $! ?

                      – clamp
                      Dec 29 '18 at 13:18











                    • @clamp, Yeah. Fixed

                      – ikegami
                      Dec 29 '18 at 21:34



















                    • So the OP meant to write $| instead of $! ?

                      – clamp
                      Dec 29 '18 at 13:18











                    • @clamp, Yeah. Fixed

                      – ikegami
                      Dec 29 '18 at 21:34

















                    So the OP meant to write $| instead of $! ?

                    – clamp
                    Dec 29 '18 at 13:18





                    So the OP meant to write $| instead of $! ?

                    – clamp
                    Dec 29 '18 at 13:18













                    @clamp, Yeah. Fixed

                    – ikegami
                    Dec 29 '18 at 21:34





                    @clamp, Yeah. Fixed

                    – ikegami
                    Dec 29 '18 at 21:34



                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas