You are very close! First enable run time warnings (the -w flag) and use strict (first 2 lines of code below). There is no sub called main{} in a Perl program this is not Java. I wouldn't put these checks for undef input parameters. The runtime warnings will show error if that happens. Basically have have the right idea and your code runs!
#!/usr/bin/perl -w
use strict;
my @broken = (1,2,3);
my $location = "/home/user/";
find_broken_links( $location, \@broken );
foreach my $var (@broken)
{
print "Returned: $var\n";
}
sub find_broken_links
{
my ($location, $broken_links) = @_;
# other code not shown here
my $var ='X';
push @$broken_links, $var;
}
#prints:
#Returned: 1
#Returned: 2
#Returned: 3
#Returned: X
Update:I see some other posts while I was writing mine. The "stringification" as AnomalousMonk points out is a problem. The code I
wrote just automatically didn't have that condition and so it worked. I would caution against this type of "silent program error". These sub input params will be undefined if there is an error in the calling program. If that is the case, you want to know about it! Otherwise your code would just simply return with apparently no broken links found, which probably is not what you want to have happen!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.