Closed Thread Icon

Topic awaiting preservation: ...And on your left, a Perl subroutine... (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12280" title="Pages that link to Topic awaiting preservation: ...And on your left, a Perl subroutine... (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: ...And on your left, a Perl subroutine... <span class="small">(Page 1 of 1)</span>\

 
Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 06-23-2002 03:14

I know about using 'shift()', but

How do you get a return value from a sub in Perl?


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Piper
Paranoid (IV) Inmate

From: California
Insane since: Jun 2000

posted posted 06-23-2002 03:52

Here is a quick little sub that shows you how to return a value. It's pretty straight forward:

code:
sub parse_template {
my ($self, $file, @vars) = @_;

$file or return $self->error ('PAGEERR', 'WARN', '[empty]');
$file =~ /\.\./ and return $self->error ('PAGEERR', 'WARN', $file);
-e $file or return $self->error ('PAGEERR', 'WARN', $file);
-r _ or return $self->error ('PAGEERR', 'WARN', $file);

require GT::Template;
my $vars = $self->load_template_vars(@vars);

return GT::Template->parse($file, $vars, { escape => 1 });;
}



I'm returning an object in this one but you can return just about anything. I always try to return at least a "true" value, or a "false" if something broke, when I build subs. It makes the subs quite a bit more portable.

Regards,
Charlie

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-23-2002 03:55

What's nice about Perl is you can return a list of things...

sub mysub {
return (3,4,5);
}

my ($a,$b,$c) = &mysub();

(Apologies if I got the syntax wrong, Perl is weird and I don't use it much)

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 06-23-2002 04:02
code:
$subValue = returnScript();

sub returnScript {
return(1);
}



subVal = 1

There is also that slime mention

code:
($val1, $val2, $val3) = returnScript();

@vals = returnScript();

sub returnScript {
return(1,2,3);
}



val1 = 1
val2 = 2
val3 = 3

vals[0] = 1
vals[1] = 2
vals[2] = 3

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 06-23-2002 04:20

so, you're saying is- all I need is the 'return();' function?


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-23-2002 06:31

Well, yes. But technically speaking, return isn't a function. I'm tempted to say it's an operator, but I'm not sure, so I'm going to look it up...

Yup, it's an operator. Don't be fooled by the fact that I put parenthesis after it. The parenthesis in that case were a container for a list. The list was being returned.

return 4;

is a completely valid statement.

(For the record, while loops aren't functions either, they're a "control structure". Same for if blocks and for loops and some other things.)

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 06-23-2002 06:41

note the return statement is not even always needed, if not explictly called the sub will automatically return the value of the last statement.
try this.

sub no_return {
my $ret = "This is what is returned";
}
print no_return();





.:[ Never resist a perfect moment ]:.

Rahly
Bipolar (III) Inmate

From: Michigan
Insane since: Jul 2002

posted posted 07-04-2002 21:56

ok but subroutines do not need parenthsis either.

for example

code:
sub this {
my ($x, $y) = @_;
print "$x, $y\n";
return 5, 6, 7;
};

my @x = this "that", "this";
foreach my $b (@x) {
print "$b\n";
};



produces the results

that, this
5
6
7

also.. you can specify a subroutine as

sub this {
"that";
};

and when you call this... you get that back.... this is how perl makes constants in the packages.

Rahly

[This message has been edited by Petskull (edited 07-07-2003).]

« BackwardsOnwards »

Show Forum Drop Down Menu