Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Invalid use of constant pragma?

by dhable (Monk)
on Nov 29, 2001 at 01:30 UTC ( [id://128195]=perlquestion: print w/replies, xml ) Need Help??

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

I've been working on a script that accepts some command line parameters from the user. The first few lines in the program setup the constants I want to use in the rest of the program.
# Because of my C++ background use constant PROJECT => $ARGV[0]; use constant ARCHIVE => $ARGV[1]; # debuggging print "PROJECT value = " . PROJECT . "\n"; print "ARCHIVE value = " . ARCHIVE . "\n";
Later on in the program, I want to make a decision about the parameters, so I created the following if statment:
if( ARCHIVE eq "") { warn( "No archive file was supplied.\n"); exit 1201; }
Without specifying an ARCHIVE name, I would expect the script to die. It doesn't. So I started reading my trust Camel book on use constant. It said that constant is evaluated at compile time. That makes a lot of sense but my debug output shows:
PROJECT value = P:\Software ARCHIVE value =
Why is perl able to set the PROJECT value, but can't work with the lack of an ARCHIVE value? Is this an error with my copy of Perl (ActiveState 5.6.1)?

Replies are listed 'Best First'.
Re: Invalid use of constant pragma?
by mikeB (Friar) on Nov 29, 2001 at 01:42 UTC
    ARGV elements are undef, not a null string, when not supplied. Try the following to see...
    use Data::Dumper; # Because of my C++ background use constant PROJECT => $ARGV[0]; use constant ARCHIVE => $ARGV[1]; # debuggging print "PROJECT value = " . PROJECT . "\n"; print "ARCHIVE value = " . Dumper(ARCHIVE) . "\n"; if( ARCHIVE eq "") { warn( "No archive file was supplied.\n"); exit 1201; }
      The fact that $ARGV[1] (thus ARCHIVE) is undef and not an empty string isn't really relevent. He's doing a test for string equality via eq, and undef stringifies to an empty string, so the test should succeed.
      $ perl -e 'print "OK\n" if undef eq ""' OK
        D'Oh! You are, of course, correct :/

        I'll just pass along tye's CB suggestion that perhaps use strict; would be in order, to verify no typos exist in the actual code.

        Mike

        use strict; - don't leave home without it

      I did try the Data::Dumper package and I got the following output:
      ARCHIVE value = $VAR1 = '';
      Does this mean that the variable is set? I thought that's what I read about Data::Dumper.
        Interesting. When I run the program as in my previous node it displays ARCHIVE value = $VAR1 = undef; This is with Perl 5.6.0, ActiveState build#623.
Re: Invalid use of constant pragma?
by Vavoom (Scribe) on Nov 29, 2001 at 03:06 UTC
    Using the following code, I cannot duplicate your problem. I am also running ActiveState 5.6.1 build 629.

    #!/usr/bin/perl -w use strict; use constant PROJECT => $ARGV[0]; use constant ARCHIVE => $ARGV[1]; print "PROJECT value = " . PROJECT. "\n"; print "ARCHIVE value = " . ARCHIVE. "\n\n"; if (ARCHIVE eq "") { warn( "No archive file was supplied.\n"); exit 1201; } print "Still running\n";
    This consistantly gives the warning: No archive file was supplied. and then exits.

    I tried to reproduce your example accurately. Is there something I left out of my code that might account for your strange behaviour?

    Vavoom
Re: Invalid use of constant pragma?
by runrig (Abbot) on Nov 29, 2001 at 03:03 UTC
    You might want (at least to suppress uninitialized warnings, you do use strict and warnings, right?):
    # Because of my C++ background use constant PROJECT => (@ARGV) ? $ARGV[0] : ''; use constant ARCHIVE => (@ARGV > 1) ? $ARGV[1] : '';
    Hmm, undef should eq '', though with a warning if warnings are enabled. Oh well, the above should fix that also if that's what you're getting.
(tye)Re: Invalid use of constant pragma?
by tye (Sage) on Nov 29, 2001 at 03:05 UTC

    Well, you don't show lots of the code so it could be that you don't use strict and your real code says: if( ARCHVE eq "" ) { for example. Or it could be a more complex flow control problem that prevents the if block from ever running. I don't see any smoking guns in the code that you have provided.

            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-24 12:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found