Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Help with proper construction of callable scalars from a Module, please.

by taint (Chaplain)
on Nov 13, 2013 at 02:21 UTC ( [id://1062297]=note: print w/replies, xml ) Need Help??


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

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;

Replies are listed 'Best First'.
Re^3: Help with proper construction of callable scalars from a Module, please.
by Anonymous Monk on Nov 13, 2013 at 12:52 UTC

    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().

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

Log In?
Username:
Password:

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

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

    No recent polls found