CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
subroutine return values



Jan 4, 2005, 3:59 AM

Post #1 of 2 (354 views)
subroutine return values Can't Post

Im having trouble understanding how to return and save values from one subroutine and then parse them into another subroutine.
Below is my program for my simple shoppingcart. The parts im having problems with are in bold.
I just can't seem to get it too work.

I apologise for the size and uglyness of my code, but u dont need to read it all anyway. :)

Any help will be greatly appreciated.

#!/usr/bin/perl -wT 
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;

print header();
print start_html("View Cart");

my $cookie_id = &validate_cookie;

#Display the shopping cart


#need to parse $nmgivetotal varibale from display_shopcart subroutine
#into free_stuff subroutine.

#need to parse $nmgivetotal, $mgivetotal, $msubtotal and $nmsubtotal
#from display_shopcart subroutine into totals subroutine.

print end_html;

sub display_shopcart {

my($cookie_id) = @_;

my $msubtotal = 0;
my $nmsubtotal = 0;
my $mgivetotal = 0;
my $nmgivetotal = 0;

print <<HTML;
<h2><font face='Arial'>Your Shopping Cart ($cookie_id)</font></h2>
<p><font face='Arial' size=2>*All prices are in Australian dollars</font></p>
<form action="edcart.cgi" method="POST">

print qq(
<table border=0 width=65% cellSpacing=0 cellPadding=2>
<th bgcolor="#99ccff" align="LEFT"><font face="Arial" size=2>Product Name</font></th>
<th bgcolor="#99ccff" align="LEFT"><font face="Arial" size=2>Type</font></th>
<th bgcolor="#99ccff"><font face="Arial" size=2>Qty</font></th>
<th bgcolor="#99ccff"><font face="Arial" size=2>Member's Price</font></th>
<th bgcolor="#99ccff"><font face="Arial" size=2>Non-Member's Price</font></th>

open FILE, 'cookiecart.txt' or die $!;
while (<FILE>) {
my @data = split(/\|/);
next if $data[0] ne $cookie_id;

open LIST, 'converter.txt' or die $!;
while (<LIST>) {

my @productlist = split(/\|/);
if ($data[1] eq $productlist[0]) {
$msubtotal += $productlist[3] * $data[2];
$nmsubtotal += $productlist[2] * $data[2];

my $mgiveprice;
my $nmgiveprice;
if ($productlist[17] eq "1") {
$mgiveprice = 0;
$nmgiveprice = 0;
} else {
$mgiveprice = $productlist[3];
$nmgiveprice = $productlist[2];

$mgivetotal += $mgiveprice * $data[2];
$nmgivetotal += $nmgiveprice * $data[2];

if ($productlist[2] eq "") {
$productlist[2] = "Not available for non-members";
} else {
$productlist[2] = "\$$productlist[2]";

my $flag;

my $bgcolour = $flag ? '#FFF8B7' : '#FFFBD5';

print qq(
<td bgcolor="$bgcolour" align="LEFT"><font face="Arial" size=2>$productlist[4]</font></td>
<td bgcolor="$bgcolour" align="LEFT"><font face="Arial" size=2>$productlist[1]</font></td>
<td bgcolor="$bgcolour" align="CENTER"><input type='text' name='$data[1]' size=1 value='$data[2]' style="text-align: Center"></td>
<td bgcolor="$bgcolour" align="CENTER"><font face="Arial" size=2>\$$productlist[3] ($mgiveprice)</font></td>
<td bgcolor="$bgcolour" align="CENTER"><font face="Arial" size=2>$productlist[2] ($nmgiveprice)</font></td>


$flag = !$flag;

return $nmgivetotal, $mgivetotal, $msubtotal, $nmsubtotal;

sub free_stuff {

my ($nmgivetotal) = @_;

my $freebee;

open FILE, 'giveaway.txt' or die $!;
while (<FILE>) {
my @giveaway = split(/\|/);
if ($nmgivetotal <= 0) {
$freebee = "";
elsif ($nmgivetotal >= $giveaway[0] && $nmgivetotal <= $giveaway[1]) {
$freebee .= qq|<tr>
<td align="LEFT" bgcolor="#FFFF66"><font face="Arial" size=2>$giveaway[2]</font></td>
<td align="CENTER" bgcolor="#FFFF66"></td>
<td align="CENTER" bgcolor="#FFFF66"><font face="Arial" size=2>1</font></td>
<td align="CENTER" bgcolor="#FFFF66"><font face="Arial" size=2>Free</font></td>
<td align="CENTER" bgcolor="#FFFF66"><font face="Arial" size=2>Free</font></td>


print qq~$freebee~;


sub totals {

my ($nmgivetotal, $mgivetotal, $msubtotal, $nmsubtotal) = @_;

$msubtotal = sprintf("%4.2f", $msubtotal);
$nmsubtotal = sprintf("%4.2f", $nmsubtotal);

$mgivetotal = sprintf("%4.2f", $mgivetotal);
$nmgivetotal = sprintf("%4.2f", $nmgivetotal);

print qq(
<td align="CENTER" bgcolor="#99ccff"><font face="Arial" size=2><b>Subtotal:</b></font></td>
<td align="CENTER" bgcolor="#99ccff"></td>
<td align="CENTER" bgcolor="#99ccff"><font face="Arial" size=2>\$$msubtotal ($mgivetotal)</font></td>
<td align="CENTER" bgcolor="#99ccff"><font face="Arial" size=2>\$$nmsubtotal ($nmgivetotal)</font></td>

<p><input type="submit" value="Update Qty">
<a href="checkout.cgi">Checkout</a></p>



sub validate_cookie {
# Look for cookies. If they have a valid cookie, return it; if not,
# print an error message and abort.
my $cookie_id = "";
if (cookie('MyShop')) {
$cookie_id = cookie('MyShop');
} else {
&dienice("You don't have a cart. (Perhaps your cart expired?)");
return $cookie_id;

sub dienice {
my($errmsg) = @_;
print "<h2>Error</h2>\n";
print "<p>$errmsg</p>\n";
print end_html;

(This post was edited by inlimbo on Jan 4, 2005, 4:01 AM)


Jan 4, 2005, 11:11 AM

Post #2 of 2 (349 views)
Re: [inlimbo] subroutine return values [In reply to] Can't Post

define the variables you want returned from &display_shopcart sub routine:

my ($nmgivetotal, $mgivetotal, $msubtotal, $nmsubtotal) = &display_shopcart($cookie_id);

now you can use them in other sub routines by passing them in the system array:


and retrieving them the same way in your subs like you are already doing:

sub free_stuff {

my ($nmgivetotal) = @_;

you can send a list of scalars to a sub routine:


and retrieve the list of scalars

do_this sub {
my ($foo,$bar,$blah) = @_;

(This post was edited by KevinR on Jan 4, 2005, 11:15 AM)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives