Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using scalar to open and create file

Status
Not open for further replies.

myristicin

Programmer
Nov 15, 2005
1
US
I need to write a text file and name it with a numeric code submitted by a HTML form. Example - if the numeric for thanksgiving is 112405, I want to create a new text file called 112405.txt and then use the same perl script to overwrite it in the future by passing the same numeric. whew! here's my plan... will it work? any suggestions?
---

##.txt is appended to it so it will represent a text file name##

my $date = '$FORM_DATE{"numeric"}.txt;

##now I substitute the $date for the file I want to create and or overwrite##

open (TOFILE, "+>$date");

##ad now I would use print command to write or overwrite the file##

---

Gary
 
Your basic plan is correct but you have some typos.
Code:
my $date = '$FORM_DATE{"numeric"}.txt;
will not work. You've got a leading single-quote on the right-hand side which is not balanced. In fact, if you are wanting to construct a string and interpolate a variable (which is one approach), you need double-quotes anyway: Single quotes do not interpolate. Simply double-quoting the whole RHS will also break, as you've used double-quotes around the hash key in [tt]$FORM_DATE{"numeric"}[/tt] As you're not interpolating here, single quotes would be a better choice. In fact, perl lets you omit any quoting here as long as the key has the same semantics as a variable name ( ie, leading letter or underscore and then letters, digits and underscores ).

This leads to
Code:
my $date = "$FORM_DATE{numeric}.txt";

Many people code like this but, if you analyse what's going on here, it's inefficient. The double quotes construct a new string which we then assign to a new string before discarding. We could avoid the intermediate string altogether by using the concatenation operator '.' rather than the anonymous string constructors "". I'd prefer
Code:
my $date = $FORM_DATE{numeric} . '.txt';
for efficiency if this line were in a loop although I'd probably use the former for legibility if it were in initialisation or clean-up code.

I'm not convinced that
Code:
open (TOFILE, "+>$date");
will do exactly what you want either, but I can't tell from what you have said so far. '>' opeens in clobber mode and '>>' opens in append mode. You would use '+>' to open in read/write mode which may be what you want but is sufficiently unusual for me to flag it as a possible misunderstanding. Check out perlopentut and perlfaq5 for good discussions of file opening and file issues generally.

In any case, although many people code like this, you can give the mode as the second argument rather than concatenating it onto the filename. I believe that it's easier to read and more efficient (as it avoids creating another anonymous string). The syntax looks like this:
Code:
open (TOFILE, '+>', $date);

Your final question concerned printing with this new handle. You simply give it as the first argument to print() or printf() - but don't follow it with a comma. This slightly ugly syntax is so commonplace that it would be perverse to avoid it, so you'd say things like
Code:
print TOFILE 'TITLE: ', $title, "\n"; # note: no comma after filehandle
printf TOFILE "Pages: %d\n", $pages";
# not forgetting
close(TOFILE) or die "$0: $!";

Yours,

fish

["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.["]
--Maur
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top