Bug 30147: Fix modules usage in opac-detail.pl
[koha.git] / reports / catalogue_stats.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use C4::Auth qw( get_template_and_user );
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Koha qw( GetAuthorisedValues );
27 use C4::Reports qw( GetDelimiterChoices );
28 use C4::Biblio qw( GetMarcSubfieldStructureFromKohaField );
29
30 use Koha::AuthorisedValues;
31 use Koha::DateUtils qw( dt_from_string );
32 use Koha::ItemTypes;
33
34 =head1 NAME
35
36 plugin that shows a stats on borrowers
37
38 =head1 DESCRIPTION
39
40 =cut
41
42 our $debug = 0;
43 my $input = CGI->new;
44 my $fullreportname = "reports/catalogue_stats.tt";
45 my $do_it       = $input->param('do_it');
46 my $line        = $input->param("Line");
47 my $column      = $input->param("Column");
48 my $cellvalue      = $input->param("Cellvalue"); # one of 'items', 'biblios', 'deleteditems'
49 my @filters     = $input->multi_param("Filter");
50 my $cotedigits  = $input->param("cotedigits");
51 my $output      = $input->param("output");
52 my $basename    = $input->param("basename");
53 our $sep        = $input->param("sep");
54 $sep = "\t" if ($sep eq 'tabulation');
55 my $item_itype;
56 if(C4::Context->preference('item-level_itypes')) {
57         $item_itype = "items\.itype"
58 } else {
59         $item_itype = "itemtype";
60 }
61 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($line=~ /publicationyear/ )) {
62     $line = "copyrightdate";
63 }
64 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($column =~ /publicationyear/ )) {
65     $column = "copyrightdate";
66 }
67
68 my ($template, $borrowernumber, $cookie)
69         = get_template_and_user({template_name => $fullreportname,
70                                 query => $input,
71                                 type => "intranet",
72                                 flagsrequired => {reports => '*'},
73                                 });
74 $template->param(do_it => $do_it);
75 if ($do_it) {
76     my $results = calculate( $line, $column, $cellvalue, $cotedigits, \@filters );
77     if ( $output eq "screen" ) {
78         $template->param( mainloop => $results );
79         output_html_with_http_headers $input, $cookie, $template->output;
80         exit;
81     } else {
82         print $input->header(
83             -type       => 'text/csv',
84             -encoding   => 'utf-8',
85             -attachment => "$basename.csv",
86             -name       => "$basename.csv"
87         );
88         my $cols  = @$results[0]->{loopcol};
89         my $lines = @$results[0]->{looprow};
90         print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
91         foreach my $col (@$cols) {
92             print $col->{coltitle} . $sep;
93         }
94         print "Total\n";
95         foreach my $line (@$lines) {
96             my $x = $line->{loopcell};
97             print $line->{rowtitle} . $sep;
98             foreach my $cell (@$x) {
99                 print $cell->{value} . $sep;
100             }
101             print $line->{totalrow};
102             print "\n";
103         }
104         print "TOTAL";
105         $cols = @$results[0]->{loopfooter};
106         foreach my $col (@$cols) {
107             print $sep. $col->{totalcol};
108         }
109         print $sep. @$results[0]->{total};
110         exit;
111     }
112 } else {
113         my $dbh = C4::Context->dbh;
114         my $count=0;
115
116     my $itemtypes = Koha::ItemTypes->search_with_localization;
117
118     my @authvals = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } );
119     my @locations = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } );
120
121     foreach my $kohafield (qw(items.notforloan items.materials)) {
122         my $subfield_structure = GetMarcSubfieldStructureFromKohaField($kohafield);
123         if($subfield_structure) {
124             my $avlist;
125             my $avcategory = $subfield_structure->{authorised_value};
126             if($avcategory) {
127                 $avlist = GetAuthorisedValues($avcategory);
128             }
129             my $kf = $kohafield;
130             $kf =~ s/^items\.//;
131             $template->param(
132                 $kf => 1,
133                 $kf."_label" => $subfield_structure->{liblibrarian},
134                 $kf."_avlist" => $avlist
135             );
136         }
137     }
138
139     my @mime  = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
140
141     $template->param(
142         itemtypes    => $itemtypes,
143         locationloop => \@locations,
144         authvals     => \@authvals,
145         CGIextChoice => \@mime,
146         CGIsepChoice => GetDelimiterChoices,
147         item_itype   => $item_itype,
148     );
149
150 }
151 output_html_with_http_headers $input, $cookie, $template->output;
152
153 ## End of Main Body
154
155
156 sub calculate {
157     my ( $line, $column, $cellvalue, $cotedigits, $filters ) = @_;
158     my @mainloop;
159     my @loopfooter;
160     my @loopcol;
161     my @loopline;
162     my @looprow;
163     my %globalline;
164     my $grantotal     = 0;
165     my $barcodelike   = @$filters[16];
166     my $barcodefilter = @$filters[17];
167     my $not;
168     my $itemstable = ($cellvalue eq 'deleteditems') ? 'deleteditems' : 'items';
169
170     my $dbh = C4::Context->dbh;
171
172     # if barcodefilter is empty set as %
173     if ($barcodefilter) {
174
175         # Check if barcodefilter is "like" or "not like"
176         if ( !$barcodelike ) {
177             $not = "not";
178         }
179
180         # Change * to %
181         $barcodefilter =~ s/\*/%/g;
182     }
183
184     # Filters
185     # Checking filters
186     #
187     my @loopfilter;
188     for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
189         my %cell;
190         if ( defined @$filters[$i] and @$filters[$i] ne '' and $i != 11 ) {
191             if ( ( ( $i == 1 ) or ( $i == 5 ) ) and ( @$filters[ $i - 1 ] ) ) {
192                 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
193             }
194             $cell{filter} .= @$filters[$i];
195             $cell{crit} .=
196                 ( $i == 0 )  ? "Item CallNumber From"
197               : ( $i == 1 )  ? "Item CallNumber To"
198               : ( $i == 2 )  ? "Item type"
199               : ( $i == 3 )  ? "Publisher"
200               : ( $i == 4 )  ? "Publication year From"
201               : ( $i == 5 )  ? "Publication year To"
202               : ( $i == 6 ) ? "Library"
203               : ( $i == 7 ) ? "Shelving Location"
204               : ( $i == 8 ) ? "Collection Code"
205               : ( $i == 9 ) ? "Status"
206               : ( $i == 10 ) ? "Materials"
207               : ( $i == 12 and $filters->[11] == 0 ) ? "Barcode (not like)"
208               : ( $i == 12 and $filters->[11] == 1 ) ? "Barcode (like)"
209               : ( $i == 13 ) ? "Date acquired (item) from"
210               : ( $i == 14 ) ? "Date acquired (item) to"
211               : ( $i == 15 ) ? "Date deleted (item) from"
212               : ( $i == 16 ) ? "Date deleted (item) to"
213               :                '';
214
215             push @loopfilter, \%cell;
216         }
217     }
218
219     @$filters[13] = dt_from_string(@$filters[13])->date() if @$filters[13];
220     @$filters[14] = dt_from_string(@$filters[14])->date() if @$filters[14];
221     @$filters[15] = dt_from_string(@$filters[15])->date() if @$filters[15];
222     @$filters[16] = dt_from_string(@$filters[16])->date() if @$filters[16];
223
224     my @linefilter;
225     $linefilter[0] = @$filters[0] if ( $line =~ /items\.itemcallnumber/ );
226     $linefilter[1] = @$filters[1] if ( $line =~ /items\.itemcallnumber/ );
227     if ( C4::Context->preference('item-level_itypes') ) {
228         $linefilter[0] = @$filters[2] if ( $line =~ /items\.itype/ );
229     } else {
230         $linefilter[0] = @$filters[2] if ( $line =~ /itemtype/ );
231     }
232     $linefilter[0] = @$filters[3] if ( $line =~ /publishercode/ );
233     $linefilter[0] = @$filters[4] if ( $line =~ /publicationyear/ );
234     $linefilter[1] = @$filters[5] if ( $line =~ /publicationyear/ );
235
236     $linefilter[0] = @$filters[6] if ( $line =~ /items\.homebranch/ );
237     $linefilter[0] = @$filters[7] if ( $line =~ /items\.location/ );
238     $linefilter[0] = @$filters[8] if ( $line =~ /items\.ccode/ );
239     $linefilter[0] = @$filters[9] if ( $line =~ /items\.notforloan/ );
240     $linefilter[0] = @$filters[10] if ( $line =~ /items\.materials/ );
241     $linefilter[0] = @$filters[13] if ( $line =~ /items\.dateaccessioned/ );
242     $linefilter[1] = @$filters[14] if ( $line =~ /items\.dateaccessioned/ );
243     $linefilter[0] = @$filters[15] if ( $line =~ /deleteditems\.timestamp/ );
244     $linefilter[1] = @$filters[16] if ( $line =~ /deleteditems\.timestamp/ );
245
246     my @colfilter;
247     $colfilter[0] = @$filters[0] if ( $column =~ /items\.itemcallnumber/ );
248     $colfilter[1] = @$filters[1] if ( $column =~ /items\.itemcallnumber/ );
249     if ( C4::Context->preference('item-level_itypes') ) {
250         $colfilter[0] = @$filters[2] if ( $column =~ /items\.itype/ );
251     } else {
252         $colfilter[0] = @$filters[2] if ( $column =~ /itemtype/ );
253     }
254     $colfilter[0] = @$filters[3]  if ( $column =~ /publishercode/ );
255     $colfilter[0] = @$filters[4]  if ( $column =~ /publicationyear/ );
256     $colfilter[1] = @$filters[5]  if ( $column =~ /publicationyear/ );
257     $colfilter[0] = @$filters[6] if ( $column =~ /items\.homebranch/ );
258     $colfilter[0] = @$filters[7] if ( $column =~ /items\.location/ );
259     $colfilter[0] = @$filters[8] if ( $column =~ /items\.ccode/ );
260     $colfilter[0] = @$filters[9] if ( $column =~ /items\.notforloan/ );
261     $colfilter[0] = @$filters[10] if ( $column =~ /items\.materials/ );
262     $colfilter[0] = @$filters[13] if ( $column =~ /items.dateaccessioned/ );
263     $colfilter[1] = @$filters[14] if ( $column =~ /items\.dateaccessioned/ );
264     $colfilter[0] = @$filters[15] if ( $column =~ /deleteditems\.timestamp/ );
265     $colfilter[1] = @$filters[16] if ( $column =~ /deleteditems\.timestamp/ );
266
267     # 1st, loop rows.
268     my $origline = $line;
269     $line =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
270     my $linefield;
271     if ( ( $line =~ /itemcallnumber/ ) and ($cotedigits) ) {
272         $linefield = "left($line,$cotedigits)";
273     } elsif ( $line =~ /^deleteditems\.timestamp$/ ) {
274         $linefield = "DATE($line)";
275     } else {
276         $linefield = $line;
277     }
278
279     my $strsth = "SELECT DISTINCTROW $linefield FROM $itemstable
280                     LEFT JOIN biblioitems USING (biblioitemnumber)
281                     LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
282                   WHERE 1 ";
283     $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
284     if (@linefilter) {
285         if ( $linefilter[1] ) {
286             $strsth .= " AND $line >= ? ";
287             $strsth .= " AND $line <= ? ";
288         } elsif ( defined $linefilter[0] and $linefilter[0] ne '' ) {
289             $linefilter[0] =~ s/\*/%/g;
290             $strsth .= " AND $line LIKE ? ";
291         }
292     }
293     $strsth .= " ORDER BY $linefield";
294     $debug and print STDERR "catalogue_stats SQL: $strsth\n";
295
296     my $sth = $dbh->prepare($strsth);
297     if ( $barcodefilter and (@linefilter) and ( $linefilter[1] ) ) {
298         $sth->execute( $barcodefilter, $linefilter[0], $linefilter[1] );
299     } elsif ( (@linefilter) and ( $linefilter[1] ) ) {
300         $sth->execute( $linefilter[0], $linefilter[1] );
301     } elsif ( $barcodefilter and $linefilter[0] ) {
302         $sth->execute( $barcodefilter, $linefilter[0] );
303     } elsif ( $linefilter[0] ) {
304         $sth->execute($linefilter[0]);
305     } elsif ($barcodefilter) {
306         $sth->execute($barcodefilter);
307     } else {
308         $sth->execute();
309     }
310     my $rowauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origline } ) };
311     while ( my ($celvalue) = $sth->fetchrow ) {
312         my %cell;
313         if (defined $celvalue and $celvalue ne '') {
314             if($rowauthvals and $rowauthvals->{$celvalue}) {
315                 $cell{rowtitle} = $rowauthvals->{$celvalue};
316             } else {
317                 $cell{rowtitle} = $celvalue;
318             }
319             $cell{value} = $celvalue;
320         }
321         else {
322             $cell{rowtitle} = "NULL";
323             $cell{value} = "zzEMPTY";
324         }
325         $cell{totalrow} = 0;
326         push @loopline, \%cell;
327     }
328
329     # 2nd, loop cols.
330     my $origcolumn = $column;
331     $column =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
332     my $colfield;
333     if ( ( $column =~ /itemcallnumber/ ) and ($cotedigits) ) {
334         $colfield = "left($column,$cotedigits)";
335     } elsif ( $column =~ /^deleteditems\.timestamp$/ ) {
336         $colfield = "DATE($column)";
337     } else {
338         $colfield = $column;
339     }
340
341     my $strsth2 = "
342         SELECT distinctrow $colfield
343         FROM   $itemstable
344         LEFT JOIN biblioitems
345             USING (biblioitemnumber)
346         LEFT JOIN biblio
347             ON (biblioitems.biblionumber = biblio.biblionumber)
348         WHERE 1 ";
349     $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
350
351     if ( (@colfilter) and ( $colfilter[1] ) ) {
352         $strsth2 .= " AND $column >= ? AND $column <= ?";
353     } elsif ( defined $colfilter[0] and $colfilter[0] ne '' ) {
354         $colfilter[0] =~ s/\*/%/g;
355         $strsth2 .= " AND $column LIKE ? ";
356     }
357     $strsth2 .= " ORDER BY $colfield";
358     $debug and print STDERR "SQL: $strsth2";
359     my $sth2 = $dbh->prepare($strsth2);
360     if ( $barcodefilter and (@colfilter) and ( $colfilter[1] ) ) {
361         $sth2->execute( $barcodefilter, $colfilter[0], $colfilter[1] );
362     } elsif ( (@colfilter) and ( $colfilter[1] ) ) {
363         $sth2->execute( $colfilter[0], $colfilter[1] );
364     } elsif ( $barcodefilter && $colfilter[0] ) {
365         $sth2->execute( $barcodefilter , $colfilter[0] );
366     } elsif ( $colfilter[0]) {
367         $sth2->execute( $colfilter[0] );
368     } elsif ($barcodefilter) {
369         $sth2->execute($barcodefilter);
370     } else {
371         $sth2->execute();
372     }
373     my $colauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origcolumn } ) };
374     while ( my ($celvalue) = $sth2->fetchrow ) {
375         my %cell;
376         if (defined $celvalue and $celvalue ne '') {
377             if($colauthvals and $colauthvals->{$celvalue}) {
378                 $cell{coltitle} = $colauthvals->{$celvalue};
379             } else {
380                 $cell{coltitle} = $celvalue;
381             }
382             $cell{value} = $celvalue;
383         }
384         else {
385             $cell{coltitle} = "NULL";
386             $cell{value} = "zzEMPTY";
387         }
388         $cell{totalcol} = 0;
389         push @loopcol, \%cell;
390     }
391
392     my $i = 0;
393     my $hilighted = -1;
394
395     #Initialization of cell values.....
396     my %table;
397
398     foreach my $row (@loopline) {
399         foreach my $col (@loopcol) {
400             $table{ $row->{value} }->{ $col->{value} } = 0;
401         }
402         $table{ $row->{value} }->{totalrow} = 0;
403     }
404
405     # preparing calculation
406     my $select_cellvalue = " COUNT(*) ";
407     $select_cellvalue = " COUNT(DISTINCT biblioitems.biblionumber) " if($cellvalue eq 'biblios');
408     my $strcalc = "
409         SELECT $linefield, $colfield, $select_cellvalue
410         FROM $itemstable
411         LEFT JOIN biblioitems ON ($itemstable.biblioitemnumber = biblioitems.biblioitemnumber)
412         LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
413         WHERE 1 ";
414
415     my @sqlargs;
416
417     if ($barcodefilter) {
418         $strcalc .= "AND barcode $not like ? ";
419         push @sqlargs, $barcodefilter;
420     }
421
422     if ( @$filters[0] ) {
423         $strcalc .= " AND $itemstable.itemcallnumber >= ? ";
424         @$filters[0] =~ s/\*/%/g;
425         push @sqlargs, @$filters[0];
426     }
427
428     if ( @$filters[1] ) {
429         $strcalc .= " AND $itemstable.itemcallnumber <= ? ";
430         @$filters[1] =~ s/\*/%/g;
431         push @sqlargs, @$filters[1];
432     }
433
434     if ( @$filters[2] ) {
435         $strcalc .= " AND " . ( C4::Context->preference('item-level_itypes') ? "$itemstable.itype" : 'biblioitems.itemtype' ) . " LIKE ? ";
436         @$filters[2] =~ s/\*/%/g;
437         push @sqlargs, @$filters[2];
438     }
439
440     if ( @$filters[3] ) {
441         $strcalc .= " AND biblioitems.publishercode LIKE ? ";
442         @$filters[3] =~ s/\*/%/g;
443         @$filters[3] .= "%" unless @$filters[3] =~ /%/;
444         push @sqlargs, @$filters[3];
445     }
446     if ( @$filters[4] ) {
447         $strcalc .= " AND " .
448         (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
449         . "> ? ";
450         @$filters[4] =~ s/\*/%/g;
451         push @sqlargs, @$filters[4];
452     }
453     if ( @$filters[5] ) {
454         @$filters[5] =~ s/\*/%/g;
455         $strcalc .= " AND " .
456         (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
457         . "< ? ";
458         push @sqlargs, @$filters[5];
459     }
460     if ( @$filters[6] ) {
461         $strcalc .= " AND $itemstable.homebranch LIKE ? ";
462         @$filters[6] =~ s/\*/%/g;
463         push @sqlargs, @$filters[6];
464     }
465     if ( @$filters[7] ) {
466         $strcalc .= " AND $itemstable.location LIKE ? ";
467         @$filters[7] =~ s/\*/%/g;
468         push @sqlargs, @$filters[7];
469     }
470     if ( @$filters[8] ) {
471         $strcalc .= " AND $itemstable.ccode  LIKE ? ";
472         @$filters[8] =~ s/\*/%/g;
473         push @sqlargs, @$filters[8];
474     }
475     if ( defined @$filters[9] and @$filters[9] ne '' ) {
476         $strcalc .= " AND $itemstable.notforloan  LIKE ? ";
477         @$filters[9] =~ s/\*/%/g;
478         push @sqlargs, @$filters[9];
479     }
480     if ( defined @$filters[10] and @$filters[10] ne '' ) {
481         $strcalc .= " AND $itemstable.materials  LIKE ? ";
482         @$filters[10] =~ s/\*/%/g;
483         push @sqlargs, @$filters[10];
484     }
485     if ( @$filters[13] ) {
486         $strcalc .= " AND $itemstable.dateaccessioned >= ? ";
487         @$filters[13] =~ s/\*/%/g;
488         push @sqlargs, @$filters[13];
489     }
490     if ( @$filters[14] ) {
491         $strcalc .= " AND $itemstable.dateaccessioned <= ? ";
492         @$filters[14] =~ s/\*/%/g;
493         push @sqlargs, @$filters[14];
494     }
495     if ( $cellvalue eq 'deleteditems' and @$filters[15] ) {
496         $strcalc .= " AND DATE(deleteditems.timestamp) >= ? ";
497         @$filters[15] =~ s/\*/%/g;
498         push @sqlargs, @$filters[15];
499     }
500     if ( $cellvalue eq 'deleteditems' and @$filters[16] ) {
501         @$filters[16] =~ s/\*/%/g;
502         $strcalc .= " AND DATE(deleteditems.timestamp) <= ?";
503         push @sqlargs, @$filters[16];
504     }
505     $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
506     $debug and warn "SQL: $strcalc";
507     my $dbcalc = $dbh->prepare($strcalc);
508     $dbcalc->execute(@sqlargs);
509
510     while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
511
512         $col      = "zzEMPTY" if ( !defined($col) );
513         $row      = "zzEMPTY" if ( !defined($row) );
514
515         $table{$row}->{$col}     += $value;
516         $table{$row}->{totalrow} += $value;
517         $grantotal               += $value;
518     }
519
520     foreach my $row ( @loopline ) {
521         my @loopcell;
522
523         #@loopcol ensures the order for columns is common with column titles
524         # and the number matches the number of columns
525         foreach my $col (@loopcol) {
526             my $value = $table{$row->{value}}->{ $col->{value} };
527             push @loopcell, { value => $value };
528         }
529         push @looprow,
530           { 'rowtitle' => $row->{rowtitle},
531             'value'    => $row->{value},
532             'loopcell' => \@loopcell,
533             'hilighted' => ( $hilighted *= -1 > 0 ),
534             'totalrow' => $table{$row->{value}}->{totalrow}
535           };
536     }
537
538     foreach my $col (@loopcol) {
539         my $total = 0;
540         foreach my $row (@looprow) {
541             $total += $table{ $row->{value} }->{ $col->{value} };
542         }
543
544         push @loopfooter, { 'totalcol' => $total };
545     }
546
547     # the header of the table
548     $globalline{loopfilter} = \@loopfilter;
549
550     # the core of the table
551     $globalline{looprow} = \@looprow;
552     $globalline{loopcol} = \@loopcol;
553
554     # the foot (totals by borrower type)
555     $globalline{loopfooter} = \@loopfooter;
556     $globalline{total}      = $grantotal;
557     $globalline{line}       = $line;
558     $globalline{column}     = $column;
559     push @mainloop, \%globalline;
560     return \@mainloop;
561 }