David David - 10 months ago 56
Perl Question

perl subroutine output appending "1"

So after ditching PHP for Perl and Linux for FreeBSD i'm now 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.


#!/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;


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" }


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;


<table border="1" id="container">
<td id="main_content">
<li onclick="showmenu(this)">!<b>PROFILE</b>
<li><a href="#">Modify</a></li>
<li> Delete</li>
<li> Lock</li>

<td id="main_content" class="opacity_box" >

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


Any solution to the issue? cheers

Answer Source

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


(lose the & it's bad style)