David
David

Reputation: 45

perl subroutine output appending "1"

I'm playing with sub routines. It's all working as it should, but now I'm getting a random returned value of: 1 at the bottom of the page when I call the sub admin_view.

Home.pl

#!/usr/bin/env -w perl
no warnings 'experimental::smartmatch';

use Cwd qw(abs_path);
use feature qw(switch);
use CGI::Simple;
use HTML::Template;

my $cgi = CGI::Simple->new;
my $action = $cgi->url_param('cmd') || ''; #Set to: URL/?cmd=
my $mode = $ARGV[0]; #accept entry from terminal
print $cgi->header; #make html appear

if($action eq "") {
    #if URL/?cmd=
    $mode = "home";
}

if($action eq "admin_view") {
    #if URL/?cmd=admin_view
    $mode = admin_view;
}

given($mode){

when(admin_view) {
#html: Admin Mode & Display posts
    use rawr::template qw(admin_view);
    print &admin_view;
}

when(home) {
    print "Some home page";
}

##Default message
  default { print "mode not implemented" }
}

Template.pm

package rawr::template;
#template stuff

use strict;
use warnings;
use Exporter qw(import);
use HTML::Template;

our @EXPORT_OK = qw(admin_view);

sub admin_view {
# open the html template
##Yuck hardcoded but ok, works for now. some reason ../tmpl/admin_view.tmpl doesn't work
my $template = HTML::Template->new(filename => '/srv/mydomain/www/rawr/tmpl/admin_view.tmpl');
# fill some parameters
   $template->param(ALIAS => "Some User");
   $template->param(CONTENT => "Hardcoded Content");
   $template->param(DATE => "Date - change me");
   $template->param(TITLE => "Hardcoded - change me");

# send Content-Type and print the template output
   print "Content-Type: text/html\n\n", $template->output;
}
1;

admin_view.tmpl

    <table border="1" id="container">
            <tr>
                    <td id="main_content">
                    <ul>
                    <li onclick="showmenu(this)">!<b>PROFILE</b>
                    <ul>
                            <li><a href="#">Modify</a></li>
                            <li> Delete</li>
                            <li> Lock</li>
                            <br>
                    </ul>
                    </li>
                    <br>
                            <TMPL_VAR NAME=ALIAS>
                    <br>
                    <br>
                            <TMPL_VAR NAME=DSP>
                    <br>
                </td>


<td id="main_content" class="opacity_box" >
<b><TMPL_VAR NAME=TITLE></b> <TMPL_VAR NAME=DATE>
            <br>
            <br>
                             <TMPL_VAR NAME=CONTENT>
            <br>
</td>
</tr>
</table>

The template outputs fine, but at the bottom of the page I get a random output of 1 after the last </table> tag

</table>1

Any solution to the issue? cheers

Upvotes: 4

Views: 101

Answers (1)

Sobrique
Sobrique

Reputation: 53478

Your 'admin_view' sub does the print.

Thus you don't need to call it like this:

print &admin_view;

Because by doing so, it's printing the sub's return code. Which is the result of the last command without an explicit return, so shows us that print was successful.

Try just

admin_view;

(lose the & it's bad style)

Upvotes: 7

Related Questions