Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Need some help with GD::Barcode::UPCE module

by vendion (Scribe)
on Oct 08, 2010 at 18:43 UTC ( [id://864263]=perlquestion: print w/replies, xml ) Need Help??

vendion has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks!
I am trying to write a Perl module that generates a UPCE barcode, I have the module and everything installed but when I try to test my code it prints out an error about an unblessed reference. I have never used bless, nor do I quite understand how to use it (yes I know that is bad). Here is the code that I am working with.

use GD::Barcode::UPCE; my $oGdBar = GD::Barcode->new( 'UPCE', $customer{id} ); if ( !$oGdBar ) { display_menu_error("ERROR: $GD::Barcode::errStr \n"); } print "PTN:", $oGdBar->{text}, ":", $oGdBar->{barcode}, "\n"; #FIXME Prints out an error regarding unblessed referance my $output = '/tmp/UPCE.png'; open my $OUTFILE, '>', $output; print {$OUTFILE} $oGdBar->plot->png; close {$OUTFILE}; undef $oGdBar; undef $OUTFILE; undef $output; system "lpr -r $output";

I tried to follow the example file that was included with the tarbal for the module. The $customer{id} variable contains a string in this form "000-000000".

Replies are listed 'Best First'.
Re: Need some help with GD::Barcode::UPCE module
by igelkott (Priest) on Oct 08, 2010 at 22:24 UTC
    Besides strict and warnings, here are some suggestions:
    • 000-000000 is outside UPCE's range.
    • change $oGdBar->{barcode} to $oGdBar->barcode()
    • change $oGdBar->{text} to $oGdBar->{$text} (as already suggested)
    • maybe use GD::Barcode::UPCE->new($customer{id}) rather than parent
    • maybe use $GD::Barcode::UPCE::errStr rather than parent

      Thanks for pointing out that limitation with UPCE, as the hyphen is needed in I am trying to do I have switched to the Code 39 code, as it supports symbols and does not require a checksum. I guess I should have done some research on the different kinds of barcodes.

      As for strict and warnings, yes I am already using them I guess I should have said that, the code I posted is just a small portion of the whole script. Only pasted the code that is giving me trouble

Re: Need some help with GD::Barcode::UPCE module
by Khen1950fx (Canon) on Oct 08, 2010 at 23:31 UTC
    This works for 8 digits:
    #!/usr/bin/perl use strict; use warnings; use GD::Barcode::UPCE; my $oGdBar; print "=======================\nUPCE: 1234568"; $oGdBar = GD::Barcode->new( 'UPCE', '1234568' ); print "PTN:", $oGdBar->{text}, ':', $oGdBar->barcode(), "\n"; open OUT, '>', 'UPCE.png'; binmode OUT; print OUT $oGdBar->plot->png; close OUT; undef $oGdBar;
Re: Need some help with GD::Barcode::UPCE module
by kcott (Archbishop) on Oct 08, 2010 at 20:02 UTC

    Please always include the actual error message with this type of question.

    One problem would appear to be a missing $ in $oGdBar->{text}. I think that should be $oGdBar->{$text}.

    Also, adding use strict; and use warnings; to the top of your code will highlight many problems.

    -- Ken

Re: Need some help with GD::Barcode::UPCE module
by vendion (Scribe) on Oct 10, 2010 at 17:34 UTC

    Ok so I have switched to Code39 barcodes and made the suggested changes to the code, which changes the error that now gets produced. I now get this:

    PTN:000-000000:1010001110111010101000111011101010100011101110101000101 +011101110101000111011101010100011101110101010001110111010101000111011 +101010100011101110101010001110111010 Can't locate object method "Small" via package "GD::Font" (perhaps you + forgot to load "GD::Font"?) at /usr/local/share/perl5/GD/Barcode/Cod +e39.pm line 111, <> line 19.

    Here is my new code:

    my $oGdBar = GD::Barcode::Code39->new("$customer{id}"); if ( !$oGdBar ) { display_menu_error("ERROR: $GD::Barcode::Code39::errStr \n"); } print "PTN:", $oGdBar->{text}, ":", $oGdBar->barcode(), "\n"; my $output = '/tmp/Code39.png'; open my $OUTFILE, '>', $output; binmode $OUTFILE; print {$OUTFILE} $oGdBar->plot->png; close {$OUTFILE}; undef $oGdBar; undef $OUTFILE; undef $output; system "lpr -r $output";

    As for the suggestions of changing $oGdBar->{text} to $oGdBar->{$text} does not seem to work. Does the error mean I should use GD::Font in my code or is it reffering to the module?

      The documentation for these modules is misleading. GD::Barcode::UPCE has $text $oGdBar->{$text} while GD::Barcode::Code39 has $text $oGdBar->{text}. Checking the source, $oGdBar->{text} would be correct for both modules.

      The message "Can't locate object method "some_method" via package "Some::Module" (perhaps you forgot to load "Some::Module"?) at ..." generally indicates someone wrote:

      $some_object->some_method(); # where $some_object is an instance of So +me::Module

      but should have written:

      use Some::Module; ... $some_object->some_method();

      I can't see a reference to the method Small() in your code but I can see two in the source code for GD::Barcode::Code39 in the plot() method.

      If I were you, I'd raise a bug report at this point. In case you don't know, just go to GD::Barcode::Code39 and follow the Report a bug link in the CPAN RT box (near the top right-hand corner in my browser).

      -- Ken

        Thanks, that is all I needed to know I have already opened a ticket in CPAN RT.

      I couldn't replicate your error. As for $oGdBar->{text}, that is the correct way to do it. Try this and see if it helps:
      #!/usr/bin/perl use strict; use warnings; use GD::Barcode::Code39; my $oGdBar = GD::Barcode->new( 'Code39', '*123-456789*' ); print "PTN:", $oGdBar->{text}, ':', $oGdBar->barcode(), "\n"; open OUT, '>', '/tmp/Code39.png'; binmode OUT; print OUT $oGdBar->plot->png; close OUT; undef $oGdBar;

        Even after changing my $oGdBar = GD::Barcode::Code39->new() to my $oGdBar = GD::Barcode->new() I still get the same error. I checked to make sure my GD::Barcode module is current and CPAN says I have version 1.15 installed

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://864263]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found