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

#!/usr/bin/perl print("Content-type: text/html\n\n"); push @INC, "."; require "tconfig.pl"; require "tparse.pl"; use RandTexT; use NewTitle1; use NewMeta; use MakeLink; $file = "keywords/keywordfile.txt"; open(INF,"$file") ; @grok = <INF>; close(INF); foreach $i (@grok) { $title =NewTitle1->titler($i); $meta = NewMeta->newDesc($i); $keyword = $i; $keyword2 = "the 2nd keyword"; $keyword3 = "the 3rd keyword"; @links = MakeLink->exportLinks($i);
And then I have, in each of the relevant modules, to capture the $i variable that is passed, and return to this script (of which is is the relevant section) the value created around the keyword. I'm new here, and I've been struggling with perl for a while, go easy :)

Replies are listed 'Best First'.
Re: using perl variable in muliple modules
by jryan (Vicar) on Dec 23, 2001 at 07:15 UTC

    Object methods are just like normal functions; they can use return to return a value. For instance, lets take a look at exportLinks:

    sub exportLinks { my ($self, $i) = @_; my @my_links = { ... }; # do your thang here return @my_links; }

    That will return @my_links back to your main code, and assign to @links its value.

    On unrelated notes, the type of functionality you want can be found in HTML::Parser and HTML::LinkExtor. Also, use strict and -w. :)

Re: using perl variable in muliple modules
by mstone (Deacon) on Dec 24, 2001 at 05:35 UTC

    It sounds like you're asking how make functions from other namespaces catch input parameters, yes?

    The answer is: same way you do it in a regular function. Perl passes all input parameters in the array @_, and automagically shuffles @_ from namespace to namespace when you call an object method. Your modules will probably end up looking something like this:

    package NewTitle1; sub titler { my $input_value = shift; my $title = ## munge $input_value into a title return ($title); } package NewMeta; sub newDesc { my $input_value = shift; my $meta = ## munge $input_value into a meta tag return ($meta); } package MakeLink; sub exportLinks { my $input_string = shift; my @links = ## munge $input_value into a list of links return ($meta); }
      thanks for the help :) okay, another related question:
      ($self, $keyword, $keyword2, $keyword3, $keyword4, $keyword5, $keyword +6, $domain) = (@_);
      That's how I'm getting a series of values into a module, which seems to be working...however, when I parse those with this:
      $sub1 = $keyword; $sub2 = $keyword2; $sub3 = $keyword3; $sub4 = $keyword4; $sub5 = $keyword5; $sub6 = $keyword6; $noun1 = &makeNlink($sub1, $domain); $noun2 = &makeNlink($sub2, $domain); $noun3 = &makeNlink($sub3, $domain); $noun4 = &makeNlink($sub4, $domain); $noun5 = &makeNlink($sub5, $domain); $noun6 = &makeNlink($sub6, $domain);
      and then this is the sub &makeNlink
      sub makeNlink { #($tobelink, $tobelinkurl) = @_; $tobelink = shift(@_); $tobelinkurl = shift(@_); $withspaces = $tobelink; $tobelink =~ s/\s/_/g; chomp($withspaces); chomp($tobelink); $totallyscrewed = "<a href \= \"http\:\/\/$domain\/$tobelink\" +>$withspaces<\/a>"; return $totallyscrewed; }
      The values are used over and over, the same series each time, when in the main script they are totally different. How do I make sure the current value of each variable is imported into this module? Thanks very, very much for the help! This place rocks.
        I suspect from your question that you are having problems with overwriting the value of your variables. If so then what you need to do is stop using so many global variables. I strongly recommend reading Coping With Scoping to understand how to avoid having variables trample all over each other, and then strict.pm to find out how to let Perl give you feedback to avoid mistakes.