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

Hi, I have the following code:

use Archive::Zip qw(:ERROR_CODES :CONSTANTS); my $zip = Archive::Zip->new(); $fileToAdd = '12345678.pdf'; # ...etc... # ...etc... # ...etc... $fileWithPath = "$viewBillPath\\$fileToAdd"; $zip->addFile($fileWithPath) or die "couldnt add $viewBillPath\\$fileT +oAdd\n";
I keep getting the message "Can't call method "addFile" on an undefined value at postprocCDR.pl" etc... at the exact line that the addFile method is called on $zip

I have tested the path/filename that I am adding to the zip archive, and it is correct every time (i copy the path/filename that is output, paste it into Windows Explorer and it launches the correct application).

Does anyone know why I keep getting this "Can't call method addFile" error?

Your response is appreciated,

Brent.

janitored by ybiC: Balanced <code> tags around codeblock, minor format tweaks for legibility

Replies are listed 'Best First'.
Re: $zip->addFile not working.
by arden (Curate) on Jun 04, 2004 at 22:38 UTC
    change the last line to $zip->addFile($fileWithPath) != AZ_OK || die "couldn't add $fileWithPath\n";

    The single quotes are preventing the variable from being interpretted. Also, the author uses the AZ_OK constants for status. . .

    - - arden.

    update: Of course, I meant in your code, not in your post! Also added the AZ_OK info.
    update2: As discovered in the CB, you must keep your $zip variable in scope when trying to execute methods on it! :)

      Hi Arden, I just took out the single quotes and it still isn't working. Any other ideas?
Re: $zip->addFile not working.
by ChemBoy (Priest) on Jun 04, 2004 at 22:50 UTC

    The error message indicates that $zip, not $fileWithPath, is your problem (that being the object you are attempting to call the method on, while $fileWithPath is the parameter you're trying to pass to the method). Without looking at the Archive::Zip documentation, I'd suggest changing your earlier code to

    my $zip = Archive::Zip->new() or die "Unable to create archive: $@\n"

    which should tell you what went wrong, assuming Archive::Zip::new is kind enough to set the error message when it fails.



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

Re: $zip->addFile not working.
by Old_Gray_Bear (Bishop) on Jun 04, 2004 at 23:55 UTC
    I reduced your code down to this
    #!/usr/bin/perl use strict; use warnings; use Archive::Zip qw(:ERROR_CODES :CONSTANTS); my $zip = Archive::Zip->new(); my $fileToAdd = '12345678.pdf'; my $viewBillPath = 'a_path'; my $fileWithPath = "$viewBillPath\\$fileToAdd"; $zip->addFile($fileWithPath) or die "couldn't add $viewBillPath\\$file +ToAdd\n";
    and then stepped through it with the debugger. I found the addFile method successfully, stepped into it and all of its friends. Eventually the code did the right thing (it threw an error because it couldn't find the file) and exited. I am betting that the elided code does something to $zip that causes it to lose it blessed-ness.

    At this point you have a couple of options:

    • Fire up the debugger and tromp through the code
    • Insert print statements liberally -- dump the ref($zip) and some identifying locational data -- and determine what is zapping $zip.
    Either process will require you to more critically examine your code (to figure out where the next break-point or print-stmt goes). When I do this kind of analysis (usually) I end up with an "Aha! That's where it goes wrong" moment, and find my bug.

    Good Hunting

    ----
    I Go Back to Sleep, Now.

    OGB

Re: $zip->addFile not working.
by tadamec (Beadle) on Jun 04, 2004 at 22:53 UTC

    It looks like $zip isn't being created.

    Try changing:

     my $zip = Archive::Zip->new();
    to:
    my $zip; eval { $zip=Archive::Zip->new(); } or die $@

    That should provide an error message if Archive::Zip is running into problems.

    Update: $! is the os_error flag. My bad. Changed to eval{} as suggested by jZed.

      Hi Tadamec, I tried this already but I can tell by my personal log file that it executes this statement o.k. and keeps on processing. Any other ideas?
Re: $zip->addFile not working.
by Roy Johnson (Monsignor) on Jun 04, 2004 at 23:50 UTC
    This is not the cause of your problem, but you should note that it is recommended that you use forward slashes rather than backslashes for paths within Perl, even on Windows boxes.

    The PerlMonk tr/// Advocate