×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Returning a value with a subroutine?

Returning a value with a subroutine?

Returning a value with a subroutine?

(OP)
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-----

RE: Returning a value with a subroutine?

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.
http://www.oac-design.com

RE: Returning a value with a subroutine?

(OP)
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

RE: Returning a value with a subroutine?

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.
http://www.oac-design.com

RE: Returning a value with a subroutine?

(OP)
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 www.myserver.com/cgi-bin/scan.pl 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

RE: Returning a value with a subroutine?

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="post" action="scan.pl">
    <input type="text" name="SCAN"><br>
    <input type="submit">
    </form>
    </body></html>
  ~;
}


Sincerely,
 
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: Returning a value with a subroutine?

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close