text: user1224: use Bio::DB::GenBank;
  use Bio::DB::Query::GenBank;
  
  $query = "LEGK"; 
  $query_obj = Bio::DB::Query::GenBank->new(-db => 'protein', 
                                            -query => $query );
  
  $gb_obj = Bio::DB::GenBank->new;
  
  $stream_obj = $gb_obj->get_Stream_by_query($query_obj);
  
  while ($seq_obj = $stream_obj->next_seq) {
      # do something with the sequence object    
      print ">$query",' ', $seq_obj->display_id, ' ', $seq_obj->desc,"\n", $seq_obj->seq[,'\n';
  
  How can I print first occurrence of protein sequence?
  
  user298: In the simplest case, if you just want to stop after the first record was printed, you can just add exit (I also corrected the syntax errors you had and added use strict and use warnings; I suggest you get into the habit of using those two, they save you from a lot of grief in the long run):
  #!/usr/bin/env perl
  use Bio::DB::GenBank;
  use Bio::DB::Query::GenBank;
  use strict;
  use warnings; 
  my $query = "LEGK"; 
  my $query_obj = Bio::DB::Query::GenBank->new(-db => 'protein', 
                                            -query => $query );
  my $gb_obj = Bio::DB::GenBank->new;
  my $stream_obj = $gb_obj->get_Stream_by_query($query_obj);
  while (my $seq_obj = $stream_obj->next_seq) {
   print ">$query",' ', $seq_obj->display_id, ' ', $seq_obj->desc,"\n", $seq_obj->seq, "\n";
      exit;
  }
  
  If instead, you want to only process the first result for a list of multiple queries:
  #!/usr/bin/env perl
  use Bio::DB::GenBank;
  use Bio::DB::Query::GenBank;
  use strict;
  use warnings; 
  my @queries = qw(LEGK TP53 ACT1); 
  query:foreach my $query (@queries) {
   my $query_obj = Bio::DB::Query::GenBank->new(-db => 'protein', 
                                            -query => $query );
   my $gb_obj = Bio::DB::GenBank->new;
   my $stream_obj = $gb_obj->get_Stream_by_query($query_obj);
   while (my $seq_obj = $stream_obj->next_seq) {
    print ">$query",' ', $seq_obj->display_id, ' ', $seq_obj->desc,"\n", $seq_obj->seq, "\n";
          ## Move to the next query
          next query;
      }
  }
  
  Or, to print only the first sequence if multiple quries have identical sequences:
  #!/usr/bin/env perl
  use Bio::DB::GenBank;
  use Bio::DB::Query::GenBank;
  use strict;
  use warnings; 
  my @queries = qw(LEGK TP53 ACT1); 
  foreach my $query (@queries) {
   my %seen;
   my $query_obj = Bio::DB::Query::GenBank->new(-db => 'protein', 
                                            -query => $query );
   my $gb_obj = Bio::DB::GenBank->new;
   my $stream_obj = $gb_obj->get_Stream_by_query($query_obj);
   while (my $seq_obj = $stream_obj->next_seq) {
    if ($seen{$seq_obj->seq}) {
     ## Here, since we're not using an argument (next query), this will move
     ## to the next iteration of the current loop and not the `foreach`. 
     next;
    }
    print ">$query",' ', $seq_obj->display_id, ' ', $seq_obj->desc,"\n", $seq_obj->seq, "\n";
      }
  }
  
  
original_text: 1224: <pre><code>use Bio::DB::GenBank;
  use Bio::DB::Query::GenBank;
  
  $query = "LEGK"; 
  $query_obj = Bio::DB::Query::GenBank-&gt;new(-db =&gt; 'protein', 
                                            -query =&gt; $query );
  
  $gb_obj = Bio::DB::GenBank-&gt;new;
  
  $stream_obj = $gb_obj-&gt;get_Stream_by_query($query_obj);
  
  while ($seq_obj = $stream_obj-&gt;next_seq) {
      # do something with the sequence object    
      print "&gt;$query",' ', $seq_obj-&gt;display_id, ' ', $seq_obj-&gt;desc,"\n", $seq_obj-&gt;seq[,'\n';
  </code></pre>
  
  <p>How can I print first occurrence of protein sequence?</p>
   298: <p>In the simplest case, if you just want to stop after the first record was printed, you can just add <code>exit</code> (I also corrected the syntax errors you had and added <code>use strict</code> and <code>use warnings</code>; I suggest you get into the habit of using those two, they save you from a lot of grief in the long run):</p>
  
  <pre class="lang-perl prettyprint-override"><code>#!/usr/bin/env perl
  use Bio::DB::GenBank;
  use Bio::DB::Query::GenBank;
  use strict;
  use warnings; 
  my $query = "LEGK"; 
  my $query_obj = Bio::DB::Query::GenBank-&gt;new(-db =&gt; 'protein', 
                                            -query =&gt; $query );
  my $gb_obj = Bio::DB::GenBank-&gt;new;
  my $stream_obj = $gb_obj-&gt;get_Stream_by_query($query_obj);
  while (my $seq_obj = $stream_obj-&gt;next_seq) {
   print "&gt;$query",' ', $seq_obj-&gt;display_id, ' ', $seq_obj-&gt;desc,"\n", $seq_obj-&gt;seq, "\n";
      exit;
  }
  </code></pre>
  
  <p>If instead, you want to only process the first result for a list of multiple queries:</p>
  
  <pre><code>#!/usr/bin/env perl
  use Bio::DB::GenBank;
  use Bio::DB::Query::GenBank;
  use strict;
  use warnings; 
  my @queries = qw(LEGK TP53 ACT1); 
  query:foreach my $query (@queries) {
   my $query_obj = Bio::DB::Query::GenBank-&gt;new(-db =&gt; 'protein', 
                                            -query =&gt; $query );
   my $gb_obj = Bio::DB::GenBank-&gt;new;
   my $stream_obj = $gb_obj-&gt;get_Stream_by_query($query_obj);
   while (my $seq_obj = $stream_obj-&gt;next_seq) {
    print "&gt;$query",' ', $seq_obj-&gt;display_id, ' ', $seq_obj-&gt;desc,"\n", $seq_obj-&gt;seq, "\n";
          ## Move to the next query
          next query;
      }
  }
  </code></pre>
  
  <p>Or, to print only the first sequence if multiple quries have identical sequences:</p>
  
  <pre><code>#!/usr/bin/env perl
  use Bio::DB::GenBank;
  use Bio::DB::Query::GenBank;
  use strict;
  use warnings; 
  my @queries = qw(LEGK TP53 ACT1); 
  foreach my $query (@queries) {
   my %seen;
   my $query_obj = Bio::DB::Query::GenBank-&gt;new(-db =&gt; 'protein', 
                                            -query =&gt; $query );
   my $gb_obj = Bio::DB::GenBank-&gt;new;
   my $stream_obj = $gb_obj-&gt;get_Stream_by_query($query_obj);
   while (my $seq_obj = $stream_obj-&gt;next_seq) {
    if ($seen{$seq_obj-&gt;seq}) {
     ## Here, since we're not using an argument (next query), this will move
     ## to the next iteration of the current loop and not the `foreach`. 
     next;
    }
    print "&gt;$query",' ', $seq_obj-&gt;display_id, ' ', $seq_obj-&gt;desc,"\n", $seq_obj-&gt;seq, "\n";
      }
  }
  </code></pre>
  