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

OK, this looks like a really wierd one to me... I've got code like this:
sub out_pic_fullsize { my $basepath=shift; my $basename=shift; my $data=shift; my $html = $templ; print "out_pic_fullsize($basepath, $basename, $data)\n"; open HTML, ">$basepath/$basename.html"; $html =~ s/%%basepath%%/$basepath/g; $html =~ s/%%basename%%/$basename/g; $html =~ s/%%data%%/$data/g; print HTML $html; close HTML; }

Now, there's somthing really odd going on here. When you call this sub, it should make the file $basepath/$basename.html, and take the template and replace the %%foo%% with the crurrent value of $foo. This works for the %%basepath%% and %%basename%%, but NOT for %%data%% substitions, which end up being simply deleted.

It gets even wierder. If I replace the filename with $basepath/$basename$data.html, the substitions work fine.

The print before the open, however, always works.

Any ideas, guys?

Thanks,
-=- James Mastros

Replies are listed 'Best First'.
Re: open() destroying my vars?
by redmist (Deacon) on Jul 29, 2000 at 06:08 UTC

    One issue that you should be aware of is that when opening a filehandle, you must use or die: $!. Also, if $basepath and $basename are different variables, there is some wierd stuff that goes on. I guess what is happening in that snippet is that Perl is being confused by you using 2 variables in that syntax and instance of using open. I walked over to another machine and threw together a little sumpthin' sumpthin' and the only thing that worked for me is catenating $basepath and $basename. So,

    open HTML, ">$basepath/$basename.html";

    Should be:

    $foo = "$basepath"."$basename.html"; open HTML, ">foo" or die "Can't open $foo: $!";

    Also make sure that if $basename is a HTML file, that the *value* of the variable has a suffix of '.html'.

    redmist
    redmist@users.sourceforge.net

      Thank you muchly, the script works perfectly now.

      The lines in question now read:

      $filename = "$basepath/$basename.html"; open HTML, ">$filename" or die "Couldn't open $filename for output\n";

      However, I still don't know what's up with it not working in the first place. Do you know what the "wierd stuff" or the "confusion" is?

      It's nothing important; the script works and I should be able to give a bill to the customer tommorow, but I just don't like not knowning why my code didn't work.

      My advice would be that you should always check the return value from an open call, but that die is not always the most appropriate response to a failure.

      Don't get me wrong - I'm sure that in 95% of cases (at least) is is the best action, but there are definitely times where alternative courses of action (log the error and move on, perhaps) would be more appropriate.

      --
      <http://www.dave.org.uk>

      European Perl Conference - Sept 22/24 2000, ICA, London
      <http://www.yapc.org/Europe/>
      OK, I'm an idiot. I was looking at absolutly the wrong place. My code is perfect (fine, anyway), my datafile isn't. Specificly, it's missing data for the pictures I was acatualy looking at as test cases. They were at the top, so the debugging print appears to work fine -- the problem had scrolled off the top of the screen!
      Sorry guys, and thanks for the undeservied rep! <G>
      -- theorbtwo -- http://www.rtweb.net/theorb/
Re: open() destroying my vars?
by TQuid (Sexton) on Jul 29, 2000 at 21:07 UTC
    open HTML, ">$basepath/$basename.html";

    This works fine, yes, until it fails, and then you don't know why.

    I just had to deal with a very frustrated customer whose webcart broke after his site was moved. The problem turned out to be that his directory ownership got "fixed" so that not everyone could write to it. The "programmer" who wrote his webcart had just such an open() without a die() after it. Until I put that in myself, there was no clue whatever as to what was wrong. As a system administrator who has to deal with this kind of thing, I would really like to find and maim the programmer who did this, and incredibly, who was PAID MONEY for such garbage code.

    So remember, it may work, but the wrath of the sysadmin is a great and terrible thing. Do it for your own sanity. Do it for Larry. And if you can't do those, do it for your continued good health.

    This message brought to you by the Retired Sysadmin's Sanitarium and Glue Factory. Please give generously.

    --TQuid

Re: open() destroying my vars?
by turnstep (Parson) on Jul 29, 2000 at 07:38 UTC
    open HTML, ">$basepath/$basename.html";

    This should work just fine. I use similar constructs all the time, without a problem. As long as the first two substitutions are happening, then the file has been opened. Perhaps give us a small sample data file that causes the action you describe?

      An exelecent point... however, upon looking further, both doing the substitution elsewhere and putting in an "or die", the script doesn't die. Moreover...
      1. It is going to the right file
      2. It works fine on three out of the four pages I'm using it for.
        I'm going to post a testcase for this soon, methinks.