Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning a value with a subroutine?

Status
Not open for further replies.

yim11

MIS
Jun 26, 2000
35
0
0
US
Hello!
I have a small script that decodes a cuecat scan of a barcode. A barcode is swiped with the cuecat and the scripts returns the id number of the product.

What I am trying to do is write a script that will process the swipe from a webpage... for example:
a page is displayed with a textfield for the cuecat input (the swipe) then a submit button that will decode the input.

My efforts are posted below -
Thank you very much in advance for any and all assistance!
Jim

----Begin Code----
#!/usr/bin/perl
use CGI;
use CGI qw:)standard);
use CGI::Carp qw (fatalsToBrowser confess);

print header,
start_html('the CueCat page'),

start_form,
"Swipe Barcode: ",
textfield('text'),
p,
submit, reset,
end_form,
hr;

if (param()) {

# this begins the actual cuecat translation section
my @outputs;
my %translation = (C3 => 0, n => 0, Z => 0,
CN => 1, j => 1, Y => 1,
Cx => 2, f => 2, X => 2,
Ch => 3, b => 3, W => 3,
D3 => 4, D => 4, 3 => 4,
DN => 5, z => 5, 2 => 5,
Dx => 6, v => 6, 1 => 6,
Dh => 7, r => 7, 0 => 7,
E3 => 8, T => 8, 7 => 8,
EN => 9, P => 9, 6 => 9);
my %types = (cGf2 => 'ISBN', cGen => 'ISBN', fHmg => 'UPC', fGzX => 'UPC-E1');
while(@outputs) {

next if ($_ eq "\n");
unless (/^\.(.*)\.(....)\.(.*)\.$/) {
print "invalid code\n";
} else {
my $type = $2;
my $code = $3;
my $output = "";
while ($code =~ /^(..)(.?)(.?)(.*)/) {
$output .= $translation{$1} . $translation{$2} . $translation{$3};
$code = $4;
}
push (@outputs, (($types{$type}) ? "$types{$type} " : '') . "$output\n");
}
}
return (@outputs);
# this ends the actual cuecat translation section



print "Product ID is: ", @, hr;

}
print end_html;
----End Code-----
 
The subroutine syntax is as follows:

sub sub_name
{
my ($param1,$param2,etc.) = @_;

your code

return ($result1,$result2,etc.);
}

@results = sub_name(@params);
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Thank you very much for your reply!

I was thinking faster than I was typing when I posted :)

I was told that using a subroutine for the cuecat translation section would be my best bet, but wasnt sure how to do that.

It all seemed so simple when I started...
I had a small script that when ran would decode the cuecat scan - I thought "wow this would be great if it could be run from a webpage instead of from the command line - I'll just redirect this input and output..."

Well thats when The Eighth Networking Truth from RFC 1925 kicked in ("It is more complicated than you think").

In your opinion, is this possible, and if so - would a subroutine be my best bet?

Thanks again for everything!
Jim
 
I still don't know what your question is. I was guessing that you need the syntax for subroutines. Is that what you are asking for? Please try to formulate your question better.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
I apologize, its monday.

I have a script, called catscan.pl, that runs from command line. When run it waits for a barcode swipe from a Cuecat barcode reader. The untranslated scan looks like ".C3nZC3nZC3n3C3vZENv2CNnX.fHmc.CNnWDhj1C3nZC3bW." The translated scan would be "103716000033" - the products id code. All I want is to use this script from a webpage. In other words I would like to goto and see something to the effect of "Place cursor in this textarea and scan a barcode:____". The cuecat embeds a carrige return at the end of a scan so that would activate the submit button and the script would translate the scan and return the product id- in the webpage. (something like "the product ID is:____").

I know the translation script works, I just dont know how to change the input and output from command line to a webpage.


I hope I have explained my situation better.
Thank you for your time, help, and patience.
Jim
 
Ok, try something like this:

#!/usr/bin/perl -w -T

# Restrict unsafe constructs
use strict;

# CGI routines
use CGI_Lite;

my $cgi = new CGI_Lite;
my %in = $cgi->parse_form_data;

# get barcode scan from input
my $scan = $in{"SCAN"};

if ($scan)
{
my $translation = translate($scan);
print "Content-type: text/html\n\n";
print "Product ID is: $translation";
}
else {prompt();}

sub translate
{
my ($scan) = @_;
# do your translation code here with $scan as the input
return ($output_string);
}

sub prompt
{
print "Content-type: text/html\n\n";
print qq~
<html><body>
<form method=&quot;post&quot; action=&quot;scan.pl&quot;>
<input type=&quot;text&quot; name=&quot;SCAN&quot;><br>
<input type=&quot;submit&quot;>
</form>
</body></html>
~;
}
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Thank You Very Tom!!!

I _really_ appreciate all your help (again)!!

I'm working on doing translation code with $scan as the input now.

Hope to be good to go soon!

Again, THANK YOU very much!
Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top