save to file problem

P

phal

Hi all

I have a script to save the user input into text file

$q=new CGI;

open (somet,">> saveall.txt" );
$name=$q->param("name"); # king -> user input
$comment=$q->param("comment"); # go to school-> user input

$dd="300/20/500/30:200 4";
print somet "$dd::$name::$comment\n";


when save to the file saveall.txt as the followong only
300/20/500/30:200 4::go to school

I dont know why the name part doesnt exist there after the 4::
it should save this
300/20/500/30:200 4::king::go to school

the code above work if I using only one " : "

print somet "$dd:$name:$comment\n"

I using Perl 5.8.4
 
U

usenet

phal said:
print somet "$dd::$name::$comment\n";
snip
I dont know why the name part doesnt exist there after the 4::
snip
the code above work if I using only one " : "

Right. The "::" tells Perl you are specifying a package identifier (all
variables are in a package; for example, you could print main::$dd
which is the same as $dd in this case).

You can escape one of the colons like this:

print "$dd:\:$name:\:$comment\n";

and it won't trick Perl into thinking you are using a package
identifier.
 
J

John Bokma

Or something like:

print join '::', $dd, $name, $comment

Or (my preference):

printf "%s::%s::%s\n", $dd, $name, $comment;

print "${dd}::${name}::${comment}\n";

:-D.
 
T

Tad McClellan

The "::" tells Perl you are specifying a package identifier (all
variables are in a package;


Not all variables are in a package.

(I'm sure David knows this, but that isn't what he said. :)

Perl has 2 different and separate variable systems, lexical variables
and package variables.

All package variables (our, local) are in a package.

Lexical variables (my) are never in a package.
 
U

usenet

Tad said:
Not all variables are in a package.
(I'm sure David knows this, but that isn't what he said. :)

Oops. I meant to point out that all the variables in the OP's program
were in a package (namely, main).

Thanks for the catch. I hate to be imprecise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top