Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

one of these codes is not like the other

by speedo (Initiate)
on Jul 16, 2001 at 23:55 UTC ( #97156=perlquestion: print w/replies, xml ) Need Help??
speedo has asked for the wisdom of the Perl Monks concerning the following question:

here's a piece of code that will print when run by my interpretter and on the web:
foreach $entry (@GILIST) { $seq1 = $gb->get_Seq_by_acc("$entry") || die "$entry is not a real + accession number.\n"; $speciesname = $seq1->species->common_name; $specieslist{$entry} = $speciesname; print p ("Gi# $entry is a $speciesname nucleotide sequence.\n"); }
A slight alteration to this code prevents it from printing to the webpage:
print p ("Gi# $entry is a $specieslist{$entry} nucleotide sequence +.\n");
And this version of the code does not print at all! :
foreach $entry (@GILIST) { $seq1 = $gb->get_Seq_by_acc("$entry") || die "$entry is not a real + accession number.\n"; $speciesname = $seq1->species->common_name; $specieslist{$entry} = $speciesname; } foreach my $entry (sort keys %specieslist) { print p ("GI# $entry is a $specieslist{$entry} nucleotide sequenc +e.\n"); }
And to add insult to injury the following version of my code prints when I run it with the interpretter but not on the web:
foreach $entry (@GILIST) { $seq1 = $gb->get_Seq_by_acc("$entry") || die "$entry is not a real + accession number.\n"; $speciesname = $seq1->species->common_name; unless ($speciesname eq $specieslist{$entry}) { $specieslist{$entry} = $speciesname; print p ("Gi# $entry is a $speciesname nucleotide sequence.\n" +); } }
In this case on the web it will print until the unless loops is "true". So does anyone have the SLIGHTEST INKLING what is happening?!? thanks in advance. . . . -speedo

Replies are listed 'Best First'.
Re: one of these codes is not like the other
by runrig (Abbot) on Jul 17, 2001 at 00:03 UTC
    I have a feeling that you are not using strict. Use it, make your code work with it, e.g., use 'foreach my $entry (@GILIST) {...', and see if your problem becomes apparent. If it does not become apparent, at least problems will be easier to find (for all of us).
      I followed your suggestion, and ended up with 20 or so: Global symbol "%blahblah" requires explicit package name. so I remedied this by adding "my"'s to my variables. But there's a syntax error I don't get: it's near "$specieslist{" of the following line of code:
      unless (my $speciesname eq (my specieslist{$entry})) {
      And then the pgm is aborted due to compilation errors. Does anyone see fundamental problems with that line? Thanks again -speedo
        unless (my $speciesname eq (my specieslist{$entry})) {
        You don't blindly put my in front of every variable. It is a way to declare variables, so you only put it in front of the first use of a variable (and in a scope where every other use can 'see' it). First, 'specieslist' is a hash, so somewhere you would have 'my %specieslist;' or 'my %specieslist = ....;'. and so speciesname and specieslist in your clause above would not have a my.

        And your clause above also has another typo, the lack of a '$' in front of specieslist (but I'm just assuming that's just a typo here from your error message). FYI, you are declaring a new lexical variable '$speciesname' above, which is only scoped (it only exists) in the 'unless' block, and it has an undefined value.

        Update: Just noticed this node that might help.

Re: one of these codes is not like the other
by nysus (Priest) on Jul 17, 2001 at 00:16 UTC
    Yes, you are definitely not using strict. A big no-no that will cause you much grief. Also: I'm not a CGI.pm guru but your print syntax looks pretty suspect, too. Here's example code from CGI:
    print start_html('A Simple Example'), h1('A Simple Example'), start_form, "What's your name? ",textfield('name'), p, "What's the combination?", p, checkbox_group(-name=>'words', -values=>['eenie','meenie','minie','moe'], -defaults=>['eenie','minie']), p, "What's your favorite color? ", popup_menu(-name=>'color', -values=>['red','green','blue','chartreuse']), p, submit, end_form, hr;
    Note the commas after the "p" tag. You may want to review this module.

    Update: Actually, TIMTOWTDI. Your print syntax looks alright.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop";
    $nysus = $PM . $MCF;
    Click here if you love Perl Monks

      Actually the P in this case just creates a <p> tag and then the comma continues the print statement. So the comma has no relation to the CGI.pm function, P, at all.

      Some minor corrections as credited to crazyinsomniac.

      $_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H; print chr($_-39); # Easy but its ok.
        Actually we don't know WHAT the p is (of course, we're assuming he's using CGI.pm, but who knows :-) . It might be a function, it might be a filehandle, it might be undefined. And perl behaves differently depending on what it is. Comment out the glob assignment, then the sub, then both, then neither in the following (and uncomment them both and switch 'em around):
        #!/usr/local/bin/perl #sub p { # return "bob\n"; #} *p=\*STDOUT; print p ("hello\n");
        Update: Oops, sorry damian, I wasn't keeping track of who was replying to who :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://97156]
Approved by root
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2018-01-19 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (218 votes). Check out past polls.

    Notices?