Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Logging to web page with Mojolicious and strict

by hippo (Bishop)
on Sep 12, 2019 at 14:43 UTC ( [id://11106069]=note: print w/replies, xml ) Need Help??


in reply to Logging to web page with Mojolicious and strict

Probably all you need is to declare it with my at the outermost scope of your code.

#!/usr/bin/env perl use strict; use warnings; my $logline; foo (); sub foo { $logline = 'Now we have called foo'; bar (); } sub bar { Log (); } sub Log { print "Logging: $logline\n"; }

If this does not work for you for some reason, an SSCCE would help to pin down the problem.

Replies are listed 'Best First'.
Re^2: Logging to web page with Mojolicious and strict
by TieUpYourCamel (Scribe) on Sep 12, 2019 at 15:37 UTC
    Thanks for your suggestions. Unfortunately I get the same error. Here's a SSCCE. Hopefully this helps.

    index.mojo:

    #!/usr/bin/perl use Mojolicious::Lite; my $logLine; require './bigFileFullOfSubs.pl'; require './subImWorkingOn.pl'; any '/' => sub { my $c = shift; $logLine = ""; subImWorkingOn(); $c->stash(logLine => $logLine); $c->render(template => 'main'); }; app->start; __DATA__ @@ main.html.ep <!DOCTYPE html> <html><head></head> <body> <pre> <%= $logLine %> </pre> </body> </html>
    BigFileFullOfSubs.pl:

    #!/usr/bin/perl -w use strict; use warnings; sub Log { # Generates nice looking log comment with time. my ($LogItem) = @_; $LogItem =~ s/\n/ /g; my $Time = "functionThatGetsTheTime()"; my $thisLog = "[ $Time ] :: $LogItem\n"; $logLine .= $thisLog; } return 1;
    SubImWorkingOn.pl:

    use warnings; use strict; sub subImWorkingOn { Log("Interesting information for the user here."); Log("Even more fascinating information for the user here!"); Log("Processing complete"); } return 1;
    Error message:

    Global symbol "$logLine" requires explicit package name (did you forg +et to declare "my $logLine"?) at ./bigFileFullOfSubs.pl line 11.

      As the error message suggests, your BigFileFullOfSubs.pl won't compile because you have not declared $logLine in it.

      One solution would be to share this variable among your scripts but that would get very messy very quickly. However, since you don't actually need $logLine anywhere else, just move the declaration of it out of index.mojo and into BigFileFullOfSubs.pl (outside the subroutine) and you may find this solves your problems.

      Perhaps, and I mean this with all sincerity, it's also long overdue that you convert the Perl-4-style "libraries" to real modules.

      In either case, just store the variable in the library/module that is creating it, and have it provide a subroutine to access it for the template.

        Point well taken. I've started in on that, I'm about halfway through.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11106069]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-28 10:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found