save to file problem

Discussion in 'Perl Misc' started by phal, Nov 24, 2005.

  1. phal

    phal Guest

    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
    phal, Nov 24, 2005
    #1
    1. Advertising

  2. phal

    Guest

    phal wrote:
    > 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.
    , Nov 24, 2005
    #2
    1. Advertising

  3. phal

    Guest

    wrote:
    > You can escape one of the colons like this:
    > print "$dd:\:$name:\:$comment\n";


    Or something like:

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

    Or (my preference):

    printf "%s::%s::%s\n", $dd, $name, $comment;
    , Nov 24, 2005
    #3
  4. phal

    John Bokma Guest

    wrote:

    > wrote:
    >> You can escape one of the colons like this:
    >> print "$dd:\:$name:\:$comment\n";

    >
    > 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.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
    John Bokma, Nov 24, 2005
    #4
  5. phal

    Dr.Ruud Guest

    John Bokma:
    > :
    >> :


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

    >>
    >> print join '::', $dd, $name, $comment
    >>
    >> printf "%s::%s::%s\n", $dd, $name, $comment;

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


    { local ($OFS, $ORS) = ('::', "\n");
    print $dd, $name, $comment;
    }


    > :-D.


    blegh

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Nov 24, 2005
    #5
  6. <> wrote:

    >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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 24, 2005
    #6
  7. phal

    Ala Qumsieh Guest

    Dr.Ruud wrote:

    > { local ($OFS, $ORS) = ('::', "\n");
    > print $dd, $name, $comment;
    > }


    That requires a 'use English;'.

    --Ala
    Ala Qumsieh, Nov 24, 2005
    #7
  8. phal

    Guest

    Tad McClellan wrote:
    > 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.
    , Nov 25, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. user
    Replies:
    1
    Views:
    761
    Bruce Barker
    Apr 4, 2005
  2. teo
    Replies:
    1
    Views:
    459
  3. BusyBoy
    Replies:
    2
    Views:
    599
    =?Utf-8?B?TGVvbiBNYXluZQ==?=
    Oct 31, 2006
  4. subhadip
    Replies:
    0
    Views:
    627
    subhadip
    Mar 28, 2007
  5. Shahar Golan
    Replies:
    5
    Views:
    289
    kaeli
    Oct 16, 2003
Loading...

Share This Page