I wrote a program a long time ago, before I discovered the virtues of strict and warnings. It was designed only for use on the command line. For nice looking output I created a subroutine Log() that accepted the string of log information and printed it to stdout with a nice looking time and added the . "\n" at the end.

Recently I converted the program to Mojolicious so it could be used by others in a web browser. I still wanted that log information to be displayed to the user, so I created a $logline variable in the main code that Log() appended to. Each route cleared it, did its thing (calling subroutines that also called Log()), and then put it in the stash to be displayed with the page. It worked very well.

The problem came when I decided it was long overdue to add strict and warnings to the program. Most of the changes I had to make were minor but I can't figure out a good way to incorporate this $logline. Many of the subroutines call other subroutines, so it seems like the only way to do this is to pass it into, and return it from, every subroutine. That seems incredibly cumbersome, modifying each subroutine and every call to it, especially since these subroutines have other data that they need to accept and pass back. I've ended up with subroutines that don't work anymore and a $logline that works sometimes and not others.

I have also tried to declare it as a global variable, but can't get that to work:

global $logLine;

error message is

Global symbol "$logLine" requires explicit package name

Is there a better way to do this? What am I missing?


In reply to Logging to web page with Mojolicious and strict by TieUpYourCamel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.