Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Help with proper construction of callable scalars from a Module, please.

by taint (Chaplain)
on Nov 13, 2013 at 01:30 UTC ( [id://1062288]=perlquestion: print w/replies, xml ) Need Help??

taint has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, Monks.

First let me apologize. I know this is a "brain dead" question. But it appears that someone has switched my Thinking cap, for a Dunce cap today. For the life of me, I'm just drawing blanks.

Anyway. What I'm trying to do is simply create a simple portal/CMS arrangement. Where I place all of the commonly (repetitious) used stuff in a module -- say; pageblocks.pm. Then I simply call all the stuff that is common to all pages from the Module. eg;

# pageblocks.pm package pageblocks; my $xmlheader=> print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; my $xmldtd=> print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"ht +tp://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en +\"> <head> <meta http-equiv=\"content-type\" content=\"application/xhtml+xml; + charset=utf-8\" />\n";
Then it's simply a matter of constructing a page thusly (index.cgi):
#!/usr/bin/perl -wT use strict; use lib ('./'); require pageblocks; print "content-type:text/html; charset=utf-8\n\n"; $xmlheader; $xmldtd; <title>This is a stupid attempt to re-invent the wheel</title> <meta name="description" content="What's worse -- I can't even figure +out the most simple part!!!" /> <meta name="keywords" content="yadda,yadda,yadda,..." /> </head> <body> ... </body></html>

While this actually works. In my gut, I know I'm doing it incorrectly.

Would anyone care to throw me a bone?
Again, apologies for the "bone headed" request, and thank you for your indulgence.

--Chris

#!/usr/bin/perl -Tw
use Perl::Always or die;
my $perl_version = (5.12.5);
print $perl_version;

Replies are listed 'Best First'.
Re: Help with proper construction of callable scalars from a Module, please.
by GrandFather (Saint) on Nov 13, 2013 at 01:49 UTC

    Define "actually works". Whatever you think the code is doing, that isn't what it is actually doing. A better approach is to use subs:

    use strict; use warnings; package Stuff; sub xmlheader { print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; } sub xmldtd { print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"ht +tp://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en +\"> <head> <meta http-equiv=\"content-type\" content=\"application/xhtml+xml; + charset=utf-8\" />\n"; } package main; print "content-type:text/html; charset=utf-8\n\n"; Stuff::xmlheader (); Stuff::xmldtd ();

    Note that adding strictures (use strict; use warnings; - see The strictures, according to Seuss) to your code would have turned up most of the bugs due to misunderstanding of what was going on.

    True laziness is hard work

      How's using subroutines that you have to close the string literal for better?

      The problem in the original code is that the variables are defines in the pageblocks package, but then attempts to access them in the main package.

      He should drop the my and use them as $pageblocks::xmlheader. And to prevent other code from modifying them, he might use Readonly.

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

      Thank you, GrandFather.

      I just came back to report that I finally sorted it out. Only to find your kind words of wisdom. :)

      This is what I ended up with (pageblocks.pm):

      use strict; package webblocks; 1; # LOADING THIS MODULE in index.cgi returns UNDEF || FALSE without t +his sub print_xmlheader { return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; } sub print_xmldtd { return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"h +ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en +\"> <head> <meta http-equiv=\"content-type\" content=\"application/xhtml+xml; + charset=utf-8\" />\n"; }
      ...and in index.cgi:
      print "content-type:text/html; charset=utf-8\n\n"; use lib ('./'); use pageblocks; print pageblocks::print_xmlheader(); print pageblocks::print_xmldtd(); ...

      I notice the version you posted retains my original print statements. Is there any reason I shouldn't use the return statements I've just chosen?

      "Define "actually works"." So noted. Thank you.

      --Chris

      #!/usr/bin/perl -Tw
      use Perl::Always or die;
      my $perl_version = (5.12.5);
      print $perl_version;

        If you are returning things instead of printing in a sub, then the name of your subs flatly lie about the purpose ...

        print pageblocks::print_xmlheader(); print pageblocks::print_xmldtd();

        ... and may make someone else doubt the purpose of print().

        P.S. I found the article: Including files the trick to get me back on track. Thank you Juerd.

        --Chris

        #!/usr/bin/perl -Tw
        use Perl::Always or die;
        my $perl_version = (5.12.5);
        print $perl_version;
        Is there any reason I shouldn't use the return statements I've just chosen?

        I think returning the data has the advantage that the caller can decide what to do with it: print it to STDOUT or any other file handle, write it to a database, change the character encoding or otherwise modify it, etc.

        If the sub prints it, you could add an optional filehandle argument so it can be redirected, and you could print it to a scalar, thereby, but, in my opinion only, this would be more complicated for, at best, very little advantage.

Re: Help with proper construction of callable scalars from a Module, please.
by ig (Vicar) on Nov 13, 2013 at 06:23 UTC

    Perhaps not the sort of bone you want to chew on, but I would use one of the many existing template systems, with or without one of the website/application platforms, rather than write my own.

      Good advice, and thank you ig.

      I hadn't actually intended to actually "re-invent the wheel". But rather, simply wanted to eliminate repeatedly re-typing the same lines, file, after file. So this idea seemed a simple solution -- combine all those blocks of (x)HTML in a Module. Then simply call them with short lines. In short; a great deal less typing.

      As to "one of the many existing template systems"
      I've yet to find one I actually like. But to each their own. :)

      Best wishes, and thanks for the honest reply.

      --Chris

      #!/usr/bin/perl -Tw
      use Perl::Always or die;
      my $perl_version = (5.12.5);
      print $perl_version;
        Revized edition:

        Seems in my initial frustration with accomplishing such a simple task. I ultimately overcomplicated matters. :(
        I think the following get's it right:
        pageblocks.pm

        use strict; package pageblocks; sub xmlheader { qq(<?xml version="1.0" encoding="UTF-8"?>\n); } sub xmldtd { qq(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http: +//www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="application/xhtml+xml; ch +arset=utf-8" />\n); } 1; # polygraph
        and index.cgi
        #!/usr/bin/perl -wT # index.cgi print "content-type:text/html; charset=utf-8\n\n"; use strict; use lib ('./'); use pageblocks; print pageblocks::xmlheader(); print pageblocks::xmldtd(); print "<body> ... </body></html>";

        A big thanks to GrandFather, Jenda, and Anonymous Monk, for their thoughtful feedback.

        --Chris

        #!/usr/bin/perl -Tw
        use Perl::Always or die;
        my $perl_version = (5.12.5);
        print $perl_version;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-24 06:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found