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

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
  • Comment on Re: Help with proper construction of callable scalars from a Module, please.
  • Download Code

Replies are listed 'Best First'.
Re^2: Help with proper construction of callable scalars from a Module, please.
by Jenda (Abbot) on Nov 13, 2013 at 14:18 UTC

    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.

Re^2: Help with proper construction of callable scalars from a Module, please.
by taint (Chaplain) on Nov 13, 2013 at 02:21 UTC
    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.

        Thank you for the thorough evaluation ig.
        That's what I was wondering. I've since updated it (at the bottom of this thread).

        Best wishes, and thanks again.

        --Chris

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