Wiggins d Anconia said: >> Hi all, >> >> I have this: >> >> ----------------- >> #!/usr/bin/perl >> >> use warnings; >> use strict; >> > > Good start.
Courtesy of the Lama :-) >> my @pics = <*.jpg>; >> my $website = 'http://www.website.com/uploads'; >> >> sub links { >> foreach (@pics) { >> print "<a href=\"$website/$_\">$website/$_</a>\n"; >> } >> } > > Your sub breaks encapsulation because it relies on the fact that @pics and $website are available in the sub but have not been passed into it. Generally we want our subs to take arguments and return values. If you were to move this sub to the top of the file or to another file (such as a library/module) it would break. So the first thing you should correct is the encapsulation by making your sub accept its arguments and return a value. > > sub links { > my ($base_url, @list) = @_; > foreach my $element (@list) { > print ..... > } > return; > } Doesn't this still get the scalar and array from what I defined at the top? Also, why do you assign them to @_, can't we use that when iterating @list? Why wouldn't it accept it's arguments? >> ----------------- >> >> I would normally call the subroutine like &links; but I want to put the result into a variable in order to stick it into an e-mail. I have been trying all day reading all my perl books and it's time to ask for help. >> > > Note that we don't want to call C<&links> that is better written as C<links()>. > > What is the result? In your case you are printing the links, so there is no real result. That is what I want, but I can't figure how to but the list into the e-mail message. > You need to decide what the return value of the sub > will be, maybe the list of urls? A list of urls. > a bool? undef for positive or an > exception for failure? In the case of the list you should be C<push>ing the values to list and then returning the list. In this last case we can call the sub such as, > > my @ahrefs = links($website, @pics); I get this part. > foreach my $link (@ahrefs) { > print "$link<br />\n"; > } Isn't the above the same as my subroutine? > > For instance. > >> Thanks. >> > > Give it a shot, see if this helps, if not come back and ask more. Much appreciated. I will be re-reading Ch2-4 of the Lama tonight. -- Just getting into the best language ever... Fancy a [EMAIL PROTECTED] Just ask!!! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>